mtekk_adminKit

The Breadcrumb NavXT mtekk adminKit class.

Defined (1)

The class is defined in the following location(s).

/includes/class.mtekk_adminkit.php  
  1. abstract class mtekk_adminKit 
  2. const version = '1.5.60'; 
  3. protected $full_name; 
  4. protected $short_name; 
  5. protected $plugin_basename; 
  6. protected $access_level = 'manage_options'; 
  7. protected $identifier; 
  8. protected $unique_prefix; 
  9. protected $opt = array(); 
  10. protected $message; 
  11. protected $support_url; 
  12. protected $allowed_html; 
  13. function __construct() 
  14. //Admin Init Hook 
  15. add_action('admin_init', array($this, 'init')); 
  16. //WordPress Admin interface hook 
  17. add_action('admin_menu', array($this, 'add_page')); 
  18. //Installation Script hook 
  19. add_action('activate_' . $this->plugin_basename, array($this, 'install')); 
  20. //Initilizes l10n domain 
  21. $this->local(); 
  22. add_action('wp_loaded', array($this, 'wp_loaded')); 
  23. //Register Help Output 
  24. //add_action('add_screen_help_and_options', array($this, 'help')); 
  25. function wp_loaded() 
  26. //Filter our allowed html tags 
  27. $this->allowed_html = apply_filters($this->unique_prefix . '_allowed_html', wp_kses_allowed_html('post')); 
  28. /** 
  29. * Returns the internal mtekk_admin_class version 
  30. */ 
  31. function get_admin_class_version() 
  32. return mtekk_adminKit::version; 
  33. /** 
  34. * Return the URL of the settings page for the plugin 
  35. */ 
  36. function admin_url() 
  37. return admin_url('options-general.php?page=' . $this->identifier); 
  38. /** 
  39. * A wrapper for nonced_anchor returns a nonced anchor for admin pages 
  40. *  
  41. * @param string $mode The nonce "mode", a unique string at the end of the standardized nonce identifier 
  42. * @param string $title (optional) The text to use in the title portion of the anchor 
  43. * @param string $text (optional) The text that will be surrounded by the anchor tags 
  44. * @return string the assembled anchor 
  45. */ 
  46. function admin_anchor($mode, $title = '', $text = '') 
  47. return $this->nonced_anchor($this->admin_url(), 'admin_' . $mode, 'true', $title, $text); 
  48. /** 
  49. * Returns a properly formed nonced anchor to the specified URI 
  50. *  
  51. * @param string $uri The URI that the anchor should be for 
  52. * @param string $mode The nonce "mode", a unique string at the end of the standardized nonce identifier 
  53. * @param mixed $value (optional) The value to place in the query string  
  54. * @param string $title (optional) The text to use in the title portion of the anchor 
  55. * @param string $text (optional) The text that will be surrounded by the anchor tags 
  56. * @param string $anchor_extras (optional) This text is placed within the opening anchor tag, good for adding id, classe, rel field 
  57. * @return string the assembled anchor 
  58. */ 
  59. function nonced_anchor($uri, $mode, $value = 'true', $title = '', $text = '', $anchor_extras = '') 
  60. //Assemble our url, nonce and all 
  61. $url = wp_nonce_url(add_query_arg($this->unique_prefix . '_' . $mode, $value, $uri), $this->unique_prefix . '_' . $mode); 
  62. //Return a valid anchor 
  63. return ' <a title="' . $title . '" href="' . $url . '" '. $anchor_extras . '>' . $text . '</a>'; 
  64. /** 
  65. * Abstracts the check_admin_referer so that all the end user has to supply is the mode 
  66. *  
  67. * @param string $mode The specific nonce "mode" (see nonced_anchor) that is being checked 
  68. */ 
  69. function check_nonce($mode) 
  70. check_admin_referer($this->unique_prefix . '_' . $mode); 
  71. /** 
  72. * Makes sure the current user can manage options to proceed 
  73. */ 
  74. function security() 
  75. //If the user can not manage options we will die on them 
  76. if(!current_user_can($this->access_level)) 
  77. wp_die(__('Insufficient privileges to proceed.', $this->identifier)); 
  78. function init() 
  79. //Admin Options reset hook 
  80. if(isset($_POST[$this->unique_prefix . '_admin_reset'])) 
  81. //Run the reset function on init if reset form has been submitted 
  82. $this->opts_reset(); 
  83. //Admin Options export hook 
  84. else if(isset($_POST[$this->unique_prefix . '_admin_export'])) 
  85. //Run the export function on init if export form has been submitted 
  86. $this->opts_export(); 
  87. //Admin Options import hook 
  88. else if(isset($_FILES[$this->unique_prefix . '_admin_import_file']) && !empty($_FILES[$this->unique_prefix . '_admin_import_file']['name'])) 
  89. //Run the import function on init if import form has been submitted 
  90. $this->opts_import(); 
  91. //Admin Options rollback hook 
  92. else if(isset($_GET[$this->unique_prefix . '_admin_undo'])) 
  93. //Run the rollback function on init if undo button has been pressed 
  94. $this->opts_undo(); 
  95. //Admin Options upgrade hook 
  96. else if(isset($_GET[$this->unique_prefix . '_admin_upgrade'])) 
  97. //Run the upgrade function on init if upgrade button has been pressed 
  98. $this->opts_upgrade_wrapper(); 
  99. //Admin Options fix hook 
  100. else if(isset($_GET[$this->unique_prefix . '_admin_fix'])) 
  101. //Run the options fix function on init if fix button has been pressed 
  102. $this->opts_upgrade_wrapper(); 
  103. //Admin Options update hook 
  104. else if(isset($_POST[$this->unique_prefix . '_admin_options'])) 
  105. //Temporarily add update function on init if form has been submitted 
  106. $this->opts_update(); 
  107. //Add in the nice "settings" link to the plugins page 
  108. add_filter('plugin_action_links', array($this, 'filter_plugin_actions'), 10, 2); 
  109. if(defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) 
  110. $suffix = ''; 
  111. else 
  112. $suffix = '.min'; 
  113. //Register JS for enable/disable settings groups 
  114. wp_register_script('mtekk_adminkit_engroups', plugins_url('/mtekk_adminkit_engroups' . $suffix . '.js', dirname(__FILE__) . '/mtekk_adminkit_engroups' . $suffix . '.js'), array('jquery'), self::version, true); 
  115. //Register JS for tabs 
  116. wp_register_script('mtekk_adminkit_tabs', plugins_url('/mtekk_adminkit_tabs' . $suffix . '.js', dirname(__FILE__) . '/mtekk_adminkit_tabs' . $suffix . '.js'), array('jquery-ui-tabs'), self::version, true); 
  117. //Register CSS for tabs 
  118. wp_register_style('mtekk_adminkit_tabs', plugins_url('/mtekk_adminkit_tabs' . $suffix . '.css', dirname(__FILE__) . '/mtekk_adminkit_tabs' . $suffix . '.css')); 
  119. //Register options 
  120. register_setting($this->unique_prefix . '_options', $this->unique_prefix . '_options', ''); 
  121. //Synchronize up our settings with the database as we're done modifying them now 
  122. $this->opt = $this::parse_args($this->get_option($this->unique_prefix . '_options'), $this->opt); 
  123. //Run the opts fix filter 
  124. $this->opts_fix($this->opt); 
  125. /** 
  126. * Adds the adminpage the menu and the nice little settings link 
  127. * TODO: make this more generic for easier extension 
  128. */ 
  129. function add_page() 
  130. //Add the submenu page to "settings" menu 
  131. $hookname = add_submenu_page('options-general.php', $this->full_name, $this->short_name, $this->access_level, $this->identifier, array($this, 'admin_page')); 
  132. // check capability of user to manage options (access control) 
  133. if(current_user_can($this->access_level)) 
  134. //Register admin_head-$hookname callback 
  135. add_action('admin_head-' . $hookname, array($this, 'admin_head')); 
  136. //Register admin_print_styles-$hookname callback 
  137. add_action('admin_print_styles-' . $hookname, array($this, 'admin_styles')); 
  138. //Register admin_print_scripts-$hookname callback 
  139. add_action('admin_print_scripts-' . $hookname, array($this, 'admin_scripts')); 
  140. //Register Help Output 
  141. add_action('load-' . $hookname, array($this, 'help')); 
  142. /** 
  143. * Initilizes localization textdomain for translations (if applicable) 
  144. *  
  145. * Will conditionally load the textdomain for translations. This is here for 
  146. * plugins that span multiple files and have localization in more than one file 
  147. *  
  148. * @return void 
  149. */ 
  150. function local() 
  151. global $l10n; 
  152. // the global and the check might become obsolete in 
  153. // further wordpress versions 
  154. // @see https://core.trac.wordpress.org/ticket/10527  
  155. if(!isset($l10n[$this->identifier])) 
  156. load_plugin_textdomain($this->identifier, false, $this->identifier . '/languages'); 
  157. /** 
  158. * Places in a link to the settings page in the plugins listing entry 
  159. *  
  160. * @param array $links An array of links that are output in the listing 
  161. * @param string $file The file that is currently in processing 
  162. * @return array Array of links that are output in the listing. 
  163. */ 
  164. function filter_plugin_actions($links, $file) 
  165. //Make sure we are adding only for the current plugin 
  166. if($file == $this->plugin_basename) 
  167. {  
  168. //Add our link to the end of the array to better integrate into the WP 2.8 plugins page 
  169. $links[] = '<a href="' . $this->admin_url() . '">' . __('Settings') . '</a>'; 
  170. return $links; 
  171. /** 
  172. * Checks to see if the plugin has been fully installed 
  173. * @return bool whether or not the plugin has been installed 
  174. */ 
  175. function is_installed() 
  176.  
  177. /**  
  178. * This sets up and upgrades the database settings, runs on every activation 
  179. */ 
  180. function install() 
  181. //Call our little security function 
  182. $this->security(); 
  183. //Try retrieving the options from the database 
  184. $opts = $this->get_option($this->unique_prefix . '_options'); 
  185. //If there are no settings, copy over the default settings 
  186. if(!is_array($opts)) 
  187. //Grab defaults from the object 
  188. $opts = $this->opt; 
  189. //Add the options 
  190. $this->add_option($this->unique_prefix . '_options', $opts); 
  191. $this->add_option($this->unique_prefix . '_options_bk', $opts, '', 'no'); 
  192. //Add the version, no need to autoload the db version 
  193. $this->add_option($this->unique_prefix . '_version', $this::version, '', 'no'); 
  194. else 
  195. //Retrieve the database version 
  196. $db_version = $this->get_option($this->unique_prefix . '_version'); 
  197. if($this::version !== $db_version) 
  198. //Run the settings update script 
  199. $this->opts_upgrade($opts, $db_version); 
  200. //Always have to update the version 
  201. $this->update_option($this->unique_prefix . '_version', $this::version); 
  202. //Store the options 
  203. $this->update_option($this->unique_prefix . '_options', $this->opt); 
  204. /** 
  205. * This removes database settings upon deletion of the plugin from WordPress 
  206. */ 
  207. function uninstall() 
  208. //Remove the option array setting 
  209. $this->delete_option($this->unique_prefix . '_options'); 
  210. //Remove the option backup array setting 
  211. $this->delete_option($this->unique_prefix . '_options_bk'); 
  212. //Remove the version setting 
  213. $this->delete_option($this->unique_prefix . '_version'); 
  214. /** 
  215. * Compares the supplided version with the internal version, places an upgrade warning if there is a missmatch 
  216. * TODO: change this to being auto called in admin_init action 
  217. */ 
  218. function version_check($version) 
  219. //If we didn't get a version, setup 
  220. if($version === false) 
  221. //Add the version, no need to autoload the db version 
  222. $this->add_option($this->unique_prefix . '_version', $this::version, '', 'no'); 
  223. //Do a quick version check 
  224. if($version && version_compare($version, $this::version, '<') && is_array($this->opt)) 
  225. //Throw an error since the DB version is out of date 
  226. $this->message['error'][] = __('Your settings are for an older version of this plugin and need to be migrated.', $this->identifier) . $this->admin_anchor('upgrade', __('Migrate the settings now.', $this->identifier), __('Migrate now.', $this->identifier)); 
  227. //Output any messages that there may be 
  228. $this->messages(); 
  229. return false; 
  230. //Do a quick version check 
  231. else if($version && version_compare($version, $this::version, '>') && is_array($this->opt)) 
  232. //Let the user know that their settings are for a newer version 
  233. $this->message['error'][] = __('Your settings are for a newer version of this plugin.', $this->identifier) . $this->admin_anchor('upgrade', __('Migrate the settings now.', $this->identifier), __('Attempt back migration now.', $this->identifier)); 
  234. //Output any messages that there may be 
  235. $this->messages(); 
  236. return true; 
  237. else if(!is_array($this->opt)) 
  238. //Throw an error since it appears the options were never registered 
  239. $this->message['error'][] = __('Your plugin install is incomplete.', $this->identifier) . $this->admin_anchor('upgrade', __('Load default settings now.', $this->identifier), __('Complete now.', $this->identifier)); 
  240. //Output any messages that there may be 
  241. $this->messages(); 
  242. return false; 
  243. else if(!$this->opts_validate($this->opt)) 
  244. //Throw an error since it appears the options contain invalid data 
  245. $this->message['error'][] = __('One or more of your plugin settings are invalid.', $this->identifier) . $this->admin_anchor('fix', __('Attempt to fix settings now.', $this->identifier), __('Fix now.', $this->identifier)); 
  246. //Output any messages that there may be 
  247. $this->messages(); 
  248. return false; 
  249. return true; 
  250. /** 
  251. * A prototype function. End user should override if they need this feature. 
  252. */ 
  253. function opts_validate(&$opts) 
  254. return true; 
  255. /** 
  256. * A prototype function. End user should override if they need this feature. 
  257. *  
  258. * @param array $opts 
  259. */ 
  260. function opts_fix(&$opts) 
  261. /** 
  262. * Synchronizes the backup options entry with the current options entry 
  263. */ 
  264. function opts_backup() 
  265. //Set the backup options in the DB to the current options 
  266. $this->update_option($this->unique_prefix . '_options_bk', $this->get_option($this->unique_prefix . '_options')); 
  267. /** 
  268. * Runs recursivly through the opts array, sanitizing and merging in updates from the $input array 
  269. *  
  270. * @param array $opts good, clean array 
  271. * @param array $input unsanitzed input array, not trusted at all 
  272. * @todo This function should probably get a filter thrown within it to be more extensible 
  273. */ 
  274. protected function opts_update_loop(&$opts, $input) 
  275. //Loop through all of the existing options (avoids random setting injection) 
  276. foreach($opts as $option => $value) 
  277. //If we have an array, dive into another recursive loop 
  278. if(isset($input[$option]) && is_array($value)) 
  279. $this->opts_update_loop($opts[$option], $input[$option]); 
  280. //We must check for unset settings, but booleans are ok to be unset 
  281. else if(isset($input[$option]) || $option[0] == 'b') 
  282. switch($option[0]) 
  283. //Handle the boolean options 
  284. case 'b': 
  285. $opts[$option] = isset($input[$option]); 
  286. break; 
  287. //Handle the integer options 
  288. case 'i': 
  289. $opts[$option] = (int) $input[$option]; 
  290. break; 
  291. //Handle the absolute integer options 
  292. case 'a': 
  293. $opts[$option] = absint($input[$option]); 
  294. break; 
  295. //Handle the floating point options 
  296. case 'f': 
  297. $opts[$option] = (float) $input[$option]; 
  298. break; 
  299. //Handle the HTML options 
  300. case 'h': 
  301. //May be better to use wp_kses here 
  302. $opts[$option] = wp_kses(stripslashes($input[$option]), $this->allowed_html); 
  303. break; 
  304. //Handle the HTML options that must not be null 
  305. case 'H': 
  306. if(isset($input[$option])) 
  307. $opts[$option] = wp_kses(stripslashes($input[$option]), $this->allowed_html); 
  308. break; 
  309. //Handle the text options that must not be null 
  310. case 'S': 
  311. if(isset($input[$option])) 
  312. $opts[$option] = esc_html($input[$option]); 
  313. break; 
  314. //Deal with strings that can be null 
  315. case 's': 
  316. $opts[$option] = esc_html($input[$option]); 
  317. break; 
  318. //Deal with enumerated types 
  319. case 'E': 
  320. $opts[$option] = $this->opts_sanitize_enum($input[$option], $option); 
  321. break; 
  322. //By default we have nothing to do, allows for internal settings 
  323. default: 
  324. break; 
  325. /** 
  326. * Simple sanitization function for enumerated types, end users should overload this 
  327. * with something more usefull 
  328. *  
  329. * @param string $value The input value from the form 
  330. * @param string $option The option name 
  331. * @return string The sanitized enumerated string 
  332. */ 
  333. private function opts_sanitize_enum($value, $option) 
  334. return esc_html($value); 
  335. /** 
  336. * A better version of parse_args, will recrusivly follow arrays 
  337. *  
  338. * @param mixed $args The arguments to be parsed 
  339. * @param mixed $defaults (optional) The default values to validate against 
  340. * @return mixed 
  341. */ 
  342. static function parse_args($args, $defaults = '') 
  343. if(is_object($args)) 
  344. $r = get_object_vars($args); 
  345. else if(is_array($args)) 
  346. $r =& $args; 
  347. else 
  348. wp_parse_str($args, $r);  
  349. if(is_array($defaults)) 
  350. return mtekk_adminKit::array_merge_recursive($defaults, $r); 
  351. return $r; 
  352. /** 
  353. * An alternate version of array_merge_recursive, less flexible 
  354. * still recursive, ~2x faster than the more flexible version 
  355. *  
  356. * @param array $arg1 first array 
  357. * @param array $arg2 second array to merge into $arg1 
  358. * @return array 
  359. */ 
  360. static function array_merge_recursive($arg1, $arg2) 
  361. foreach($arg2 as $key => $value) 
  362. if(array_key_exists($key, $arg1) && is_array($value)) 
  363. $arg1[$key] = mtekk_adminKit::array_merge_recursive($arg1[$key], $value); 
  364. else 
  365. $arg1[$key] = $value; 
  366. return $arg1; 
  367. /** 
  368. * An action that fires just before the options backup, use to add in dynamically detected options 
  369. *  
  370. * @param array $opts the options array, passed in by reference 
  371. * @return null 
  372. */ 
  373. function opts_update_prebk(&$opts) 
  374. //Just a prototype function 
  375. /** 
  376. * Updates the database settings from the webform 
  377. */ 
  378. function opts_update() 
  379. //Do some security related thigns as we are not using the normal WP settings API 
  380. $this->security(); 
  381. //Do a nonce check, prevent malicious link/form problems 
  382. check_admin_referer($this->unique_prefix . '_options-options'); 
  383. //Update local options from database 
  384. $this->opt = $this::parse_args($this->get_option($this->unique_prefix . '_options'), $this->opt); 
  385. $this->opts_update_prebk($this->opt); 
  386. //Update our backup options 
  387. $this->update_option($this->unique_prefix . '_options_bk', $this->opt); 
  388. $opt_prev = $this->opt; 
  389. //Grab our incomming array (the data is dirty) 
  390. $input = $_POST[$this->unique_prefix . '_options']; 
  391. //Run the update loop 
  392. $this->opts_update_loop($this->opt, $input); 
  393. //Commit the option changes 
  394. $updated = $this->update_option($this->unique_prefix . '_options', $this->opt); 
  395. //Check if known settings match attempted save 
  396. if($updated && count(array_diff_key($input, $this->opt)) == 0) 
  397. //Let the user know everything went ok 
  398. $this->message['updated fade'][] = __('Settings successfully saved.', $this->identifier) . $this->admin_anchor('undo', __('Undo the options save.', $this->identifier), __('Undo', $this->identifier)); 
  399. else if(!$updated && count(array_diff_key($opt_prev, $this->opt)) == 0) 
  400. $this->message['updated fade'][] = __('Settings did not change, nothing to save.', $this->identifier); 
  401. else if(!$updated) 
  402. $this->message['error fade'][] = __('Settings were not saved.', $this->identifier); 
  403. else 
  404. //Let the user know the following were not saved 
  405. $this->message['updated fade'][] = __('Some settings were not saved.', $this->identifier) . $this->admin_anchor('undo', __('Undo the options save.', $this->identifier), __('Undo', $this->identifier)); 
  406. $temp = __('The following settings were not saved:', $this->identifier); 
  407. foreach(array_diff_key($input, $this->opt) as $setting => $value) 
  408. $temp .= '<br />' . $setting; 
  409. $this->message['updated fade'][] = $temp . '<br />' . sprintf(__('Please include this message in your %sbug report%s.', $this->identifier), '<a title="' . sprintf(__('Go to the %s support post for your version.', $this->identifier), $this->short_name) . '" href="' . $this->support_url . $this::version . '/#respond">', '</a>'); 
  410. add_action('admin_notices', array($this, 'messages')); 
  411. /** 
  412. * Exports a XML options document 
  413. */ 
  414. function opts_export() 
  415. //Do a nonce check, prevent malicious link/form problems  
  416. check_admin_referer($this->unique_prefix . '_admin_import_export'); 
  417. //Update our internal settings 
  418. $this->opt = $this->get_option($this->unique_prefix . '_options'); 
  419. //Create a DOM document 
  420. $dom = new DOMDocument('1.0', 'UTF-8'); 
  421. //Adds in newlines and tabs to the output 
  422. $dom->formatOutput = true; 
  423. //We're not using a DTD therefore we need to specify it as a standalone document 
  424. $dom->xmlStandalone = true; 
  425. //Add an element called options 
  426. $node = $dom->createElement('options'); 
  427. $parnode = $dom->appendChild($node); 
  428. //Add a child element named plugin 
  429. $node = $dom->createElement('plugin'); 
  430. $plugnode = $parnode->appendChild($node); 
  431. //Add some attributes that identify the plugin and version for the options export 
  432. $plugnode->setAttribute('name', $this->short_name); 
  433. $plugnode->setAttribute('version', $this::version); 
  434. //Change our headder to text/xml for direct save 
  435. header('Cache-Control: public'); 
  436. //The next two will cause good browsers to download instead of displaying the file 
  437. header('Content-Description: File Transfer'); 
  438. header('Content-disposition: attachemnt; filename=' . $this->unique_prefix . '_settings.xml'); 
  439. header('Content-Type: text/xml'); 
  440. //Loop through the options array 
  441. foreach($this->opt as $key=>$option) 
  442. //Add a option tag under the options tag, store the option value 
  443. $node = $dom->createElement('option', htmlentities($option, ENT_COMPAT, 'UTF-8')); 
  444. $newnode = $plugnode->appendChild($node); 
  445. //Change the tag's name to that of the stored option 
  446. $newnode->setAttribute('name', $key); 
  447. //Prepair the XML for output 
  448. $output = $dom->saveXML(); 
  449. //Let the browser know how long the file is 
  450. header('Content-Length: ' . strlen($output)); // binary length 
  451. //Output the file 
  452. echo $output; 
  453. //Prevent WordPress from continuing on 
  454. die(); 
  455. /** 
  456. * Imports a XML options document 
  457. */ 
  458. function opts_import() 
  459. //Our quick and dirty error supressor 
  460. function error($errno, $errstr, $eerfile, $errline) 
  461. return true; 
  462. //Do a nonce check, prevent malicious link/form problems 
  463. check_admin_referer($this->unique_prefix . '_admin_import_export'); 
  464. //Set the backup options in the DB to the current options 
  465. $this->opts_backup(); 
  466. //Create a DOM document 
  467. $dom = new DOMDocument('1.0', 'UTF-8'); 
  468. //We want to catch errors ourselves 
  469. set_error_handler('error'); 
  470. //Load the user uploaded file, handle failure gracefully 
  471. if($dom->load($_FILES[$this->unique_prefix . '_admin_import_file']['tmp_name'])) 
  472. $opts_temp = array(); 
  473. $version = ''; 
  474. //Have to use an xpath query otherwise we run into problems 
  475. $xpath = new DOMXPath($dom);  
  476. $option_sets = $xpath->query('plugin'); 
  477. //Loop through all of the xpath query results 
  478. foreach($option_sets as $options) 
  479. //We only want to import options for only this plugin 
  480. if($options->getAttribute('name') === $this->short_name) 
  481. //Grab the file version 
  482. $version = $options->getAttribute('version'); 
  483. //Loop around all of the options 
  484. foreach($options->getelementsByTagName('option') as $child) 
  485. //Place the option into the option array, DOMDocument decodes html entities for us 
  486. $opts_temp[$child->getAttribute('name')] = $child->nodeValue; 
  487. //Make sure we safely import and upgrade settings if needed 
  488. $this->opts_upgrade($opts_temp, $version); 
  489. //Commit the loaded options to the database 
  490. $this->update_option($this->unique_prefix . '_options', $this->opt); 
  491. //Everything was successful, let the user know 
  492. $this->message['updated fade'][] = __('Settings successfully imported from the uploaded file.', $this->identifier) . $this->admin_anchor('undo', __('Undo the options import.', $this->identifier), __('Undo', $this->identifier)); 
  493. else 
  494. //Throw an error since we could not load the file for various reasons 
  495. $this->message['error'][] = __('Importing settings from file failed.', $this->identifier); 
  496. //Reset to the default error handler after we're done 
  497. restore_error_handler(); 
  498. //Output any messages that there may be 
  499. add_action('admin_notices', array($this, 'messages')); 
  500. /** 
  501. * Resets the database settings array to the default set in opt 
  502. */ 
  503. function opts_reset() 
  504. //Do a nonce check, prevent malicious link/form problems 
  505. check_admin_referer($this->unique_prefix . '_admin_import_export'); 
  506. //Set the backup options in the DB to the current options 
  507. $this->opts_backup(); 
  508. //Load in the hard coded default option values 
  509. $this->update_option($this->unique_prefix . '_options', $this->opt); 
  510. //Reset successful, let the user know 
  511. $this->message['updated fade'][] = __('Settings successfully reset to the default values.', $this->identifier) . $this->admin_anchor('undo', __('Undo the options reset.', $this->identifier), __('Undo', $this->identifier)); 
  512. add_action('admin_notices', array($this, 'messages')); 
  513. /** 
  514. * Undos the last settings save/reset/import 
  515. */ 
  516. function opts_undo() 
  517. //Do a nonce check, prevent malicious link/form problems 
  518. check_admin_referer($this->unique_prefix . '_admin_undo'); 
  519. //Set the options array to the current options 
  520. $opt = $this->get_option($this->unique_prefix . '_options'); 
  521. //Set the options in the DB to the backup options 
  522. $this->update_option($this->unique_prefix . '_options', $this->get_option($this->unique_prefix . '_options_bk')); 
  523. //Set the backup options to the undone options 
  524. $this->update_option($this->unique_prefix . '_options_bk', $opt); 
  525. //Send the success/undo message 
  526. $this->message['updated fade'][] = __('Settings successfully undid the last operation.', $this->identifier) . $this->admin_anchor('undo', __('Undo the last undo operation.', $this->identifier), __('Undo', $this->identifier)); 
  527. add_action('admin_notices', array($this, 'messages')); 
  528. /** 
  529. * Upgrades input options array, sets to $this->opt, designed to be overwritten 
  530. *  
  531. * @param array $opts 
  532. * @param string $version the version of the passed in options 
  533. */ 
  534. function opts_upgrade($opts, $version) 
  535. //We don't support using newer versioned option files in older releases 
  536. if(version_compare($this::version, $version, '>=')) 
  537. $this->opt = $opts; 
  538. /** 
  539. * Forces a database settings upgrade 
  540. */ 
  541. function opts_upgrade_wrapper() 
  542. //Do a nonce check, prevent malicious link/form problems 
  543. check_admin_referer($this->unique_prefix . '_admin_upgrade'); 
  544. //Grab the database options 
  545. $opts = $this->get_option($this->unique_prefix . '_options'); 
  546. if(is_array($opts)) 
  547. //Feed the just read options into the upgrade function 
  548. $this->opts_upgrade($opts, $this->get_option($this->unique_prefix . '_version')); 
  549. //Always have to update the version 
  550. $this->update_option($this->unique_prefix . '_version', $this::version); 
  551. //Store the options 
  552. $this->update_option($this->unique_prefix . '_options', $this->opt); 
  553. //Send the success message 
  554. $this->message['updated fade'][] = __('Settings successfully migrated.', $this->identifier); 
  555. else 
  556. //Run the install script 
  557. $this->install(); 
  558. //Send the success message 
  559. $this->message['updated fade'][] = __('Default settings successfully installed.', $this->identifier); 
  560. add_action('admin_notices', array($this, 'messages')); 
  561. /** 
  562. * help action hook function, meant to be overridden 
  563. *  
  564. * @return string 
  565. *  
  566. */ 
  567. function help() 
  568. $screen = get_current_screen(); 
  569. //Add contextual help on current screen 
  570. if($screen->id == 'settings_page_' . $this->identifier) 
  571.  
  572. /** 
  573. * Prints to screen all of the messages stored in the message member variable 
  574. */ 
  575. function messages() 
  576. if(count($this->message)) 
  577. //Loop through our message classes 
  578. foreach($this->message as $key => $class) 
  579. //Loop through the messages in the current class 
  580. foreach($class as $message) 
  581. printf('<div class="%s"><p>%s</p></div>', $key, $message);  
  582. $this->message = array(); 
  583. /** 
  584. * Function prototype to prevent errors 
  585. */ 
  586. function admin_styles() 
  587.  
  588. /** 
  589. * Function prototype to prevent errors 
  590. */ 
  591. function admin_scripts() 
  592.  
  593. /** 
  594. * Function prototype to prevent errors 
  595. */ 
  596. function admin_head() 
  597.  
  598. /** 
  599. * Function prototype to prevent errors 
  600. */ 
  601. function admin_page() 
  602.  
  603. /** 
  604. * Function prototype to prevent errors 
  605. */ 
  606. protected function _get_help_text() 
  607.  
  608. /** 
  609. * Returns a valid xHTML element ID 
  610. *  
  611. * @param object $option 
  612. * @return  
  613. */ 
  614. function get_valid_id($option) 
  615. if(is_numeric($option[0])) 
  616. return 'p' . $option; 
  617. else 
  618. return $option; 
  619. function import_form() 
  620. $form = '<div id="mtekk_admin_import_export_relocate">'; 
  621. $form .= sprintf('<form action="options-general.php?page=%s" method="post" enctype="multipart/form-data" id="%s_admin_upload">', $this->identifier, $this->unique_prefix); 
  622. $form .= wp_nonce_field($this->unique_prefix . '_admin_import_export', '_wpnonce', true, false); 
  623. $form .= sprintf('<fieldset id="import_export" class="%s_options">', $this->unique_prefix); 
  624. $form .= '<p>' . __('Import settings from a XML file, export the current settings to a XML file, or reset to the default settings.', $this->identifier) . '</p>'; 
  625. $form .= '<table class="form-table"><tr valign="top"><th scope="row">'; 
  626. $form .= sprintf('<label for="%s_admin_import_file">', $this->unique_prefix); 
  627. $form .= __('Settings File', $this->identifier); 
  628. $form .= '</label></th><td>'; 
  629. $form .= sprintf('<input type="file" name="%s_admin_import_file" id="%s_admin_import_file" size="32" /><p class="description">', $this->unique_prefix, $this->unique_prefix); 
  630. $form .= __('Select a XML settings file to upload and import settings from.', 'breadcrumb_navxt'); 
  631. $form .= '</p></td></tr></table><p class="submit">'; 
  632. $form .= sprintf('<input type="submit" class="button" name="%s_admin_import" value="' . __('Import', $this->identifier) . '"/>', $this->unique_prefix, $this->unique_prefix); 
  633. $form .= sprintf('<input type="submit" class="button" name="%s_admin_export" value="' . __('Export', $this->identifier) . '"/>', $this->unique_prefix); 
  634. $form .= sprintf('<input type="submit" class="button" name="%s_admin_reset" value="' . __('Reset', $this->identifier) . '"/>', $this->unique_prefix, $this->unique_prefix); 
  635. $form .= '</p></fieldset></form></div>'; 
  636. return $form; 
  637. /** 
  638. * This will output a well formed hidden option 
  639. *  
  640. * @param string $option 
  641. * @return  
  642. */ 
  643. function input_hidden($option) 
  644. $optid = $this->get_valid_id($option);?> 
  645. <input type="hidden" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" id="<?php echo $optid;?>" value="<?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?>"/> 
  646. <?php 
  647. /** 
  648. * This will output a well formed table row for a text input 
  649. *  
  650. * @param string $label 
  651. * @param string $option 
  652. * @param string $class (optional) 
  653. * @param bool $disable (optional) 
  654. * @param string $description (optional) 
  655. * @return  
  656. */ 
  657. function input_text($label, $option, $class = 'regular-text', $disable = false, $description = '') 
  658. $optid = $this->get_valid_id($option); 
  659. if($disable) 
  660. {?> 
  661. <input type="hidden" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" value="<?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?>" /> 
  662. <?php } ?> 
  663. <tr valign="top"> 
  664. <th scope="row"> 
  665. <label for="<?php echo $optid;?>"><?php echo $label;?></label> 
  666. </th> 
  667. <td> 
  668. <input type="text" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" id="<?php echo $optid;?>" <?php if($disable) {echo 'disabled="disabled"'; $class .= ' disabled';}?> value="<?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?>" class="<?php echo $class;?>" /><br /> 
  669. <?php if($description !== '') {?><p class="description"><?php echo $description;?></p><?php }?> 
  670. </td> 
  671. </tr> 
  672. <?php 
  673. /** 
  674. * This will output a well formed table row for a HTML5 number input 
  675. *  
  676. * @param string $label 
  677. * @param string $option 
  678. * @param string $class (optional) 
  679. * @param bool $disable (optional) 
  680. * @param string $description (optional) 
  681. * @param int|string $min (optional)  
  682. * @param int|string $max (optional) 
  683. * @param int|string $step (optional) 
  684. * @return  
  685. */ 
  686. function input_number($label, $option, $class = 'small-text', $disable = false, $description = '', $min = '', $max = '', $step = '') 
  687. $optid = $this->get_valid_id($option); 
  688. $extras = ''; 
  689. if($min !== '') 
  690. $extras .= 'min="' . $min . '" '; 
  691. if($max !== '') 
  692. $extras .= 'max="' . $max . '" '; 
  693. if($step !== '') 
  694. $extras .= 'step="' . $step . '" '; 
  695. if($disable) 
  696. {?> 
  697. <input type="hidden" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" value="<?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?>" /> 
  698. <?php } ?> 
  699. <tr valign="top"> 
  700. <th scope="row"> 
  701. <label for="<?php echo $optid;?>"><?php echo $label;?></label> 
  702. </th> 
  703. <td> 
  704. <input type="number" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" id="<?php echo $optid;?>" <?php echo $extras;?><?php if($disable) {echo 'disabled="disabled"'; $class .= ' disabled';}?> value="<?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?>" class="<?php echo $class;?>" /><br /> 
  705. <?php if($description !== '') {?><p class="description"><?php echo $description;?></p><?php }?> 
  706. </td> 
  707. </tr> 
  708. <?php 
  709. /** 
  710. * This will output a well formed textbox 
  711. *  
  712. * @param string $label 
  713. * @param string $option 
  714. * @param string $rows (optional) 
  715. * @param bool $disable (optional) 
  716. * @param string $description (optional) 
  717. */ 
  718. function textbox($label, $option, $height = '3', $disable = false, $description = '') 
  719. $optid = $this->get_valid_id($option);?> 
  720. <p> 
  721. <label for="<?php echo $optid;?>"><?php echo $label;?></label> 
  722. </p> 
  723. <textarea rows="<?php echo $height;?>" <?php if($disable) {echo 'disabled="disabled" class="large-text code disabled"';}else{echo 'class="large-text code"';}?> id="<?php echo $optid;?>" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]"><?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?></textarea><br /> 
  724. <?php if($description !== '') {?><p class="description"><?php echo $description;?></p><?php } 
  725. /** 
  726. * This will output a well formed tiny mce ready textbox 
  727. *  
  728. * @param string $label 
  729. * @param string $option 
  730. * @param string $rows (optional) 
  731. * @param bool $disable (optional) 
  732. * @param string $description (optional) 
  733. */ 
  734. function tinymce($label, $option, $height = '3', $disable = false, $description = '') 
  735. $optid = $this->get_valid_id($option); 
  736. if($disable) 
  737. {?> 
  738. <input type="hidden" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" value="<?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?>" /> 
  739. <?php } ?> 
  740. <tr valign="top"> 
  741. <th scope="row"> 
  742. <label for="<?php echo $optid;?>"><?php echo $label;?></label> 
  743. </th> 
  744. <td> 
  745. <textarea rows="<?php echo $height;?>" <?php if($disable) {echo 'disabled="disabled" class="mtekk_mce disabled"';}else{echo 'class="mtekk_mce"';}?> id="<?php echo $optid;?>" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]"><?php echo htmlentities($this->opt[$option], ENT_COMPAT, 'UTF-8');?></textarea><br /> 
  746. <?php if($description !== '') {?><p class="description"><?php echo $description;?></p><?php }?> 
  747. </td> 
  748. </tr> 
  749. <?php 
  750. /** 
  751. * This will output a well formed table row for a checkbox input 
  752. *  
  753. * @param string $label 
  754. * @param string $option 
  755. * @param string $instruction 
  756. * @param bool $disable (optional) 
  757. * @param string $description (optional) 
  758. * @param string $class (optional) 
  759. * @return  
  760. */ 
  761. function input_check($label, $option, $instruction, $disable = false, $description = '', $class = '') 
  762. $optid = $this->get_valid_id($option);?> 
  763. <tr valign="top"> 
  764. <th scope="row"> 
  765. <label for="<?php echo $optid;?>"><?php echo $label;?></label> 
  766. </th> 
  767. <td>  
  768. <label> 
  769. <input type="checkbox" name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" id="<?php echo $optid;?>" <?php if($disable) {echo 'disabled="disabled" class="disabled ' . $class . '"';} else{echo 'class="' . $class . '"';}?> value="true" <?php checked(true, $this->opt[$option]);?> /> 
  770. <?php echo $instruction; ?>  
  771. </label><br /> 
  772. <?php if($description !== '') {?><p class="description"><?php echo $description;?></p><?php }?> 
  773. </td> 
  774. </tr> 
  775. <?php 
  776. /** 
  777. * This will output a singular radio type form input field 
  778. *  
  779. * @param string $option 
  780. * @param string $value 
  781. * @param string $instruction 
  782. * @param object $disable (optional) 
  783. * @param string $class (optional) 
  784. * @return  
  785. */ 
  786. function input_radio($option, $value, $instruction, $disable = false, $class = '') 
  787. {?> 
  788. <label> 
  789. <input name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" type="radio" <?php if($disable) {echo 'disabled="disabled" class="disabled togx ' . $class . '"';}else{echo 'class="togx ' . $class . '"';}?> value="<?php echo $value;?>" <?php checked($value, $this->opt[$option]);?> /> 
  790. <?php echo $instruction; ?> 
  791. </label><br/> 
  792. <?php 
  793. /** 
  794. * This will output a well formed table row for a select input 
  795. *  
  796. * @param string $label 
  797. * @param string $option 
  798. * @param array $values 
  799. * @param bool $disable (optional) 
  800. * @param string $description (optional) 
  801. * @param array $titles (optional) The array of titiles for the options, if they should be different from the values 
  802. * @param string $class (optional) Extra class to apply to the elements 
  803. * @return  
  804. */ 
  805. function input_select($label, $option, $values, $disable = false, $description = '', $titles = false, $class = '') 
  806. //If we don't have titles passed in, we'll use option names as values 
  807. if(!$titles) 
  808. $titles = $values; 
  809. $optid = $this->get_valid_id($option);?> 
  810. <tr valign="top"> 
  811. <th scope="row"> 
  812. <label for="<?php echo $optid;?>"><?php echo $label;?></label> 
  813. </th> 
  814. <td> 
  815. <select name="<?php echo $this->unique_prefix . '_options[' . $option;?>]" id="<?php echo $optid;?>" <?php if($disable) {echo 'disabled="disabled" class="disabled ' . $class . '"';} else{echo 'class="' . $class . '"';}?>> 
  816. <?php $this->select_options($option, $titles, $values); ?> 
  817. </select><br /> 
  818. <?php if($description !== '') {?><p class="description"><?php echo $description;?></p><?php }?> 
  819. </td> 
  820. </tr> 
  821. <?php 
  822. /** 
  823. * Displays wordpress options as <seclect> 
  824. * @param string $optionname name of wordpress options store 
  825. * @param array $options array of names of options that can be selected 
  826. * @param array $values array of the values of the options that can be selected 
  827. * @param array $exclude(optional) array of names in $options array to be excluded 
  828. */ 
  829. function select_options($optionname, $options, $values, $exclude = array()) 
  830. $value = $this->opt[$optionname]; 
  831. //Now do the rest 
  832. foreach($options as $key => $option) 
  833. if(!in_array($option, $exclude)) 
  834. printf('<option value="%s" %s>%s</option>', $values[$key], selected(true, ($value == $values[$key]), false), $option); 
  835. /** 
  836. * A local pass through for get_option so that we can hook in and pick the correct method if needed 
  837. *  
  838. * @param string $option The name of the option to retrieve 
  839. * @return mixed The value of the option 
  840. */ 
  841. function get_option($option) 
  842. return get_option($option); 
  843. /** 
  844. * A local pass through for update_option so that we can hook in and pick the correct method if needed 
  845. *  
  846. * @param string $option The name of the option to update 
  847. * @param mixed $newvalue The new value to set the option to 
  848. *  
  849. */ 
  850. function update_option($option, $newvalue) 
  851. return update_option($option, $newvalue); 
  852. /** 
  853. * A local pass through for add_option so that we can hook in and pick the correct method if needed 
  854. *  
  855. * @param string $option The name of the option to update 
  856. * @param mixed $value The new value to set the option to 
  857. * @param null $deprecated Deprecated parameter 
  858. * @param string $autoload Whether or not to autoload the option, it's a string because WP is special 
  859. *  
  860. */ 
  861. function add_option($option, $value = '', $deprecated = '', $autoload = 'yes') 
  862. return add_option($option, $value, null, $autoload); 
  863. /** 
  864. * A local pass through for delete_option so that we can hook in and pick the correct method if needed 
  865. *  
  866. * @param string $option The name of the option to delete 
  867. */ 
  868. function delete_option($option) 
  869. return delete_option($option);