PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/simple-tags/inc/class.admin.php

https://bitbucket.org/Wallynm/iptb
PHP | 565 lines | 351 code | 78 blank | 136 comment | 86 complexity | 13b635b2df2d44e9bd15c8da0ef4db26 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.0
  1. <?php
  2. class SimpleTags_Admin {
  3. // CPT and Taxonomy support
  4. var $post_type = 'post';
  5. var $post_type_name = '';
  6. var $taxonomy = '';
  7. var $taxo_name = '';
  8. // Error management
  9. var $message = '';
  10. var $status = '';
  11. /**
  12. * PHP4 Constructor - Initialize Admin
  13. *
  14. * @return void
  15. * @author Amaury Balmer
  16. */
  17. function SimpleTags_Admin() {
  18. global $simple_tags;
  19. // DB Upgrade ?
  20. $this->upgrade();
  21. // Get options
  22. $options = get_option( STAGS_OPTIONS_NAME );
  23. // Which taxo ?
  24. $this->registerDetermineTaxonomy();
  25. // Admin menu
  26. add_action('admin_menu', array(&$this, 'adminMenu'));
  27. add_action('admin_notices', array(&$this, 'displayMessage'));
  28. // Load JavaScript and CSS
  29. add_action('admin_enqueue_scripts', array(&$this, 'initJavaScript'));
  30. // Load custom part of plugin depending option
  31. if ( isset($options['use_suggested_tags']) && $options['use_suggested_tags'] == 1 ) {
  32. require( STAGS_DIR . '/inc/class.admin.suggest.php');
  33. $simple_tags['admin-suggest'] = new SimpleTags_Admin_Suggest();
  34. }
  35. if ( isset($options['use_click_tags']) && $options['use_click_tags'] == 1 ) {
  36. require( STAGS_DIR . '/inc/class.admin.clickterms.php');
  37. $simple_tags['admin-clicktags'] = new SimpleTags_Admin_ClickTags();
  38. }
  39. if ( isset($options['use_autocompletion']) && $options['use_autocompletion'] == 1 ) {
  40. require( STAGS_DIR . '/inc/class.admin.autocomplete.php');
  41. $simple_tags['admin-autocomplete'] = new SimpleTags_Admin_Autocomplete();
  42. }
  43. if ( isset($options['active_mass_edit']) && $options['active_mass_edit'] == 1 ) {
  44. require( STAGS_DIR . '/inc/class.admin.mass.php');
  45. $simple_tags['admin-mass'] = new SimpleTags_Admin_Mass();
  46. }
  47. if ( isset($options['active_manage']) && $options['active_manage'] == 1 ) {
  48. require( STAGS_DIR . '/inc/class.admin.manage.php');
  49. $simple_tags['admin-manage'] = new SimpleTags_Admin_Manage();
  50. }
  51. if ( isset($options['active_autotags']) && $options['active_autotags'] == 1 ) {
  52. require( STAGS_DIR . '/inc/class.admin.autoterms.php');
  53. $simple_tags['admin-autotags'] = new SimpleTags_Admin_AutoTags();
  54. }
  55. if ( (isset($options['active_autotags']) && $options['active_autotags'] == 1) || (isset($options['auto_link_tags']) && $options['auto_link_tags'] == '1') ) {
  56. require( STAGS_DIR . '/inc/class.admin.post.php');
  57. $simple_tags['admin-post_settings'] = new SimpleTags_Admin_Post_Settings();
  58. }
  59. }
  60. /**
  61. * Init taxonomy class variable, load this action after all actions on init !
  62. * Make a function for call it from children class...
  63. *
  64. * @return void
  65. * @author Amaury Balmer
  66. */
  67. function registerDetermineTaxonomy() {
  68. add_action( 'init', array(&$this, 'determineTaxonomy'), 99999999 );
  69. }
  70. /**
  71. * Put in var class the current taxonomy choose by the user
  72. *
  73. * @return void
  74. * @author Amaury Balmer
  75. */
  76. function determineTaxonomy() {
  77. $this->taxo_name = __('Post tags', 'simpletags');
  78. $this->post_type_name = __('Posts', 'simpletags');
  79. // Custom CPT ?
  80. if ( isset($_GET['cpt']) && !empty($_GET['cpt']) && post_type_exists($_GET['cpt']) ) {
  81. $cpt = get_post_type_object($_GET['cpt']);
  82. $this->post_type = $cpt->name;
  83. $this->post_type_name = $cpt->labels->name;
  84. }
  85. // Get compatible taxo for current post type
  86. $compatible_taxonomies = get_object_taxonomies( $this->post_type );
  87. // Custom taxo ?
  88. if ( isset($_GET['taxo']) && !empty($_GET['taxo']) && taxonomy_exists($_GET['taxo']) ) {
  89. $taxo = get_taxonomy($_GET['taxo']);
  90. // Taxo is compatible ?
  91. if ( in_array( $taxo->name, $compatible_taxonomies ) ) {
  92. $this->taxonomy = $taxo->name;
  93. $this->taxo_name = $taxo->labels->name;
  94. } else {
  95. unset($taxo);
  96. }
  97. }
  98. // Default taxo from CPT...
  99. if ( !isset($taxo) && is_array($compatible_taxonomies) && !empty($compatible_taxonomies) ) {
  100. // Take post_tag before category
  101. if ( in_array('post_tag', $compatible_taxonomies) ) {
  102. $taxo = get_taxonomy( 'post_tag' );
  103. } else {
  104. $taxo = get_taxonomy( current($compatible_taxonomies) );
  105. }
  106. $this->taxonomy = $taxo->name;
  107. $this->taxo_name = $taxo->labels->name;
  108. // TODO: Redirect for help user that see the URL...
  109. } elseif( !isset($taxo) ) {
  110. wp_die( __('This custom post type not have taxonomies.', 'simpletags') );
  111. }
  112. // Free memory
  113. unset($cpt, $taxo);
  114. }
  115. /**
  116. * Build HTML form for allow user to change taxonomy for the current page.
  117. *
  118. * @param string $page_value
  119. * @return void
  120. * @author Amaury Balmer
  121. */
  122. function boxSelectorTaxonomy( $page_value = '' ) {
  123. echo '<div class="box-selector-taxonomy">' . "\n";
  124. echo '<p class="current-taxonomy">'.sprintf(__('You currently use the custom post type "<span>%s</span>" and the taxonomy "<span>%s</span>"', 'simpletags'), $this->post_type_name, $this->taxo_name).'</p>' . "\n";
  125. echo '<div class="change-taxo">' . "\n";
  126. echo '<form action="" method="get">' . "\n";
  127. if ( !empty($page_value) ) {
  128. echo '<input type="hidden" name="page" value="'.$page_value.'" />' . "\n";
  129. }
  130. echo '<select name="cpt" id="cpt-select">' . "\n";
  131. foreach ( get_post_types( array('show_ui' => true ), 'objects') as $post_type ) {
  132. echo '<option '.selected( $post_type->name, $this->post_type, false ).' value="'.esc_attr($post_type->name).'">'.esc_html($post_type->labels->name).'</option>' . "\n";
  133. }
  134. echo '</select>' . "\n";
  135. echo '<select name="taxo" id="taxonomy-select">' . "\n";
  136. foreach ( get_object_taxonomies($this->post_type) as $tax_name ) {
  137. $taxonomy = get_taxonomy($tax_name);
  138. if ( $taxonomy->show_ui == false )
  139. continue;
  140. echo '<option '.selected( $tax_name, $this->taxonomy, false ).' value="'.esc_attr($tax_name).'">'.esc_html($taxonomy->labels->name).'</option>' . "\n";
  141. }
  142. echo '</select>' . "\n";
  143. echo '<input type="submit" class="button" id="submit-change-taxo" value="'.__('Change selection', 'simpletags').'" />' . "\n";
  144. echo '</form>' . "\n";
  145. echo '</div>' . "\n";
  146. echo '</div>' . "\n";
  147. }
  148. /**
  149. * Init somes JS and CSS need for simple tags.
  150. *
  151. * @return void
  152. * @author Amaury Balmer
  153. */
  154. function initJavaScript() {
  155. global $pagenow;
  156. // Library JS
  157. wp_register_script('jquery-cookie', STAGS_URL.'/ressources/jquery.cookie.min.js', array('jquery'), '1.0.0');
  158. // Helper simple tags
  159. wp_register_script('st-helper-add-tags', STAGS_URL.'/inc/js/helper-add-tags.min.js', array('jquery'), STAGS_VERSION);
  160. wp_register_script('st-helper-options', STAGS_URL.'/inc/js/helper-options.min.js', array('jquery'), STAGS_VERSION);
  161. // Register CSS
  162. wp_register_style('st-admin', STAGS_URL.'/inc/css/admin.css', array(), STAGS_VERSION, 'all' );
  163. // Register location
  164. $wp_post_pages = array('post.php', 'post-new.php');
  165. $wp_page_pages = array('page.php', 'page-new.php');
  166. // Common Helper for Post, Page and Plugin Page
  167. if (
  168. in_array($pagenow, $wp_post_pages) ||
  169. ( in_array($pagenow, $wp_page_pages) && is_page_have_tags() ) ||
  170. ( isset($_GET['page']) && in_array($_GET['page'], array('st_mass_terms', 'st_auto', 'st_options', 'st_manage')) )
  171. ) {
  172. wp_enqueue_style ('st-admin');
  173. }
  174. // add jQuery tabs for options page. Use jQuery UI Tabs from WP
  175. if ( isset($_GET['page']) && $_GET['page'] == 'st_options' ) {
  176. wp_enqueue_script('jquery-ui-tabs');
  177. wp_enqueue_script('jquery-cookie');
  178. wp_enqueue_script('st-helper-options');
  179. }
  180. }
  181. /**
  182. * Add settings page on WordPress admin menu
  183. *
  184. * @return void
  185. * @author Amaury Balmer
  186. */
  187. function adminMenu() {
  188. add_options_page( __('Simple Tags: Options', 'simpletags'), __('Simple Tags', 'simpletags'), 'admin_simple_tags', 'st_options', array(&$this, 'pageOptions'));
  189. }
  190. /**
  191. * Build HTML for page options, manage also save/reset settings
  192. *
  193. * @return void
  194. * @author Amaury Balmer
  195. */
  196. function pageOptions() {
  197. // Get default & current options and merge
  198. $default_options = (array) include( dirname(__FILE__) . '/helper.options.default.php' );
  199. $options = (array) get_option( STAGS_OPTIONS_NAME );
  200. $options = array_merge( $default_options, $options );
  201. // Update or reset options
  202. if ( isset($_POST['updateoptions']) ) {
  203. check_admin_referer('updateresetoptions-simpletags');
  204. foreach( (array) $options as $key => $value) {
  205. $newval = ( isset($_POST[$key]) ) ? stripslashes($_POST[$key]) : '0';
  206. if ( $newval != $value ) {
  207. $options[$key] = $newval;
  208. }
  209. }
  210. update_option( STAGS_OPTIONS_NAME, $options );
  211. $this->message = __('Options saved', 'simpletags');
  212. $this->status = 'updated';
  213. } elseif ( isset($_POST['reset_options']) ) {
  214. check_admin_referer('updateresetoptions-simpletags');
  215. $options = (array) include( dirname(__FILE__) . '/helper.options.default.php' );
  216. update_option( STAGS_OPTIONS_NAME, $options );
  217. $this->message = __('Simple Tags options resetted to default options!', 'simpletags');
  218. }
  219. $this->displayMessage();
  220. ?>
  221. <div class="wrap st_wrap">
  222. <h2><?php _e('Simple Tags: Options', 'simpletags'); ?></h2>
  223. <div style="float:right">
  224. <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  225. <input type="hidden" name="cmd" value="_s-xclick">
  226. <input type="hidden" name="hosted_button_id" value="L9QU9QT9R5FQS">
  227. <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG_global.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online.">
  228. <img alt="" border="0" src="https://www.paypal.com/fr_FR/i/scr/pixel.gif" width="1" height="1" />
  229. </form>
  230. </div>
  231. <p><?php _e('Visit the <a href="http://redmine.beapi.fr/projects/show/simple-tags/">plugin\'s homepage</a> for further details. If you find a bug, or have a fantastic idea for this plugin, <a href="mailto:amaury@wordpress-fr.net">ask me</a> !', 'simpletags'); ?></p>
  232. <form action="" method="post">
  233. <p>
  234. <input class="button-primary" type="submit" name="updateoptions" value="<?php _e('Update options &raquo;', 'simpletags'); ?>" />
  235. <input class="button" type="submit" name="reset_options" onclick="return confirm('<?php _e('Do you really want to restore the default options?', 'simpletags'); ?>');" value="<?php _e('Reset Options', 'simpletags'); ?>" /></p>
  236. <div id="printOptions">
  237. <ul class="st_submenu">
  238. <?php
  239. // Get array options/description
  240. $option_data = (array) include( dirname(__FILE__) . '/helper.options.admin.php' );
  241. foreach ( $option_data as $key => $val ) {
  242. $style = '';
  243. // Deactive tabs if feature not actived
  244. if ( isset($options['active_related_posts']) && (int) $options['active_related_posts'] == 0 && $key == 'relatedposts' )
  245. $style = 'display:none;';
  246. // Deactive tabs if feature not actived
  247. if ( isset($options['auto_link_tags']) && (int) $options['auto_link_tags'] == 0 && $key == 'auto-links' )
  248. $style = 'display:none;';
  249. echo '<li style="'.$style.'"><a href="#'. sanitize_title ( $key ) .'">'.$this->getNiceTitleOptions($key).'</a></li>';
  250. }
  251. ?>
  252. </ul>
  253. <div class="clear"></div>
  254. <?php echo $this->printOptions( $option_data ); ?>
  255. </div>
  256. <p>
  257. <?php wp_nonce_field( 'updateresetoptions-simpletags' ); ?>
  258. <input class="button-primary" type="submit" name="updateoptions" value="<?php _e('Update options &raquo;', 'simpletags'); ?>" />
  259. <input class="button" type="submit" name="reset_options" onclick="return confirm('<?php _e('Do you really want to restore the default options?', 'simpletags'); ?>');" value="<?php _e('Reset Options', 'simpletags'); ?>" />
  260. </p>
  261. </form>
  262. <?php $this->printAdminFooter(); ?>
  263. </div>
  264. <?php
  265. }
  266. /**
  267. * Get terms for a post, format terms for input and autocomplete usage
  268. *
  269. * @param string $taxonomy
  270. * @param integer $post_id
  271. * @return string
  272. * @author Amaury Balmer
  273. */
  274. function getTermsToEdit( $taxonomy = 'post_tag', $post_id = 0 ) {
  275. $post_id = (int) $post_id;
  276. if ( !$post_id )
  277. return false;
  278. $terms = wp_get_post_terms( $post_id, $taxonomy, array('fields' => 'names') );
  279. if ( $terms == false )
  280. return false;
  281. $terms = array_unique( $terms ); // Remove duplicate
  282. $terms = join( ', ', $terms );
  283. $terms = esc_attr( $terms );
  284. $terms = apply_filters( 'tags_to_edit', $terms );
  285. return $terms;
  286. }
  287. /**
  288. * Default content for meta box of Simple Tags
  289. *
  290. * @return string
  291. * @author Amaury Balmer
  292. */
  293. function getDefaultContentBox() {
  294. if ( (int) wp_count_terms('post_tag', 'ignore_empty=false') == 0 ) { // TODO: Custom taxonomy
  295. return __('This feature requires at least 1 tag to work. Begin by adding tags!', 'simpletags');
  296. } else {
  297. return __('This feature works only with activated JavaScript. Activate it in your Web browser so you can!', 'simpletags');
  298. }
  299. }
  300. /**
  301. * A short function for display the same copyright on all admin pages
  302. *
  303. * @return void
  304. * @author Amaury Balmer
  305. */
  306. function printAdminFooter() {
  307. ?>
  308. <p class="footer_st"><?php printf(__('&copy; Copyright 2007-2011 <a href="http://www.herewithme.fr/" title="Here With Me">Amaury Balmer</a> | <a href="http://wordpress.org/extend/plugins/simple-tags">Simple Tags</a> | Version %s', 'simpletags'), STAGS_VERSION); ?></p>
  309. <?php
  310. }
  311. /**
  312. * Display WP alert using class var
  313. *
  314. * @return void
  315. * @author Amaury Balmer
  316. */
  317. function displayMessage() {
  318. if ( $this->message != '') {
  319. $message = $this->message;
  320. $status = $this->status;
  321. $this->message = $this->status = ''; // Reset
  322. }
  323. if ( isset($message) && !empty($message) ) {
  324. ?>
  325. <div id="message" class="<?php echo ($status != '') ? $status :'updated'; ?> fade">
  326. <p><strong><?php echo $message; ?></strong></p>
  327. </div>
  328. <?php
  329. }
  330. }
  331. /**
  332. * Ouput formatted options
  333. *
  334. * @param array $option_data
  335. * @return string
  336. * @author Amaury Balmer
  337. */
  338. function printOptions( $option_data ) {
  339. // Get options
  340. $option_actual = (array) get_option( STAGS_OPTIONS_NAME );
  341. // Generate output
  342. $output = '';
  343. foreach( $option_data as $section => $options) {
  344. $output .= "\n" . '<div id="'. sanitize_title($section) .'"><fieldset class="options"><legend>' . $this->getNiceTitleOptions($section) . '</legend><table class="form-table">' . "\n";
  345. foreach((array) $options as $option) {
  346. // Helper
  347. if ( $option[2] == 'helper' ) {
  348. $output .= '<tr style="vertical-align: middle;"><td class="helper" colspan="2">' . stripslashes($option[4]) . '</td></tr>' . "\n";
  349. continue;
  350. }
  351. // Fix notices
  352. if ( !isset($option_actual[ $option[0] ]) )
  353. $option_actual[ $option[0] ] = '';
  354. switch ( $option[2] ) {
  355. case 'checkbox':
  356. $input_type = '<input type="checkbox" id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option[3]) . '" ' . ( ($option_actual[ $option[0] ]) ? 'checked="checked"' : '') . ' />' . "\n";
  357. break;
  358. case 'dropdown':
  359. $selopts = explode('/', $option[3]);
  360. $seldata = '';
  361. foreach( (array) $selopts as $sel) {
  362. $seldata .= '<option value="' . esc_attr($sel) . '" ' .((isset($option_actual[ $option[0] ]) &&$option_actual[ $option[0] ] == $sel) ? 'selected="selected"' : '') .' >' . ucfirst($sel) . '</option>' . "\n";
  363. }
  364. $input_type = '<select id="' . $option[0] . '" name="' . $option[0] . '">' . $seldata . '</select>' . "\n";
  365. break;
  366. case 'text-color':
  367. $input_type = '<input type="text" ' . ((isset($option[3]) && $option[3]>50) ? ' style="width: 95%" ' : '') . 'id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option_actual[ $option[0] ]) . '" size="' . $option[3] .'" /><div class="box_color ' . $option[0] . '"></div>' . "\n";
  368. break;
  369. case 'text':
  370. default:
  371. $input_type = '<input type="text" ' . ((isset($option[3]) && $option[3]>50) ? ' style="width: 95%" ' : '') . 'id="' . $option[0] . '" name="' . $option[0] . '" value="' . esc_attr($option_actual[ $option[0] ]) . '" size="' . $option[3] .'" />' . "\n";
  372. break;
  373. }
  374. // Additional Information
  375. $extra = '';
  376. if( !empty($option[4]) ) {
  377. $extra = '<div class="stpexplan">' . __($option[4]) . '</div>' . "\n";
  378. }
  379. // Output
  380. $output .= '<tr style="vertical-align: top;"><th scope="row"><label for="'.$option[0].'">' . __($option[1]) . '</label></th><td>' . $input_type . ' ' . $extra . '</td></tr>' . "\n";
  381. }
  382. $output .= '</table>' . "\n";
  383. $output .= '</fieldset></div>' . "\n";
  384. }
  385. return $output;
  386. }
  387. /**
  388. * Get nice title for tabs title option
  389. *
  390. * @param string $id
  391. * @return string
  392. */
  393. function getNiceTitleOptions( $id = '' ) {
  394. switch ( $id ) {
  395. case 'administration':
  396. return __('Administration', 'simpletags');
  397. break;
  398. case 'auto-links':
  399. return __('Auto link', 'simpletags');
  400. break;
  401. case 'features':
  402. return __('Features', 'simpletags');
  403. break;
  404. case 'metakeywords':
  405. return __('Meta Keyword', 'simpletags');
  406. break;
  407. case 'embeddedtags':
  408. return __('Embedded Tags', 'simpletags');
  409. break;
  410. case 'tagspost':
  411. return __('Tags for Current Post', 'simpletags');
  412. break;
  413. case 'relatedposts':
  414. return __('Related Posts', 'simpletags');
  415. break;
  416. case 'relatedtags':
  417. return __('Related Tags', 'simpletags');
  418. break;
  419. case 'tagcloud':
  420. return __('Tag cloud', 'simpletags');
  421. break;
  422. }
  423. return '';
  424. }
  425. /**
  426. * This method allow to check if the DB is up to date, and if a upgrade is need for options
  427. *
  428. * @return void
  429. * @author Amaury Balmer
  430. */
  431. function upgrade() {
  432. // Get current version number
  433. $current_version = get_option( STAGS_OPTIONS_NAME . '-version' );
  434. // Upgrade needed ?
  435. if ( $current_version == false || version_compare($current_version, STAGS_VERSION, '<') ) {
  436. $current_options = get_option( STAGS_OPTIONS_NAME );
  437. $default_options = (array) include( dirname(__FILE__) . '/helper.options.default.php' );
  438. // Add new options
  439. foreach( $default_options as $key => $default_value ) {
  440. if ( !isset($current_options[$key]) ) {
  441. $current_options[$key] = $default_value;
  442. }
  443. }
  444. // Remove old options
  445. foreach( $current_options as $key => $current_value ) {
  446. if ( !isset($default_options[$key]) ) {
  447. unset($current_options[$key]);
  448. }
  449. }
  450. update_option( STAGS_OPTIONS_NAME . '-version', STAGS_VERSION );
  451. update_option( STAGS_OPTIONS_NAME, $current_options );
  452. }
  453. return true;
  454. }
  455. /**
  456. * Make a simple SQL query with some args for get terms for ajax display
  457. *
  458. * @param string $taxonomy
  459. * @param string $search
  460. * @param string $order_by
  461. * @param string $order
  462. * @return array
  463. * @author Amaury Balmer
  464. */
  465. function getTermsForAjax( $taxonomy = 'post_tag', $search = '', $order_by = 'name', $order = 'ASC' ) {
  466. global $wpdb;
  467. if ( !empty($search) ) {
  468. return $wpdb->get_results( $wpdb->prepare("
  469. SELECT DISTINCT t.name, t.term_id
  470. FROM {$wpdb->terms} AS t
  471. INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
  472. WHERE tt.taxonomy = %s
  473. AND name LIKE %s
  474. ORDER BY $order_by $order
  475. ", $taxonomy, '%'.$search.'%' ) );
  476. } else {
  477. return $wpdb->get_results( $wpdb->prepare("
  478. SELECT DISTINCT t.name, t.term_id
  479. FROM {$wpdb->terms} AS t
  480. INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
  481. WHERE tt.taxonomy = %s
  482. ORDER BY $order_by $order
  483. ", $taxonomy) );
  484. }
  485. }
  486. }
  487. ?>