PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/class-primary-term-admin.php

https://bitbucket.org/carloskikea/helpet
PHP | 234 lines | 104 code | 38 blank | 92 comment | 12 complexity | d1026c8b8394763b31a22f0baa0386af MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin
  6 */
  7
  8/**
  9 * Adds the UI to change the primary term for a post
 10 */
 11class WPSEO_Primary_Term_Admin {
 12
 13	/**
 14	 * Constructor.
 15	 */
 16	public function __construct() {
 17		add_action( 'admin_footer', array( $this, 'wp_footer' ), 10 );
 18
 19		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
 20
 21		add_action( 'save_post', array( $this, 'save_primary_terms' ) );
 22
 23		$primary_term = new WPSEO_Frontend_Primary_Category();
 24		$primary_term->register_hooks();
 25	}
 26
 27	/**
 28	 * Get the current post ID.
 29	 *
 30	 * @return integer The post ID.
 31	 */
 32	protected function get_current_id() {
 33		$post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
 34		if ( empty( $post_id ) && isset( $GLOBALS['post_ID'] ) ) {
 35			$post_id = filter_var( $GLOBALS['post_ID'], FILTER_SANITIZE_NUMBER_INT );
 36		}
 37
 38		return $post_id;
 39	}
 40
 41	/**
 42	 * Add primary term templates
 43	 */
 44	public function wp_footer() {
 45		$taxonomies = $this->get_primary_term_taxonomies();
 46
 47		if ( ! empty( $taxonomies ) ) {
 48			$this->include_js_templates();
 49		}
 50	}
 51
 52	/**
 53	 * Enqueues all the assets needed for the primary term interface
 54	 *
 55	 * @return void
 56	 */
 57	public function enqueue_assets() {
 58		global $pagenow;
 59
 60		if ( ! WPSEO_Metabox::is_post_edit( $pagenow ) ) {
 61			return;
 62		}
 63
 64		$taxonomies = $this->get_primary_term_taxonomies();
 65
 66		// Only enqueue if there are taxonomies that need a primary term.
 67		if ( empty( $taxonomies ) ) {
 68			return;
 69		}
 70
 71		$asset_manager = new WPSEO_Admin_Asset_Manager();
 72		$asset_manager->enqueue_style( 'primary-category' );
 73		$asset_manager->enqueue_script( 'primary-category' );
 74
 75		$taxonomies = array_map( array( $this, 'map_taxonomies_for_js' ), $taxonomies );
 76
 77		$data = array(
 78			'taxonomies' => $taxonomies,
 79		);
 80		wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'primary-category', 'wpseoPrimaryCategoryL10n', $data );
 81	}
 82
 83	/**
 84	 * Saves all selected primary terms
 85	 *
 86	 * @param int $post_id Post ID to save primary terms for.
 87	 */
 88	public function save_primary_terms( $post_id ) {
 89		// Bail if this is a multisite installation and the site has been switched.
 90		if ( is_multisite() && ms_is_switched() ) {
 91			return;
 92		}
 93
 94		$taxonomies = $this->get_primary_term_taxonomies( $post_id );
 95
 96		foreach ( $taxonomies as $taxonomy ) {
 97			$this->save_primary_term( $post_id, $taxonomy );
 98		}
 99	}
100
101	/**
102	 * /**
103	 * Get the id of the primary term
104	 *
105	 * @param string $taxonomy_name Taxonomy name for the term.
106	 *
107	 * @return int primary term id
108	 */
109	protected function get_primary_term( $taxonomy_name ) {
110		$primary_term = new WPSEO_Primary_Term( $taxonomy_name, $this->get_current_id() );
111
112		return $primary_term->get_primary_term();
113	}
114
115	/**
116	 * Returns all the taxonomies for which the primary term selection is enabled
117	 *
118	 * @param int $post_id Default current post ID.
119	 * @return array
120	 */
121	protected function get_primary_term_taxonomies( $post_id = null ) {
122
123		if ( null === $post_id ) {
124			$post_id = $this->get_current_id();
125		}
126
127		$taxonomies = wp_cache_get( 'primary_term_taxonomies_' . $post_id, 'wpseo' );
128		if ( false !== $taxonomies ) {
129			return $taxonomies;
130		}
131
132		$taxonomies = $this->generate_primary_term_taxonomies( $post_id );
133
134		wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );
135
136		return $taxonomies;
137	}
138
139	/**
140	 * Include templates file
141	 */
142	protected function include_js_templates() {
143		include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php';
144	}
145
146	/**
147	 * Save the primary term for a specific taxonomy
148	 *
149	 * @param int     $post_id  Post ID to save primary term for.
150	 * @param WP_Term $taxonomy Taxonomy to save primary term for.
151	 */
152	protected function save_primary_term( $post_id, $taxonomy ) {
153		$primary_term = filter_input( INPUT_POST, WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term', FILTER_SANITIZE_NUMBER_INT );
154
155		// We accept an empty string here because we need to save that if no terms are selected.
156		if ( null !== $primary_term && check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) ) {
157			$primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id );
158			$primary_term_object->set_primary_term( $primary_term );
159		}
160	}
161
162	/**
163	 * Generate the primary term taxonomies.
164	 *
165	 * @param int $post_id ID of the post.
166	 *
167	 * @return array
168	 */
169	protected function generate_primary_term_taxonomies( $post_id ) {
170		$post_type      = get_post_type( $post_id );
171		$all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
172		$all_taxonomies = array_filter( $all_taxonomies, array( $this, 'filter_hierarchical_taxonomies' ) );
173
174		/**
175		 * Filters which taxonomies for which the user can choose the primary term.
176		 *
177		 * @api array    $taxonomies An array of taxonomy objects that are primary_term enabled.
178		 *
179		 * @param string $post_type      The post type for which to filter the taxonomies.
180		 * @param array  $all_taxonomies All taxonomies for this post types, even ones that don't have primary term
181		 *                               enabled.
182		 */
183		$taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );
184
185		return $taxonomies;
186	}
187
188	/**
189	 * Returns an array suitable for use in the javascript
190	 *
191	 * @param stdClass $taxonomy The taxonomy to map.
192	 *
193	 * @return array
194	 */
195	private function map_taxonomies_for_js( $taxonomy ) {
196		$primary_term = $this->get_primary_term( $taxonomy->name );
197
198		if ( empty( $primary_term ) ) {
199			$primary_term = '';
200		}
201
202		return array(
203			'title'   => $taxonomy->labels->singular_name,
204			'name'    => $taxonomy->name,
205			'primary' => $primary_term,
206			'terms'   => array_map( array( $this, 'map_terms_for_js' ), get_terms( $taxonomy->name ) ),
207		);
208	}
209
210	/**
211	 * Returns an array suitable for use in the javascript
212	 *
213	 * @param stdClass $term The term to map.
214	 *
215	 * @return array
216	 */
217	private function map_terms_for_js( $term ) {
218		return array(
219			'id'   => $term->term_id,
220			'name' => $term->name,
221		);
222	}
223
224	/**
225	 * Returns whether or not a taxonomy is hierarchical
226	 *
227	 * @param stdClass $taxonomy Taxonomy object.
228	 *
229	 * @return bool
230	 */
231	private function filter_hierarchical_taxonomies( $taxonomy ) {
232		return (bool) $taxonomy->hierarchical;
233	}
234}