PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/vanafroo/landingpage
PHP | 322 lines | 251 code | 42 blank | 29 comment | 48 complexity | 4416afe3d2c9947582abfe6badc7b5a4 MD5 | raw file
  1. <?php
  2. class SimpleTags_Admin_AutoTags {
  3. // Build admin URL
  4. static $tools_base_url = '';
  5. /**
  6. *
  7. */
  8. public function __construct() {
  9. self::$tools_base_url = admin_url( 'tools.php' ) . '?page=';
  10. // Admin menu
  11. add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
  12. // Register taxo, parent method...
  13. SimpleTags_Admin::registerDetermineTaxonomy();
  14. }
  15. /**
  16. * Add WP admin menu for Tags
  17. *
  18. * @return void
  19. * @author Amaury Balmer
  20. */
  21. public static function admin_menu() {
  22. add_management_page( __( 'Simple Terms: Auto Terms', 'simpletags' ), __( 'Auto Terms', 'simpletags' ), 'simple_tags', 'st_auto', array(
  23. __CLASS__,
  24. 'pageAutoTerms'
  25. ) );
  26. }
  27. /**
  28. * WP Page - Auto Tags
  29. *
  30. * @return void
  31. * @author Amaury Balmer
  32. */
  33. public static function pageAutoTerms() {
  34. global $wpdb;
  35. // Get options
  36. $options = get_option( STAGS_OPTIONS_NAME_AUTO );
  37. if ( $options == false ) // First save ?
  38. {
  39. $options = array();
  40. }
  41. if ( ! isset( $options[ SimpleTags_Admin::$post_type ] ) ) { // First save for this CPT ?
  42. $options[ SimpleTags_Admin::$post_type ] = array();
  43. }
  44. if ( ! isset( $options[ SimpleTags_Admin::$post_type ][ SimpleTags_Admin::$taxonomy ] ) ) { // First save for this taxo ?
  45. $options[ SimpleTags_Admin::$post_type ][ SimpleTags_Admin::$taxonomy ] = array();
  46. }
  47. $taxo_options = $options[ SimpleTags_Admin::$post_type ][ SimpleTags_Admin::$taxonomy ]; // Edit local option taxo
  48. $action = false;
  49. if ( isset( $_POST['update_auto_list'] ) ) {
  50. check_admin_referer( 'update_auto_list-simpletags' );
  51. // Tags list
  52. $terms_list = stripslashes( $_POST['auto_list'] );
  53. $terms = explode( ',', $terms_list );
  54. // Remove empty and duplicate elements
  55. $terms = array_filter( $terms, '_delete_empty_element' );
  56. $terms = array_unique( $terms );
  57. $taxo_options['auto_list'] = maybe_serialize( $terms );
  58. // Active auto terms ?
  59. $taxo_options['use_auto_terms'] = ( isset( $_POST['use_auto_terms'] ) && $_POST['use_auto_terms'] == '1' ) ? '1' : '0';
  60. // All terms ?
  61. $taxo_options['at_all'] = ( isset( $_POST['at_all'] ) && $_POST['at_all'] == '1' ) ? '1' : '0';
  62. // Empty only ?
  63. $taxo_options['at_empty'] = ( isset( $_POST['at_empty'] ) && $_POST['at_empty'] == '1' ) ? '1' : '0';
  64. // Full word ?
  65. $taxo_options['only_full_word'] = ( isset( $_POST['only_full_word'] ) && $_POST['only_full_word'] == '1' ) ? '1' : '0';
  66. // Support hashtag format ?
  67. $taxo_options['allow_hashtag_format'] = ( isset( $_POST['allow_hashtag_format'] ) && $_POST['allow_hashtag_format'] == '1' ) ? '1' : '0';
  68. $options[ SimpleTags_Admin::$post_type ][ SimpleTags_Admin::$taxonomy ] = $taxo_options;
  69. update_option( STAGS_OPTIONS_NAME_AUTO, $options );
  70. add_settings_error( __CLASS__, __CLASS__, __( 'Auto terms options updated !', 'simpletags' ), 'updated' );
  71. } elseif ( isset( $_GET['action'] ) && $_GET['action'] == 'auto_tag' ) {
  72. $action = true;
  73. $n = ( isset( $_GET['n'] ) ) ? intval( $_GET['n'] ) : 0;
  74. }
  75. $terms_list = '';
  76. if ( isset( $taxo_options['auto_list'] ) && ! empty( $taxo_options['auto_list'] ) ) {
  77. $terms = maybe_unserialize( $taxo_options['auto_list'] );
  78. if ( is_array( $terms ) ) {
  79. $terms_list = implode( ', ', $terms );
  80. }
  81. }
  82. settings_errors( __CLASS__ );
  83. ?>
  84. <div class="wrap st_wrap">
  85. <h2><?php _e( 'Overview', 'simpletags' ); ?>
  86. <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' ); ?>
  87. <table class="widefat tag fixed" cellspacing="0">
  88. <thead>
  89. <tr>
  90. <th scope="col" id="label"
  91. class="manage-column column-name"><?php _e( 'Custom types / Taxonomies', 'simpletags' ); ?></th>
  92. <?php
  93. foreach ( get_taxonomies( array( 'show_ui' => true ), 'object' ) as $taxo ) {
  94. if ( empty( $taxo->labels->name ) ) {
  95. continue;
  96. }
  97. echo '<th scope="col">' . esc_html( $taxo->labels->name ) . '</th>';
  98. }
  99. ?>
  100. </tr>
  101. </thead>
  102. <tfoot>
  103. <tr>
  104. <th scope="col"
  105. class="manage-column column-name"><?php _e( 'Custom types / Taxonomies', 'simpletags' ); ?></th>
  106. <?php
  107. foreach ( get_taxonomies( array( 'show_ui' => true ), 'object' ) as $taxo ) {
  108. if ( empty( $taxo->labels->name ) ) {
  109. continue;
  110. }
  111. echo '<th scope="col">' . esc_html( $taxo->labels->name ) . '</th>';
  112. }
  113. ?>
  114. </tr>
  115. </tfoot>
  116. <tbody id="the-list" class="list:taxonomies">
  117. <?php
  118. $class = 'alternate';
  119. $i = 0;
  120. foreach ( get_post_types( array(), 'objects' ) as $post_type ) :
  121. if ( ! $post_type->show_ui || empty( $post_type->labels->name ) ) {
  122. continue;
  123. }
  124. // Get compatible taxo for current post type
  125. $compatible_taxonomies = get_object_taxonomies( $post_type->name );
  126. if ( empty( $compatible_taxonomies ) ) {
  127. continue;
  128. }
  129. $i ++;
  130. $class = ( $class == 'alternate' ) ? '' : 'alternate';
  131. ?>
  132. <tr id="custom type-<?php echo $i; ?>" class="<?php echo $class; ?>">
  133. <th class="name column-name"><?php echo esc_html( $post_type->labels->name ); ?></th>
  134. <?php
  135. foreach ( get_taxonomies( array( 'show_ui' => true ), 'object' ) as $line_taxo ) {
  136. if ( empty( $line_taxo->labels->name ) ) {
  137. continue;
  138. }
  139. echo '<td>' . "\n";
  140. if ( in_array( $line_taxo->name, $compatible_taxonomies ) ) {
  141. 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' ) {
  142. echo '<a href="' . self::$tools_base_url . 'st_auto&taxo=' . $line_taxo->name . '&cpt=' . $post_type->name . '"><img src="' . STAGS_URL . '/assets/images/lightbulb.png" alt="' . __( 'Context configured & actived.', 'simpletags' ) . '" /></a>' . "\n";
  143. } else {
  144. echo '<a href="' . self::$tools_base_url . 'st_auto&taxo=' . $line_taxo->name . '&cpt=' . $post_type->name . '"><img src="' . STAGS_URL . '/assets/images/lightbulb_off.png" alt="' . __( 'Context unconfigured.', 'simpletags' ) . '" /></a>' . "\n";
  145. }
  146. } else {
  147. echo '-' . "\n";
  148. }
  149. echo '</td>' . "\n";
  150. }
  151. ?>
  152. </tr>
  153. <?php endforeach; ?>
  154. </tbody>
  155. </table>
  156. <div class="clear"></div>
  157. </div>
  158. <div class="wrap st_wrap">
  159. <h2><?php printf( __( 'Auto Terms for %s and %s', 'simpletags' ), '<strong>' . SimpleTags_Admin::$post_type_name . '</strong>', '<strong>' . SimpleTags_Admin::$taxo_name . '</strong>' ); ?></h2>
  160. <?php if ( $action === false ) : ?>
  161. <h3><?php _e( 'Auto terms list', 'simpletags' ); ?></h3>
  162. <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>
  163. <h3><?php _e( 'Options', 'simpletags' ); ?></h3>
  164. <form
  165. action="<?php echo self::$tools_base_url . 'st_auto&taxo=' . SimpleTags_Admin::$taxonomy . '&cpt=' . SimpleTags_Admin::$post_type; ?>"
  166. method="post">
  167. <table class="form-table">
  168. <tr valign="top">
  169. <th scope="row"><?php _e( 'Activation', 'simpletags' ); ?></th>
  170. <td>
  171. <input type="checkbox" id="use_auto_terms" name="use_auto_terms"
  172. value="1" <?php echo ( isset( $taxo_options['use_auto_terms'] ) && $taxo_options['use_auto_terms'] == 1 ) ? 'checked="checked"' : ''; ?> />
  173. <label for="use_auto_terms"><?php _e( 'Active Auto Tags.', 'simpletags' ); ?></label>
  174. </td>
  175. </tr>
  176. <tr valign="top">
  177. <th scope="row"><?php _e( 'Terms database', 'simpletags' ); ?></th>
  178. <td>
  179. <input type="checkbox" id="at_all" name="at_all"
  180. value="1" <?php echo ( isset( $taxo_options['at_all'] ) && $taxo_options['at_all'] == 1 ) ? 'checked="checked"' : ''; ?> />
  181. <label
  182. 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>
  183. </td>
  184. </tr>
  185. <tr valign="top">
  186. <th scope="row"><?php _e( 'Target', 'simpletags' ); ?></th>
  187. <td>
  188. <input type="checkbox" id="at_empty" name="at_empty"
  189. value="1" <?php echo ( isset( $taxo_options['at_empty'] ) && $taxo_options['at_empty'] == 1 ) ? 'checked="checked"' : ''; ?> />
  190. <label
  191. for="at_empty"><?php _e( 'Autotag only posts without terms.', 'simpletags' ); ?></label>
  192. </td>
  193. </tr>
  194. <tr valign="top">
  195. <th scope="row"><?php _e( 'Whole Word ?', 'simpletags' ); ?></th>
  196. <td>
  197. <input type="checkbox" id="only_full_word" name="only_full_word"
  198. value="1" <?php echo ( isset( $taxo_options['only_full_word'] ) && $taxo_options['only_full_word'] == 1 ) ? 'checked="checked"' : ''; ?> />
  199. <label
  200. 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>
  201. </td>
  202. </tr>
  203. <tr valign="top">
  204. <th scope="row"><?php _e( 'Suport Hashtag format ?', 'simpletags' ); ?></th>
  205. <td>
  206. <input type="checkbox" id="allow_hashtag_format" name="allow_hashtag_format"
  207. value="1" <?php echo ( isset( $taxo_options['allow_hashtag_format'] ) && $taxo_options['allow_hashtag_format'] == 1 ) ? 'checked="checked"' : ''; ?> />
  208. <label
  209. for="allow_hashtag_format"><?php _e( 'When the whole word option is enabled, hashtag will not be autotag because of # prefix. This option allow to fixed this issue!', 'simpletags' ); ?></label>
  210. </td>
  211. </tr>
  212. <tr valign="top">
  213. <th scope="row"><label for="auto_list"><?php _e( 'Keywords list', 'simpletags' ); ?></label>
  214. </th>
  215. <td>
  216. <input type="text" id="auto_list" class="auto_list" name="auto_list"
  217. value="<?php echo esc_attr( $terms_list ); ?>" style="width:98%;"/>
  218. <br/><?php _e( 'Separated with a comma', 'simpletags' ); ?>
  219. </td>
  220. </tr>
  221. </table>
  222. <p class="submit">
  223. <?php wp_nonce_field( 'update_auto_list-simpletags' ); ?>
  224. <input class="button-primary" type="submit" name="update_auto_list"
  225. value="<?php _e( 'Update options &raquo;', 'simpletags' ); ?>"/>
  226. </p>
  227. </form>
  228. <h3><?php _e( 'Auto terms old content', 'simpletags' ); ?></h3>
  229. <p>
  230. <?php _e( 'Simple Tags can also tag all existing contents of your blog. This feature use auto terms list above-mentioned.', 'simpletags' ); ?>
  231. </p>
  232. <p class="submit">
  233. <a class="button-primary"
  234. href="<?php echo self::$tools_base_url . 'st_auto&amp;taxo=' . SimpleTags_Admin::$taxonomy . '&amp;cpt=' . SimpleTags_Admin::$post_type . '&amp;action=auto_tag'; ?>"><?php _e( 'Auto terms all content &raquo;', 'simpletags' ); ?></a>
  235. </p>
  236. <?php else:
  237. // Counter
  238. {
  239. if ( $n == 0 ) {
  240. update_option( 'tmp_auto_terms_st', 0 );
  241. }
  242. // Get objects
  243. $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", SimpleTags_Admin::$post_type, $n ) );
  244. if ( ! empty( $objects ) ) {
  245. echo '<ul>';
  246. foreach ( $objects as $object ) {
  247. SimpleTags_Client_Autoterms::auto_terms_post( $object, SimpleTags_Admin::$taxonomy, $taxo_options, true );
  248. echo '<li>#' . $object->ID . ' ' . $object->post_title . '</li>';
  249. unset( $object );
  250. }
  251. echo '</ul>';
  252. ?>
  253. <p><?php _e( "If your browser doesn't start loading the next page automatically click this link:", 'simpletags' ); ?>
  254. <a href="<?php echo self::$tools_base_url . 'st_auto&amp;taxo=' . SimpleTags_Admin::$taxonomy . '&amp;cpt=' . SimpleTags_Admin::$post_type . '&amp;action=auto_tag&amp;n=' . ( $n + 20 ); ?>"><?php _e( 'Next content', 'simpletags' ); ?></a>
  255. </p>
  256. <script type="text/javascript">
  257. // <![CDATA[
  258. function nextPage() {
  259. location.href = "<?php echo self::$tools_base_url.'st_auto&taxo='.SimpleTags_Admin::$taxonomy.'&cpt='.SimpleTags_Admin::$post_type.'&action=auto_tag&n='.($n + 20); ?>";
  260. }
  261. window.setTimeout('nextPage()', 300);
  262. // ]]>
  263. </script>
  264. <?php
  265. } else {
  266. $counter = get_option( 'tmp_auto_terms_st' );
  267. delete_option( 'tmp_auto_terms_st' );
  268. echo '<p><strong>' . sprintf( __( 'All done! %s terms added.', 'simpletags' ), $counter ) . '</strong></p>';
  269. }
  270. }
  271. endif;
  272. ?>
  273. <p><?php _e( 'Visit the <a href="https://github.com/herewithme/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>
  274. <?php SimpleTags_Admin::printAdminFooter(); ?>
  275. </div>
  276. <?php
  277. do_action( 'simpletags-auto_terms', SimpleTags_Admin::$taxonomy );
  278. }
  279. }