@@ -14,215 +14,215 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Search_Keyword_Taxonomy { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The taxonomy name. |
|
| 19 | - * |
|
| 20 | - * @since 3.20.0 |
|
| 21 | - */ |
|
| 22 | - const TAXONOMY_NAME = 'wl_search_keywords'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Api_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.20.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Api_Service $api_service The {@link Wordlift_Api_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $api_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The singleton instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.20.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Search_Keyword_Taxonomy $instance The singleton instance. |
|
| 39 | - */ |
|
| 40 | - private static $instance; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Create a Wordlift_Search_Keyword_Taxonomy instance. |
|
| 44 | - * |
|
| 45 | - * @param $api_service Wordlift_Api_Service WordLift's API Service. |
|
| 46 | - * |
|
| 47 | - * @since 3.20.0 |
|
| 48 | - */ |
|
| 49 | - public function __construct( $api_service ) { |
|
| 50 | - |
|
| 51 | - // Disable this module. |
|
| 52 | - return; |
|
| 53 | - |
|
| 54 | - $this->api_service = $api_service; |
|
| 55 | - |
|
| 56 | - // Register the taxonomy. |
|
| 57 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | - |
|
| 59 | - // Catch new terms. |
|
| 60 | - add_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 61 | - |
|
| 62 | - // Delete terms. |
|
| 63 | - add_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ), 10, 3 ); |
|
| 64 | - |
|
| 65 | - // Catch requests to list the taxonomy terms. |
|
| 66 | - add_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ), 10, 2 ); |
|
| 67 | - |
|
| 68 | - // Set the singleton instance. |
|
| 69 | - self::$instance = $this; |
|
| 70 | - |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Get the singleton instance. |
|
| 75 | - * |
|
| 76 | - * @since 3.20.0 |
|
| 77 | - * |
|
| 78 | - * @return \Wordlift_Search_Keyword_Taxonomy The singleton instance. |
|
| 79 | - */ |
|
| 80 | - public static function get_instance() { |
|
| 81 | - |
|
| 82 | - return self::$instance; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Register the taxonomy. |
|
| 87 | - * |
|
| 88 | - * @since 3.20.0 |
|
| 89 | - */ |
|
| 90 | - public function init() { |
|
| 91 | - |
|
| 92 | - // Add new taxonomy, NOT hierarchical (like tags) |
|
| 93 | - $labels = array( |
|
| 94 | - 'name' => _x( 'Search Keywords', 'taxonomy general name', 'wordlift' ), |
|
| 95 | - 'singular_name' => _x( 'Search Keyword', 'taxonomy singular name', 'wordlift' ), |
|
| 96 | - 'search_items' => __( 'Search Search Keywords', 'wordlift' ), |
|
| 97 | - 'popular_items' => __( 'Popular Search Keywords', 'wordlift' ), |
|
| 98 | - 'all_items' => __( 'All Search Keywords', 'wordlift' ), |
|
| 99 | - 'parent_item' => null, |
|
| 100 | - 'parent_item_colon' => null, |
|
| 101 | - 'edit_item' => __( 'Edit Search Keyword', 'wordlift' ), |
|
| 102 | - 'update_item' => __( 'Update Search Keyword', 'wordlift' ), |
|
| 103 | - 'add_new_item' => __( 'Add New Search Keyword', 'wordlift' ), |
|
| 104 | - 'new_item_name' => __( 'New Search Keyword Name', 'wordlift' ), |
|
| 105 | - 'separate_items_with_commas' => __( 'Separate search keywords with commas', 'wordlift' ), |
|
| 106 | - 'add_or_remove_items' => __( 'Add or remove search keywords', 'wordlift' ), |
|
| 107 | - 'choose_from_most_used' => __( 'Choose from the most used search keywords', 'wordlift' ), |
|
| 108 | - 'not_found' => __( 'No search keywords found.', 'wordlift' ), |
|
| 109 | - 'menu_name' => __( 'Search Keywords', 'wordlift' ), |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - $args = array( |
|
| 113 | - 'hierarchical' => false, |
|
| 114 | - 'labels' => $labels, |
|
| 115 | - 'show_ui' => true, |
|
| 116 | - 'show_tagcloud' => false, |
|
| 117 | - 'show_in_quick_edit' => true, |
|
| 118 | - 'query_var' => true, |
|
| 119 | - 'rewrite' => array( 'slug' => 'wl_search_keywords' ), |
|
| 120 | - 'public' => false, |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - register_taxonomy( self::TAXONOMY_NAME, null, $args ); |
|
| 124 | - |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Hook to the created_{taxonomy}. |
|
| 129 | - * |
|
| 130 | - * Synchronize the keywords with the remote keywords. |
|
| 131 | - * |
|
| 132 | - * @param int $term_id Term ID. |
|
| 133 | - * |
|
| 134 | - * @since 3.20.0 |
|
| 135 | - */ |
|
| 136 | - public function created( $term_id ) { |
|
| 137 | - |
|
| 138 | - /** @var WP_Term $term */ |
|
| 139 | - $term = get_term( $term_id ); |
|
| 140 | - |
|
| 141 | - $this->api_service->post( 'keywords', array( |
|
| 142 | - 'value' => $term->name, |
|
| 143 | - ) ); |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Delete the term |
|
| 149 | - * |
|
| 150 | - * @since 3.20.0 |
|
| 151 | - * |
|
| 152 | - * @param int $term Term ID. |
|
| 153 | - * @param int $tt_id Term taxonomy ID. |
|
| 154 | - * @param mixed $deleted_term Copy of the already-deleted term, in the form specified |
|
| 155 | - * by the parent function. WP_Error otherwise. |
|
| 156 | - */ |
|
| 157 | - public function delete( $term, $tt_id, $deleted_term ) { |
|
| 158 | - |
|
| 159 | - $term_name = is_object( $deleted_term ) ? $deleted_term->name : $deleted_term['name']; |
|
| 160 | - |
|
| 161 | - $this->api_service->delete( 'keywords/' . rawurlencode( $term_name ) ); |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Refresh all the taxonomy terms. |
|
| 168 | - * |
|
| 169 | - * @since 3.20.0 |
|
| 170 | - * |
|
| 171 | - * @param array $defaults An array of default get_terms() arguments. |
|
| 172 | - * @param array $taxonomies An array of taxonomies. |
|
| 173 | - * |
|
| 174 | - * @return array The `$defaults` array, unchanged. |
|
| 175 | - */ |
|
| 176 | - public function get_terms_defaults( $defaults, $taxonomies ) { |
|
| 177 | - |
|
| 178 | - // Bail out if the request is not about our taxonomy. |
|
| 179 | - if ( ! is_array( $taxonomies ) || ! in_array( self::TAXONOMY_NAME, $taxonomies ) ) { |
|
| 180 | - return $defaults; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // Remove any potential loop. |
|
| 184 | - remove_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ) ); |
|
| 185 | - remove_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 186 | - remove_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ) ); |
|
| 187 | - |
|
| 188 | - // Save all the keywords. |
|
| 189 | - $keywords = $this->api_service->get( 'keywords' ); |
|
| 190 | - |
|
| 191 | - // Bail out if we received an error. |
|
| 192 | - if ( is_wp_error( $keywords ) ) { |
|
| 193 | - return $defaults; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // Get the local terms. |
|
| 197 | - $terms = get_terms( self::TAXONOMY_NAME, array( 'get' => 'all', ) ); |
|
| 198 | - |
|
| 199 | - // Delete terms that do not exist any more. |
|
| 200 | - /** @var WP_Term $term */ |
|
| 201 | - foreach ( $terms as $term ) { |
|
| 202 | - if ( ! in_array( $term->name, $keywords ) ) { |
|
| 203 | - wp_delete_term( $term->term_id, self::TAXONOMY_NAME ); |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // Get the term name. |
|
| 208 | - $term_names = array_map( function ( $term ) { |
|
| 209 | - return $term->name; |
|
| 210 | - }, $terms ); |
|
| 211 | - |
|
| 212 | - // Insert terms that do not exist. |
|
| 213 | - foreach ( $keywords as $keyword ) { |
|
| 214 | - if ( ! in_array( $keyword, $term_names ) ) { |
|
| 215 | - wp_insert_term( $keyword, self::TAXONOMY_NAME ); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // Add us back. |
|
| 220 | - add_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ), 10, 3 ); |
|
| 221 | - add_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 222 | - add_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ), 10, 2 ); |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - return $defaults; |
|
| 226 | - } |
|
| 17 | + /** |
|
| 18 | + * The taxonomy name. |
|
| 19 | + * |
|
| 20 | + * @since 3.20.0 |
|
| 21 | + */ |
|
| 22 | + const TAXONOMY_NAME = 'wl_search_keywords'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Api_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.20.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Api_Service $api_service The {@link Wordlift_Api_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $api_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The singleton instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.20.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Search_Keyword_Taxonomy $instance The singleton instance. |
|
| 39 | + */ |
|
| 40 | + private static $instance; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Create a Wordlift_Search_Keyword_Taxonomy instance. |
|
| 44 | + * |
|
| 45 | + * @param $api_service Wordlift_Api_Service WordLift's API Service. |
|
| 46 | + * |
|
| 47 | + * @since 3.20.0 |
|
| 48 | + */ |
|
| 49 | + public function __construct( $api_service ) { |
|
| 50 | + |
|
| 51 | + // Disable this module. |
|
| 52 | + return; |
|
| 53 | + |
|
| 54 | + $this->api_service = $api_service; |
|
| 55 | + |
|
| 56 | + // Register the taxonomy. |
|
| 57 | + add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | + |
|
| 59 | + // Catch new terms. |
|
| 60 | + add_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 61 | + |
|
| 62 | + // Delete terms. |
|
| 63 | + add_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ), 10, 3 ); |
|
| 64 | + |
|
| 65 | + // Catch requests to list the taxonomy terms. |
|
| 66 | + add_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ), 10, 2 ); |
|
| 67 | + |
|
| 68 | + // Set the singleton instance. |
|
| 69 | + self::$instance = $this; |
|
| 70 | + |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Get the singleton instance. |
|
| 75 | + * |
|
| 76 | + * @since 3.20.0 |
|
| 77 | + * |
|
| 78 | + * @return \Wordlift_Search_Keyword_Taxonomy The singleton instance. |
|
| 79 | + */ |
|
| 80 | + public static function get_instance() { |
|
| 81 | + |
|
| 82 | + return self::$instance; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Register the taxonomy. |
|
| 87 | + * |
|
| 88 | + * @since 3.20.0 |
|
| 89 | + */ |
|
| 90 | + public function init() { |
|
| 91 | + |
|
| 92 | + // Add new taxonomy, NOT hierarchical (like tags) |
|
| 93 | + $labels = array( |
|
| 94 | + 'name' => _x( 'Search Keywords', 'taxonomy general name', 'wordlift' ), |
|
| 95 | + 'singular_name' => _x( 'Search Keyword', 'taxonomy singular name', 'wordlift' ), |
|
| 96 | + 'search_items' => __( 'Search Search Keywords', 'wordlift' ), |
|
| 97 | + 'popular_items' => __( 'Popular Search Keywords', 'wordlift' ), |
|
| 98 | + 'all_items' => __( 'All Search Keywords', 'wordlift' ), |
|
| 99 | + 'parent_item' => null, |
|
| 100 | + 'parent_item_colon' => null, |
|
| 101 | + 'edit_item' => __( 'Edit Search Keyword', 'wordlift' ), |
|
| 102 | + 'update_item' => __( 'Update Search Keyword', 'wordlift' ), |
|
| 103 | + 'add_new_item' => __( 'Add New Search Keyword', 'wordlift' ), |
|
| 104 | + 'new_item_name' => __( 'New Search Keyword Name', 'wordlift' ), |
|
| 105 | + 'separate_items_with_commas' => __( 'Separate search keywords with commas', 'wordlift' ), |
|
| 106 | + 'add_or_remove_items' => __( 'Add or remove search keywords', 'wordlift' ), |
|
| 107 | + 'choose_from_most_used' => __( 'Choose from the most used search keywords', 'wordlift' ), |
|
| 108 | + 'not_found' => __( 'No search keywords found.', 'wordlift' ), |
|
| 109 | + 'menu_name' => __( 'Search Keywords', 'wordlift' ), |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + $args = array( |
|
| 113 | + 'hierarchical' => false, |
|
| 114 | + 'labels' => $labels, |
|
| 115 | + 'show_ui' => true, |
|
| 116 | + 'show_tagcloud' => false, |
|
| 117 | + 'show_in_quick_edit' => true, |
|
| 118 | + 'query_var' => true, |
|
| 119 | + 'rewrite' => array( 'slug' => 'wl_search_keywords' ), |
|
| 120 | + 'public' => false, |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + register_taxonomy( self::TAXONOMY_NAME, null, $args ); |
|
| 124 | + |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Hook to the created_{taxonomy}. |
|
| 129 | + * |
|
| 130 | + * Synchronize the keywords with the remote keywords. |
|
| 131 | + * |
|
| 132 | + * @param int $term_id Term ID. |
|
| 133 | + * |
|
| 134 | + * @since 3.20.0 |
|
| 135 | + */ |
|
| 136 | + public function created( $term_id ) { |
|
| 137 | + |
|
| 138 | + /** @var WP_Term $term */ |
|
| 139 | + $term = get_term( $term_id ); |
|
| 140 | + |
|
| 141 | + $this->api_service->post( 'keywords', array( |
|
| 142 | + 'value' => $term->name, |
|
| 143 | + ) ); |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Delete the term |
|
| 149 | + * |
|
| 150 | + * @since 3.20.0 |
|
| 151 | + * |
|
| 152 | + * @param int $term Term ID. |
|
| 153 | + * @param int $tt_id Term taxonomy ID. |
|
| 154 | + * @param mixed $deleted_term Copy of the already-deleted term, in the form specified |
|
| 155 | + * by the parent function. WP_Error otherwise. |
|
| 156 | + */ |
|
| 157 | + public function delete( $term, $tt_id, $deleted_term ) { |
|
| 158 | + |
|
| 159 | + $term_name = is_object( $deleted_term ) ? $deleted_term->name : $deleted_term['name']; |
|
| 160 | + |
|
| 161 | + $this->api_service->delete( 'keywords/' . rawurlencode( $term_name ) ); |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Refresh all the taxonomy terms. |
|
| 168 | + * |
|
| 169 | + * @since 3.20.0 |
|
| 170 | + * |
|
| 171 | + * @param array $defaults An array of default get_terms() arguments. |
|
| 172 | + * @param array $taxonomies An array of taxonomies. |
|
| 173 | + * |
|
| 174 | + * @return array The `$defaults` array, unchanged. |
|
| 175 | + */ |
|
| 176 | + public function get_terms_defaults( $defaults, $taxonomies ) { |
|
| 177 | + |
|
| 178 | + // Bail out if the request is not about our taxonomy. |
|
| 179 | + if ( ! is_array( $taxonomies ) || ! in_array( self::TAXONOMY_NAME, $taxonomies ) ) { |
|
| 180 | + return $defaults; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // Remove any potential loop. |
|
| 184 | + remove_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ) ); |
|
| 185 | + remove_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 186 | + remove_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ) ); |
|
| 187 | + |
|
| 188 | + // Save all the keywords. |
|
| 189 | + $keywords = $this->api_service->get( 'keywords' ); |
|
| 190 | + |
|
| 191 | + // Bail out if we received an error. |
|
| 192 | + if ( is_wp_error( $keywords ) ) { |
|
| 193 | + return $defaults; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // Get the local terms. |
|
| 197 | + $terms = get_terms( self::TAXONOMY_NAME, array( 'get' => 'all', ) ); |
|
| 198 | + |
|
| 199 | + // Delete terms that do not exist any more. |
|
| 200 | + /** @var WP_Term $term */ |
|
| 201 | + foreach ( $terms as $term ) { |
|
| 202 | + if ( ! in_array( $term->name, $keywords ) ) { |
|
| 203 | + wp_delete_term( $term->term_id, self::TAXONOMY_NAME ); |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // Get the term name. |
|
| 208 | + $term_names = array_map( function ( $term ) { |
|
| 209 | + return $term->name; |
|
| 210 | + }, $terms ); |
|
| 211 | + |
|
| 212 | + // Insert terms that do not exist. |
|
| 213 | + foreach ( $keywords as $keyword ) { |
|
| 214 | + if ( ! in_array( $keyword, $term_names ) ) { |
|
| 215 | + wp_insert_term( $keyword, self::TAXONOMY_NAME ); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // Add us back. |
|
| 220 | + add_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ), 10, 3 ); |
|
| 221 | + add_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 222 | + add_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ), 10, 2 ); |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + return $defaults; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | 228 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | */ |
| 49 | - public function __construct( $api_service ) { |
|
| 49 | + public function __construct($api_service) { |
|
| 50 | 50 | |
| 51 | 51 | // Disable this module. |
| 52 | 52 | return; |
@@ -54,16 +54,16 @@ discard block |
||
| 54 | 54 | $this->api_service = $api_service; |
| 55 | 55 | |
| 56 | 56 | // Register the taxonomy. |
| 57 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 57 | + add_action('init', array($this, 'init')); |
|
| 58 | 58 | |
| 59 | 59 | // Catch new terms. |
| 60 | - add_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 60 | + add_action('created_'.self::TAXONOMY_NAME, array($this, 'created')); |
|
| 61 | 61 | |
| 62 | 62 | // Delete terms. |
| 63 | - add_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ), 10, 3 ); |
|
| 63 | + add_action('delete_'.self::TAXONOMY_NAME, array($this, 'delete'), 10, 3); |
|
| 64 | 64 | |
| 65 | 65 | // Catch requests to list the taxonomy terms. |
| 66 | - add_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ), 10, 2 ); |
|
| 66 | + add_filter('get_terms_defaults', array($this, 'get_terms_defaults'), 10, 2); |
|
| 67 | 67 | |
| 68 | 68 | // Set the singleton instance. |
| 69 | 69 | self::$instance = $this; |
@@ -91,22 +91,22 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | // Add new taxonomy, NOT hierarchical (like tags) |
| 93 | 93 | $labels = array( |
| 94 | - 'name' => _x( 'Search Keywords', 'taxonomy general name', 'wordlift' ), |
|
| 95 | - 'singular_name' => _x( 'Search Keyword', 'taxonomy singular name', 'wordlift' ), |
|
| 96 | - 'search_items' => __( 'Search Search Keywords', 'wordlift' ), |
|
| 97 | - 'popular_items' => __( 'Popular Search Keywords', 'wordlift' ), |
|
| 98 | - 'all_items' => __( 'All Search Keywords', 'wordlift' ), |
|
| 94 | + 'name' => _x('Search Keywords', 'taxonomy general name', 'wordlift'), |
|
| 95 | + 'singular_name' => _x('Search Keyword', 'taxonomy singular name', 'wordlift'), |
|
| 96 | + 'search_items' => __('Search Search Keywords', 'wordlift'), |
|
| 97 | + 'popular_items' => __('Popular Search Keywords', 'wordlift'), |
|
| 98 | + 'all_items' => __('All Search Keywords', 'wordlift'), |
|
| 99 | 99 | 'parent_item' => null, |
| 100 | 100 | 'parent_item_colon' => null, |
| 101 | - 'edit_item' => __( 'Edit Search Keyword', 'wordlift' ), |
|
| 102 | - 'update_item' => __( 'Update Search Keyword', 'wordlift' ), |
|
| 103 | - 'add_new_item' => __( 'Add New Search Keyword', 'wordlift' ), |
|
| 104 | - 'new_item_name' => __( 'New Search Keyword Name', 'wordlift' ), |
|
| 105 | - 'separate_items_with_commas' => __( 'Separate search keywords with commas', 'wordlift' ), |
|
| 106 | - 'add_or_remove_items' => __( 'Add or remove search keywords', 'wordlift' ), |
|
| 107 | - 'choose_from_most_used' => __( 'Choose from the most used search keywords', 'wordlift' ), |
|
| 108 | - 'not_found' => __( 'No search keywords found.', 'wordlift' ), |
|
| 109 | - 'menu_name' => __( 'Search Keywords', 'wordlift' ), |
|
| 101 | + 'edit_item' => __('Edit Search Keyword', 'wordlift'), |
|
| 102 | + 'update_item' => __('Update Search Keyword', 'wordlift'), |
|
| 103 | + 'add_new_item' => __('Add New Search Keyword', 'wordlift'), |
|
| 104 | + 'new_item_name' => __('New Search Keyword Name', 'wordlift'), |
|
| 105 | + 'separate_items_with_commas' => __('Separate search keywords with commas', 'wordlift'), |
|
| 106 | + 'add_or_remove_items' => __('Add or remove search keywords', 'wordlift'), |
|
| 107 | + 'choose_from_most_used' => __('Choose from the most used search keywords', 'wordlift'), |
|
| 108 | + 'not_found' => __('No search keywords found.', 'wordlift'), |
|
| 109 | + 'menu_name' => __('Search Keywords', 'wordlift'), |
|
| 110 | 110 | ); |
| 111 | 111 | |
| 112 | 112 | $args = array( |
@@ -116,11 +116,11 @@ discard block |
||
| 116 | 116 | 'show_tagcloud' => false, |
| 117 | 117 | 'show_in_quick_edit' => true, |
| 118 | 118 | 'query_var' => true, |
| 119 | - 'rewrite' => array( 'slug' => 'wl_search_keywords' ), |
|
| 119 | + 'rewrite' => array('slug' => 'wl_search_keywords'), |
|
| 120 | 120 | 'public' => false, |
| 121 | 121 | ); |
| 122 | 122 | |
| 123 | - register_taxonomy( self::TAXONOMY_NAME, null, $args ); |
|
| 123 | + register_taxonomy(self::TAXONOMY_NAME, null, $args); |
|
| 124 | 124 | |
| 125 | 125 | } |
| 126 | 126 | |
@@ -133,14 +133,14 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @since 3.20.0 |
| 135 | 135 | */ |
| 136 | - public function created( $term_id ) { |
|
| 136 | + public function created($term_id) { |
|
| 137 | 137 | |
| 138 | 138 | /** @var WP_Term $term */ |
| 139 | - $term = get_term( $term_id ); |
|
| 139 | + $term = get_term($term_id); |
|
| 140 | 140 | |
| 141 | - $this->api_service->post( 'keywords', array( |
|
| 141 | + $this->api_service->post('keywords', array( |
|
| 142 | 142 | 'value' => $term->name, |
| 143 | - ) ); |
|
| 143 | + )); |
|
| 144 | 144 | |
| 145 | 145 | } |
| 146 | 146 | |
@@ -154,11 +154,11 @@ discard block |
||
| 154 | 154 | * @param mixed $deleted_term Copy of the already-deleted term, in the form specified |
| 155 | 155 | * by the parent function. WP_Error otherwise. |
| 156 | 156 | */ |
| 157 | - public function delete( $term, $tt_id, $deleted_term ) { |
|
| 157 | + public function delete($term, $tt_id, $deleted_term) { |
|
| 158 | 158 | |
| 159 | - $term_name = is_object( $deleted_term ) ? $deleted_term->name : $deleted_term['name']; |
|
| 159 | + $term_name = is_object($deleted_term) ? $deleted_term->name : $deleted_term['name']; |
|
| 160 | 160 | |
| 161 | - $this->api_service->delete( 'keywords/' . rawurlencode( $term_name ) ); |
|
| 161 | + $this->api_service->delete('keywords/'.rawurlencode($term_name)); |
|
| 162 | 162 | |
| 163 | 163 | } |
| 164 | 164 | |
@@ -173,53 +173,53 @@ discard block |
||
| 173 | 173 | * |
| 174 | 174 | * @return array The `$defaults` array, unchanged. |
| 175 | 175 | */ |
| 176 | - public function get_terms_defaults( $defaults, $taxonomies ) { |
|
| 176 | + public function get_terms_defaults($defaults, $taxonomies) { |
|
| 177 | 177 | |
| 178 | 178 | // Bail out if the request is not about our taxonomy. |
| 179 | - if ( ! is_array( $taxonomies ) || ! in_array( self::TAXONOMY_NAME, $taxonomies ) ) { |
|
| 179 | + if ( ! is_array($taxonomies) || ! in_array(self::TAXONOMY_NAME, $taxonomies)) { |
|
| 180 | 180 | return $defaults; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // Remove any potential loop. |
| 184 | - remove_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ) ); |
|
| 185 | - remove_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 186 | - remove_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ) ); |
|
| 184 | + remove_filter('get_terms_defaults', array($this, 'get_terms_defaults')); |
|
| 185 | + remove_action('created_'.self::TAXONOMY_NAME, array($this, 'created')); |
|
| 186 | + remove_action('delete_'.self::TAXONOMY_NAME, array($this, 'delete')); |
|
| 187 | 187 | |
| 188 | 188 | // Save all the keywords. |
| 189 | - $keywords = $this->api_service->get( 'keywords' ); |
|
| 189 | + $keywords = $this->api_service->get('keywords'); |
|
| 190 | 190 | |
| 191 | 191 | // Bail out if we received an error. |
| 192 | - if ( is_wp_error( $keywords ) ) { |
|
| 192 | + if (is_wp_error($keywords)) { |
|
| 193 | 193 | return $defaults; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | // Get the local terms. |
| 197 | - $terms = get_terms( self::TAXONOMY_NAME, array( 'get' => 'all', ) ); |
|
| 197 | + $terms = get_terms(self::TAXONOMY_NAME, array('get' => 'all',)); |
|
| 198 | 198 | |
| 199 | 199 | // Delete terms that do not exist any more. |
| 200 | 200 | /** @var WP_Term $term */ |
| 201 | - foreach ( $terms as $term ) { |
|
| 202 | - if ( ! in_array( $term->name, $keywords ) ) { |
|
| 203 | - wp_delete_term( $term->term_id, self::TAXONOMY_NAME ); |
|
| 201 | + foreach ($terms as $term) { |
|
| 202 | + if ( ! in_array($term->name, $keywords)) { |
|
| 203 | + wp_delete_term($term->term_id, self::TAXONOMY_NAME); |
|
| 204 | 204 | } |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // Get the term name. |
| 208 | - $term_names = array_map( function ( $term ) { |
|
| 208 | + $term_names = array_map(function($term) { |
|
| 209 | 209 | return $term->name; |
| 210 | - }, $terms ); |
|
| 210 | + }, $terms); |
|
| 211 | 211 | |
| 212 | 212 | // Insert terms that do not exist. |
| 213 | - foreach ( $keywords as $keyword ) { |
|
| 214 | - if ( ! in_array( $keyword, $term_names ) ) { |
|
| 215 | - wp_insert_term( $keyword, self::TAXONOMY_NAME ); |
|
| 213 | + foreach ($keywords as $keyword) { |
|
| 214 | + if ( ! in_array($keyword, $term_names)) { |
|
| 215 | + wp_insert_term($keyword, self::TAXONOMY_NAME); |
|
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // Add us back. |
| 220 | - add_action( 'delete_' . self::TAXONOMY_NAME, array( $this, 'delete' ), 10, 3 ); |
|
| 221 | - add_action( 'created_' . self::TAXONOMY_NAME, array( $this, 'created' ) ); |
|
| 222 | - add_filter( 'get_terms_defaults', array( $this, 'get_terms_defaults' ), 10, 2 ); |
|
| 220 | + add_action('delete_'.self::TAXONOMY_NAME, array($this, 'delete'), 10, 3); |
|
| 221 | + add_action('created_'.self::TAXONOMY_NAME, array($this, 'created')); |
|
| 222 | + add_filter('get_terms_defaults', array($this, 'get_terms_defaults'), 10, 2); |
|
| 223 | 223 | |
| 224 | 224 | |
| 225 | 225 | return $defaults; |