-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-academic-standards.php
223 lines (191 loc) · 8.01 KB
/
wp-academic-standards.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
Plugin Name: WP Academic Standards
Plugin URI: https://www.navigationnorth.com
Description: Wordpress Academic Standards
Version: 0.2.4
Requires at least: 4.4
Requires PHP: 7.0
Tested up to: 6.0.2
Author: Navigation North
Author URI: https://www.navigationnorth.com
Text Domain: wp-academic-standards
License: GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Copyright (C) 2019 Navigation North
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
//defining constants for slugs, path, name, and version
define( 'WAS_URL', plugin_dir_url(__FILE__) );
define( 'WAS_PATH', plugin_dir_path(__FILE__) );
define( 'WAS_SLUG','wp-academic-standards' );
define( 'WAS_FILE',__FILE__);
define( 'WAS_PLUGIN_NAME', 'WP Academic Standards' );
define( 'WAS_ADMIN_PLUGIN_NAME', 'WP Academic Standards');
define( 'WAS_VERSION', '0.2.4' );
global $_oer_prefix, $message, $type;
$_oer_prefix = "oer_";
include_once(WAS_PATH.'includes/init.php');
include_once(WAS_PATH.'includes/functions.php');
register_activation_hook(__FILE__, 'was_create_table');
function was_create_table()
{
global $wpdb;
$subprefix = "oer_";
//Change hard-coded table prefix to $wpdb->prefix
$table_name = $wpdb->prefix . $subprefix . "core_standards";
if($wpdb->get_var("show tables like '$table_name'") != $table_name)
{
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id int(20) NOT NULL AUTO_INCREMENT,
standard_name varchar(255) NOT NULL,
standard_url varchar(255) NOT NULL,
PRIMARY KEY (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//Change hard-coded table prefix to $wpdb->prefix
$table_name = $wpdb->prefix . $subprefix . "sub_standards";
if($wpdb->get_var("show tables like '$table_name'") != $table_name)
{
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id int(20) NOT NULL AUTO_INCREMENT,
parent_id varchar(255) NOT NULL,
standard_title varchar(1000) NOT NULL,
url varchar(255) NOT NULL,
PRIMARY KEY (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Alter substandards table and add pos field
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '".$table_name."' AND column_name = 'pos'" );
if(empty($row)){
$sql = "ALTER TABLE ".$table_name." ADD pos INT(11) NOT NULL";
$wpdb->query($sql);
}
// One Time alteration of standard_title field size in sub standards table
//$sql = "ALTER TABLE ".$table_name." MODIFY COLUMN standard_title VARCHAR(1000)";
//$wpdb->query($sql);
//Change hard-coded table prefix to $wpdb->prefix
$table_name = $wpdb->prefix . $subprefix . "standard_notation";
if($wpdb->get_var("show tables like '$table_name'") != $table_name)
{
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id int(20) NOT NULL AUTO_INCREMENT,
parent_id varchar(255) NOT NULL,
standard_notation varchar(255) NOT NULL,
description varchar(1000) NOT NULL,
comment varchar(255) NOT NULL,
url varchar(255) NOT NULL,
PRIMARY KEY (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Alter standard notation table and add pos field
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '".$table_name."' AND column_name = 'pos'" );
if(empty($row)){
$sql = "ALTER TABLE ".$table_name." ADD pos INT(11) NOT NULL";
$wpdb->query($sql);
}
// One Time alteration of description field size in standard notation table
//$sql = "ALTER TABLE ".$table_name." MODIFY COLUMN description VARCHAR(1000)";
//$wpdb->query($sql);
was_add_rewrites();
//Trigger permalink reset
flush_rewrite_rules();
}
//Load localization directory
add_action('plugins_loaded', 'was_load_textdomain');
function was_load_textdomain() {
load_plugin_textdomain( 'wp-academic-standards', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
}
/** Add Settings Link on Plugins page **/
add_filter( 'plugin_action_links' , 'was_add_settings_link' , 10 , 2 );
/** Add Settings Link function **/
function was_add_settings_link( $links, $file ){
if ( $file == plugin_basename(dirname(__FILE__).'/wp-academic-standards.php') ) {
/** Insert settings link **/
$link = "<a href='edit.php?post_type=standards&page=was_settings'>".__('Settings','wp-acad')."</a>";
array_unshift($links, $link);
/** End of Insert settings link **/
}
return $links;
}
// Add rewrite rule for substandards
add_action( 'init', 'was_add_rewrites', 10, 0 );
function was_add_rewrites($root_slug="standards")
{
global $wp_rewrite;
$root_slug = get_option('was_standard_slug');
add_rewrite_tag( '%standard%', '([^&]+)' );
add_rewrite_tag( '%substandard%' , '([^&]+)' );
add_rewrite_tag( '%notation%' , '([^&]+)' );
add_rewrite_rule( '^'.$root_slug.'/([^/]*)/?$', 'index.php?standard=$matches[1]', 'top' );
add_rewrite_rule( '^'.$root_slug.'/([^/]*)/([^&]+)/?$', 'index.php?standard=$matches[1]&substandard=$matches[2]', 'top' );
add_rewrite_rule( '^'.$root_slug.'/([^/]*)/([^&]+)/([^/]*)/?$', 'index.php?standard=$matches[1]&substandard=$matches[2]¬ation=$matches[3]', 'top' );
$flush_rewrite = get_option('oer_rewrite_rules');
if ($flush_rewrite==false) {
$wp_rewrite->init();
$wp_rewrite->flush_rules();
update_option('oer_rewrite_rules', true);
}
}
add_filter( 'query_vars', 'was_add_query_vars' );
function was_add_query_vars( $vars ){
$vars[] = "standard";
$vars[] = "substandard";
$vars[] = "notation";
return $vars;
}
add_action( 'template_include' , 'was_assign_standard_template' );
function was_assign_standard_template($template) {
global $wp_query;
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
$root_slug = get_option('was_standard_slug');
if (!$root_slug || $root_slug==""){
$root_slug = "standards";
}
status_header(200);
$slug_chars = strlen($root_slug);
if ( substr($url_path,-$slug_chars)==$root_slug && !get_query_var('standard') && !get_query_var('substandard') && !get_query_var('notation') ) {
// load the file if exists
$wp_query->is_404 = false;
$template = locate_template('template/frontend/standards.php', true);
if (!$template) {
$template = dirname(__FILE__) . '/template/frontend/standards.php';
}
} elseif (get_query_var('standard') && !get_query_var('substandard') && !get_query_var('notation')){
$wp_query->is_404 = false;
$template = locate_template('template/frontend/template-standard.php', true);
if (!$template) {
$template = dirname(__FILE__) . '/template/frontend/template-standard.php';
}
} elseif (get_query_var('standard') && get_query_var('substandard') && !get_query_var('notation')){
$wp_query->is_404 = false;
$template = locate_template('template/frontend/template-substandard.php', true);
if (!$template) {
$template = dirname(__FILE__) . '/template/frontend/template-substandard.php';
}
} elseif (get_query_var('standard') && get_query_var('substandard') && get_query_var('notation')){
$wp_query->is_404 = false;
$template = locate_template('template/frontend/template-notation.php', true);
if (!$template) {
$template = dirname(__FILE__) . '/template/frontend/template-notation.php';
}
}
return $template;
}
?>