PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/Wallynm/iptb
PHP | 275 lines | 211 code | 39 blank | 25 comment | 43 complexity | bb611f0d73937b9cbbf815e1e64f649b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.0
  1. <?php
  2. class SimpleTags_Admin_AutoTags extends SimpleTags_Admin {
  3. // Build admin URL
  4. var $tools_base_url = '';
  5. function SimpleTags_Admin_AutoTags() {
  6. $this->tools_base_url = admin_url('tools.php') . '?page=';
  7. // Admin menu
  8. add_action('admin_menu', array(&$this, 'adminMenu'));
  9. // Register taxo, parent method...
  10. $this->registerDetermineTaxonomy();
  11. }
  12. /**
  13. * Add WP admin menu for Tags
  14. *
  15. * @return void
  16. * @author Amaury Balmer
  17. */
  18. function adminMenu() {
  19. add_management_page( __('Simple Terms: Auto Terms', 'simpletags'), __('Auto Terms', 'simpletags'), 'simple_tags', 'st_auto', array(&$this, 'pageAutoTerms'));
  20. }
  21. /**
  22. * WP Page - Auto Tags
  23. *
  24. * @return void
  25. * @author Amaury Balmer
  26. */
  27. function pageAutoTerms() {
  28. global $simple_tags, $wpdb;
  29. // Get options
  30. $options = get_option( STAGS_OPTIONS_NAME_AUTO );
  31. if ( $options == false ) // First save ?
  32. $options = array();
  33. if ( !isset($options[$this->post_type]) ) { // First save for this CPT ?
  34. $options[$this->post_type] = array();
  35. }
  36. if ( !isset($options[$this->post_type][$this->taxonomy]) ) { // First save for this taxo ?
  37. $options[$this->post_type][$this->taxonomy] = array();
  38. }
  39. $taxo_options = $options[$this->post_type][$this->taxonomy]; // Edit local option taxo
  40. $action = false;
  41. if ( isset($_POST['update_auto_list']) ) {
  42. check_admin_referer('update_auto_list-simpletags');
  43. // Tags list
  44. $terms_list = stripslashes($_POST['auto_list']);
  45. $terms = explode(',', $terms_list);
  46. // Remove empty and duplicate elements
  47. $terms = array_filter($terms, '_delete_empty_element');
  48. $terms = array_unique($terms);
  49. $taxo_options['auto_list'] = maybe_serialize($terms);
  50. // Active auto terms ?
  51. $taxo_options['use_auto_terms'] = ( isset($_POST['use_auto_terms']) && $_POST['use_auto_terms'] == '1' ) ? '1' : '0';
  52. // All terms ?
  53. $taxo_options['at_all'] = ( isset($_POST['at_all']) && $_POST['at_all'] == '1' ) ? '1' : '0';
  54. // Empty only ?
  55. $taxo_options['at_empty'] = ( isset($_POST['at_empty']) && $_POST['at_empty'] == '1' ) ? '1' : '0';
  56. // Full word ?
  57. $taxo_options['only_full_word'] = ( isset($_POST['only_full_word']) && $_POST['only_full_word'] == '1' ) ? '1' : '0';
  58. $options[$this->post_type][$this->taxonomy] = $taxo_options;
  59. update_option( STAGS_OPTIONS_NAME_AUTO, $options );
  60. $this->message = __('Auto terms options updated !', 'simpletags');
  61. } elseif ( isset($_GET['action']) && $_GET['action'] == 'auto_tag' ) {
  62. $action = true;
  63. $n = ( isset($_GET['n']) ) ? intval($_GET['n']) : 0;
  64. }
  65. $terms_list = '';
  66. if ( isset($taxo_options['auto_list']) && !empty($taxo_options['auto_list']) ) {
  67. $terms = maybe_unserialize($taxo_options['auto_list']);
  68. if ( is_array($terms) ) {
  69. $terms_list = implode(', ', $terms);
  70. }
  71. }
  72. $this->displayMessage();
  73. ?>
  74. <div class="wrap st_wrap">
  75. <h2><?php _e('Overview', 'simpletags'); ?>
  76. <p><?php _e('The bulb are lit when the association taxonomy and custom post type have the classification automatic activated. Otherwise, the bulb is off.', 'simpletags'); ?>
  77. <table class="widefat tag fixed" cellspacing="0">
  78. <thead>
  79. <tr>
  80. <th scope="col" id="label" class="manage-column column-name"><?php _e('Custom types / Taxonomies', 'simpletags'); ?></th>
  81. <?php
  82. foreach ( get_taxonomies( array( 'show_ui' => true ), 'object' ) as $taxo ) {
  83. if ( empty($taxo->labels->name) )
  84. continue;
  85. echo '<th scope="col">'.esc_html($taxo->labels->name).'</th>';
  86. }
  87. ?>
  88. </tr>
  89. </thead>
  90. <tfoot>
  91. <tr>
  92. <th scope="col" class="manage-column column-name"><?php _e('Custom types / Taxonomies', 'simpletags'); ?></th>
  93. <?php
  94. foreach ( get_taxonomies( array( 'show_ui' => true ), 'object' ) as $taxo ) {
  95. if ( empty($taxo->labels->name) )
  96. continue;
  97. echo '<th scope="col">'.esc_html($taxo->labels->name).'</th>';
  98. }
  99. ?>
  100. </tr>
  101. </tfoot>
  102. <tbody id="the-list" class="list:taxonomies">
  103. <?php
  104. $class = 'alternate';
  105. $i = 0;
  106. foreach ( get_post_types( array(), 'objects' ) as $post_type ) :
  107. if ( !$post_type->show_ui || empty($post_type->labels->name) )
  108. continue;
  109. $i++;
  110. $class = ( $class == 'alternate' ) ? '' : 'alternate';
  111. ?>
  112. <tr id="custom type-<?php echo $i; ?>" class="<?php echo $class; ?>">
  113. <th class="name column-name"><?php echo esc_html($post_type->labels->name); ?></th>
  114. <?php
  115. // Get compatible taxo for current post type
  116. $compatible_taxonomies = get_object_taxonomies( $post_type->name );
  117. foreach ( get_taxonomies( array( 'show_ui' => true ), 'object' ) as $line_taxo ) {
  118. if ( empty($line_taxo->labels->name) )
  119. continue;
  120. echo '<td>' . "\n";
  121. if ( in_array($line_taxo->name, $compatible_taxonomies) ) {
  122. if ( isset($options[$post_type->name][$line_taxo->name]) && isset($options[$post_type->name][$line_taxo->name]['use_auto_terms']) && $options[$post_type->name][$line_taxo->name]['use_auto_terms'] == '1' ) {
  123. echo '<a href="'.$this->tools_base_url.'st_auto&taxo='.$line_taxo->name.'&cpt='.$post_type->name.'"><img src="'.STAGS_URL.'/inc/images/lightbulb.png" alt="'.__('Context configured & actived.', 'simpletags').'" /></a>' . "\n";
  124. } else {
  125. echo '<a href="'.$this->tools_base_url.'st_auto&taxo='.$line_taxo->name.'&cpt='.$post_type->name.'"><img src="'.STAGS_URL.'/inc/images/lightbulb_off.png" alt="'.__('Context unconfigured.', 'simpletags').'" /></a>' . "\n";
  126. }
  127. } else {
  128. echo '-' . "\n";
  129. }
  130. echo '</td>' . "\n";
  131. }
  132. ?>
  133. </tr>
  134. <?php endforeach; ?>
  135. </tbody>
  136. </table>
  137. <div class="clear"></div>
  138. </div>
  139. <div class="wrap st_wrap">
  140. <h2><?php printf(__('Auto Terms for %s and %s', 'simpletags'), '<strong>'.$this->post_type_name.'</strong>', '<strong>'.$this->taxo_name.'</strong>' ); ?></h2>
  141. <?php if ( $action === false ) : ?>
  142. <h3><?php _e('Auto terms list', 'simpletags'); ?></h3>
  143. <p><?php _e('This feature allows Wordpress to look into post content and title for specified terms when saving posts. If your post content or title contains the word "WordPress" and you have "wordpress" in auto terms list, Simple Tags will add automatically "wordpress" as term for this post.', 'simpletags'); ?></p>
  144. <h3><?php _e('Options', 'simpletags'); ?></h3>
  145. <form action="<?php echo $this->tools_base_url.'st_auto&taxo='.$this->taxonomy.'&cpt='.$this->post_type; ?>" method="post">
  146. <table class="form-table">
  147. <tr valign="top">
  148. <th scope="row"><?php _e('Activation', 'simpletags'); ?></th>
  149. <td>
  150. <input type="checkbox" id="use_auto_terms" name="use_auto_terms" value="1" <?php echo ( isset($taxo_options['use_auto_terms']) && $taxo_options['use_auto_terms'] == 1 ) ? 'checked="checked"' : ''; ?> />
  151. <label for="use_auto_terms"><?php _e('Active Auto Tags.', 'simpletags'); ?></label>
  152. </td>
  153. </tr>
  154. <tr valign="top">
  155. <th scope="row"><?php _e('Terms database', 'simpletags'); ?></th>
  156. <td>
  157. <input type="checkbox" id="at_all" name="at_all" value="1" <?php echo ( isset($taxo_options['at_all']) && $taxo_options['at_all'] == 1 ) ? 'checked="checked"' : ''; ?> />
  158. <label for="at_all"><?php _e('Use also local terms database with auto terms. (Warning, this option can increases the CPU consumption a lot if you have many terms)', 'simpletags'); ?></label>
  159. </td>
  160. </tr>
  161. <tr valign="top">
  162. <th scope="row"><?php _e('Target', 'simpletags'); ?></th>
  163. <td>
  164. <input type="checkbox" id="at_empty" name="at_empty" value="1" <?php echo ( isset($taxo_options['at_empty']) && $taxo_options['at_empty'] == 1 ) ? 'checked="checked"' : ''; ?> />
  165. <label for="at_empty"><?php _e('Autotag only posts without terms.', 'simpletags'); ?></label>
  166. </td>
  167. </tr>
  168. <tr valign="top">
  169. <th scope="row"><?php _e('Whole Word ?', 'simpletags'); ?></th>
  170. <td>
  171. <input type="checkbox" id="only_full_word" name="only_full_word" value="1" <?php echo ( isset($taxo_options['only_full_word']) && $taxo_options['only_full_word'] == 1 ) ? 'checked="checked"' : ''; ?> />
  172. <label for="only_full_word"><?php _e('Autotag only a post when terms finded in the content are a the same name. (whole word only)', 'simpletags'); ?></label>
  173. </td>
  174. </tr>
  175. <tr valign="top">
  176. <th scope="row"><label for="auto_list"><?php _e('Keywords list', 'simpletags'); ?></label></th>
  177. <td>
  178. <input type="text" id="auto_list" class="auto_list" name="auto_list" value="<?php echo esc_attr($terms_list); ?>" style="width:98%;" />
  179. <br /><?php _e('Separated with a comma', 'simpletags'); ?>
  180. </td>
  181. </tr>
  182. </table>
  183. <p class="submit">
  184. <?php wp_nonce_field( 'update_auto_list-simpletags' ); ?>
  185. <input class="button-primary" type="submit" name="update_auto_list" value="<?php _e('Update options &raquo;', 'simpletags'); ?>" />
  186. </p>
  187. </form>
  188. <h3><?php _e('Auto terms old content', 'simpletags'); ?></h3>
  189. <p>
  190. <?php _e('Simple Tags can also tag all existing contents of your blog. This feature use auto terms list above-mentioned.', 'simpletags'); ?>
  191. </p>
  192. <p class="submit">
  193. <a class="button-primary" href="<?php echo $this->tools_base_url.'st_auto&amp;taxo='.$this->taxonomy.'&amp;cpt='.$this->post_type.'&amp;action=auto_tag'; ?>"><?php _e('Auto terms all content &raquo;', 'simpletags'); ?></a>
  194. </p>
  195. <?php else:
  196. // Counter
  197. if ( $n == 0 ) {
  198. update_option('tmp_auto_terms_st', 0);
  199. }
  200. // Get objects
  201. $objects = (array) $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'publish' ORDER BY ID DESC LIMIT %d, 20", $this->post_type, $n) );
  202. if( !empty($objects) ) {
  203. echo '<ul>';
  204. foreach( $objects as $object ) {
  205. $simple_tags['client-autoterms']->autoTermsPost( $object, $this->taxonomy, $taxo_options, true );
  206. echo '<li>#'. $object->ID .' '. $object->post_title .'</li>';
  207. unset($object);
  208. }
  209. echo '</ul>';
  210. ?>
  211. <p><?php _e("If your browser doesn't start loading the next page automatically click this link:", 'simpletags'); ?> <a href="<?php echo $this->tools_base_url.'st_auto&amp;taxo='.$this->taxonomy.'&amp;cpt='.$this->post_type.'&amp;action=auto_tag&amp;n='.($n + 20); ?>"><?php _e('Next content', 'simpletags'); ?></a></p>
  212. <script type="text/javascript">
  213. // <![CDATA[
  214. function nextPage() {
  215. location.href = "<?php echo $this->tools_base_url.'st_auto&taxo='.$this->taxonomy.'&cpt='.$this->post_type.'&action=auto_tag&n='.($n + 20); ?>";
  216. }
  217. window.setTimeout( 'nextPage()', 300 );
  218. // ]]>
  219. </script>
  220. <?php
  221. } else {
  222. $counter = get_option('tmp_auto_terms_st');
  223. delete_option('tmp_auto_terms_st');
  224. echo '<p><strong>'.sprintf(__('All done! %s terms added.', 'simpletags'), $counter).'</strong></p>';
  225. }
  226. endif;
  227. ?>
  228. <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>
  229. <?php $this->printAdminFooter(); ?>
  230. </div>
  231. <?php
  232. do_action( 'simpletags-auto_terms', $this->taxonomy );
  233. }
  234. }
  235. ?>