· 10 years ago · Sep 10, 2016, 07:20 AM
1<?php
2
3/*
4 * RBAgency_Init Class
5 *
6 * These are core functions needed to enable a WordPress plugin shell
7 * and handle common plugin functions like activation & uninstall, etc.
8 */
9
10class RBAgency_Init {
11
12
13 // *************************************************************************************************** //
14 /*
15 *
16 */
17
18 public static function init(){
19
20 /*
21 * Internationalization
22 */
23
24 // Identify Folder for PO files
25 load_plugin_textdomain( RBAGENCY_TEXTDOMAIN, false, basename( dirname( __FILE__ ) ) . '/assets/translation/' );
26
27 // Register Settings
28 add_action('admin_init', array('RBAgency_Init', 'do_register_settings'));
29
30 }
31
32
33
34 // *************************************************************************************************** //
35
36 /*
37 * Plugin Activation
38 * Run when the plugin is installed.
39 */
40
41 public static function activation(){
42
43 // Required for all WordPress database manipulations
44 global $wpdb;
45 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
46
47 /*
48 * Check Permissions
49 */
50 // Does the user have permission to activate the plugin
51 if ( !current_user_can('activate_plugins') )
52 return;
53 // Check Admin Referer
54 $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
55 check_admin_referer( "activate-plugin_{$plugin}" );
56
57
58 /*
59 * Setup Required Directories
60 */
61
62 // Create Upload Directory
63 if (!is_dir(RBAGENCY_UPLOADPATH)) {
64 @mkdir(RBAGENCY_UPLOADPATH, 0755);
65 @chmod(RBAGENCY_UPLOADPATH, 0777);
66 }
67
68
69 /*
70 * Initialize Options
71 */
72
73 // Update the options in the database
74 if(!get_option("rb_agency_options")) {
75
76 // Set Default Options
77 $rb_agency_options_arr = array(
78/*... TRUNCATED FOR SECUIRTY ... */
79 );
80 // Add Options
81 //add_option("rb_agency_options",$rb_agency_options_arr);
82 update_option("rb_agency_options",$rb_agency_options_arr);
83 }
84
85 /*
86 * License Key
87 */
88
89 // Set license key via the RB_AGENCY_LICENSE constant or the $rb_agency_LICENSE variable
90 global $rb_agency_LICENSE;
91 // Set Constant
92 $license_key = defined("RB_AGENCY_LICENSE") && empty($rb_agency_LICENSE) ? RB_AGENCY_LICENSE : $rb_agency_LICENSE;
93 if(!empty($license_key)) {
94 // Store Key
95 update_option("rb_agency_license", md5($license_key));
96 }
97
98
99 /*
100 * Install Schema
101 */
102
103 // Profile
104 $sql = "CREATE TABLE IF NOT EXISTS " . table_agency_profile
105/*... TRUNCATED FOR SECUIRTY ... */
106
107
108
109
110 /*
111 * Flush rewrite rules
112 */
113
114 // Flush rewrite rules
115 self::flush_rules();
116 }
117
118
119 /*
120 * Plugin Deactivation
121 * Cleanup when complete
122 */
123
124 public static function deactivation(){
125 /*
126 // Does user have correct permissions?
127 if ( ! current_user_can( 'activate_plugins' ) )
128 return;
129
130 // Is it coming from the right referer?
131 $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
132 check_admin_referer( "deactivate-plugin_{$plugin}" );
133 */
134 // TODO: Enhance
135
136 }
137
138
139 /*
140 * Plugin Uninstall
141 * Cleanup when complete
142 */
143
144 public static function uninstall(){
145
146 // Permission Granted... Remove
147 global $wpdb; // Required for all WordPress database manipulations
148
149 // Drop the tables
150 $wpdb->query("DROP TABLE " . table_agency_xxx);
151/*... TRUNCATED FOR SECUIRTY ... */
152
153 // Delete Saved Settings
154 delete_option('rb_agency_options');
155
156 /*
157 // Deactivate Plugin
158 $thepluginfile = "rb-agency/rb-agency.php";
159 $current = get_settings('active_plugins');
160 array_splice($current, array_search( $thepluginfile, $current), 1 );
161 update_option('active_plugins', $current);
162 do_action('deactivate_' . $thepluginfile );
163 */
164
165 // Redirect back to Plugins
166 echo "<div style=\"padding:50px;font-weight:bold;\"><p>". __("Almost done...", RBAGENCY_TEXTDOMAIN) ."</p><h1>". __("please uninstall on plugins page.", RBAGENCY_TEXTDOMAIN) ."</h1><a href=\"plugins.php?deactivate=true\">". __("Please click here to complete the uninstallation process", RBAGENCY_TEXTDOMAIN) ."</a></h1></div>";
167 die;
168
169 }
170
171
172
173 /*
174 * Register Settings
175 * Register Settings group
176 */
177
178 public static function do_register_settings() {
179 register_setting('rb-agency-settings-group', 'rb_agency_options'); //, 'rb_agency_options_validate'
180 register_setting('rb-agency-settings-layout-group', 'rb_agency_layout_options');
181 register_setting('rb-agency-dummy-settings-group', 'rb_agency_dummy_options'); //, setup dummy profile options
182 }
183
184
185 /*
186 * License
187 * Updates and License related
188 */
189
190 // Get License Key
191 public static function get_key(){
192 return get_option("rb_agency_license");
193 }
194
195
196
197 // *************************************************************************************************** //
198
199
200 /*
201 * Flush Rewrite Rules
202 * Remember to flush_rules() when adding rules
203 */
204
205 public static function flush_rules(){
206
207 global $wp_rewrite;
208 $wp_rewrite->flush_rules();
209
210 }
211
212
213 // *************************************************************************************************** //
214
215
216 /*
217 * Update Needed
218 * Is this an updated version of the software and needs database upgrade?
219 */
220
221 public static function update_check(){
222
223 // Hold the version in a seprate option
224 if(!get_option("rb_agency_version")) {
225 update_option("rb_agency_version", RBAGENCY_VERSION);
226 } else {
227 // Version Exists, but is it out of date?
228 if(get_option("rb_agency_version") <> RBAGENCY_VERSION){
229 include_once(RBAGENCY_PLUGIN_DIR .'/upgrade.php');
230 } else {
231 // Namaste, version number is correct
232 }
233 }
234 }
235
236
237 /*
238 * Upgrade Check
239 * Is there a newer version of the software available to upgrade to?
240 */
241
242 public static function upgrade_check(){
243 // TODO:
244 //if(!class_exists("RBAgency_Update"))
245 //require_once("update.php");
246
247 //return RBAgency_Update::check_version($update_plugins_option, true);
248 }
249
250
251 // *************************************************************************************************** //
252
253
254 /*
255 * Diagnostics
256 */
257
258 // Check Setup
259 public static function setup_check(){
260 // Get Options
261 $options = get_option('arez_options'); // TODO
262
263 // Check if missing permalinks
264 if ( isset($options['authorized']) && ! $options['authorized'] ) {
265 // Hide if on Settings Page
266 if ( (isset($_GET["page"]) && $_GET["page"] == 'arez') || (isset($_GET["page"]) && $_GET["page"] == 'arez-settings') ) {
267 } else {
268 echo '<div class="updated"><p>ActivityRez Plugin ready for setup. <a href="'. admin_url("admin.php?page=arez") .'">Click here to get started</a>.</p></div>';
269 }
270 }
271
272 }
273
274 // Check Permalinks
275 public static function permalinks_check(){
276 // Check if missing permalinks
277 if ( ! get_option('permalink_structure') ) {
278 // Check if we are already on settings page
279 if ( get_option( 'rbagency_permalinkignore' ) !== true ) {
280 // Hide if on Settings Page
281 if (isset($_GET["page"]) && $_GET["page"] == 'arez-settings') {
282 } else {
283 echo '<div class="error"><p>WARNING: Your permalinks are not set. <a href="'. admin_url("admin.php?page=arez-settings") .'">Click here to resolve</a>.</p><span class="dismiss"><a href="'. admin_url("admin.php?page=arez-settings&action=permalink-dismiss") .'">Dismiss</a></div>';
284 }
285 }
286 }
287
288 }
289
290 // Check Permalinks
291 public static function permalinks_change(){
292 global $wp_rewrite;
293 $wp_rewrite->set_permalink_structure('/%category%/%postname%/');
294 $wp_rewrite->flush_rules();
295 }
296
297
298}