PageRenderTime 18ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/formatter/class-post-metabox-formatter.php

https://bitbucket.org/carloskikea/helpet
PHP | 213 lines | 90 code | 34 blank | 89 comment | 7 complexity | 8e4cf6c3129986b326ef2da4a5de226f MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin\Formatter
  6 */
  7
  8/**
  9 * This class provides data for the post metabox by return its values for localization
 10 */
 11class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
 12
 13	/**
 14	 * @var WP_Post
 15	 */
 16	private $post;
 17
 18	/**
 19	 * @var string The permalink to follow.
 20	 */
 21	private $permalink;
 22
 23	/**
 24	 * Constructor.
 25	 *
 26	 * @param WP_Post|array $post      Post object.
 27	 * @param array         $options   Title options to use.
 28	 * @param string        $structure The permalink to follow.
 29	 */
 30	public function __construct( $post, array $options, $structure ) {
 31		$this->post      = $post;
 32		$this->permalink = $structure;
 33	}
 34
 35	/**
 36	 * Returns the translated values.
 37	 *
 38	 * @return array
 39	 */
 40	public function get_values() {
 41		$values = array(
 42			'search_url'          => $this->search_url(),
 43			'post_edit_url'       => $this->edit_url(),
 44			'base_url'            => $this->base_url_for_js(),
 45			'metaDescriptionDate' => '',
 46		);
 47
 48		if ( $this->post instanceof WP_Post ) {
 49			$values_to_set = array(
 50				'keyword_usage'       => $this->get_focus_keyword_usage(),
 51				'title_template'      => $this->get_title_template(),
 52				'metadesc_template'   => $this->get_metadesc_template(),
 53				'metaDescriptionDate' => $this->get_metadesc_date(),
 54			);
 55
 56			$values = ( $values_to_set + $values );
 57		}
 58
 59		return $values;
 60	}
 61
 62	/**
 63	 * Returns the url to search for keyword for the post
 64	 *
 65	 * @return string
 66	 */
 67	private function search_url() {
 68		return admin_url( 'edit.php?seo_kw_filter={keyword}' );
 69	}
 70
 71	/**
 72	 * Returns the url to edit the taxonomy
 73	 *
 74	 * @return string
 75	 */
 76	private function edit_url() {
 77		return admin_url( 'post.php?post={id}&action=edit' );
 78	}
 79
 80	/**
 81	 * Returns a base URL for use in the JS, takes permalink structure into account
 82	 *
 83	 * @return string
 84	 */
 85	private function base_url_for_js() {
 86		global $pagenow;
 87
 88		// The default base is the home_url.
 89		$base_url = home_url( '/', null );
 90
 91		if ( 'post-new.php' === $pagenow ) {
 92			return $base_url;
 93		}
 94
 95		// If %postname% is the last tag, just strip it and use that as a base.
 96		if ( 1 === preg_match( '#%postname%/?$#', $this->permalink ) ) {
 97			$base_url = preg_replace( '#%postname%/?$#', '', $this->permalink );
 98		}
 99
100		return $base_url;
101	}
102
103	/**
104	 * Counts the number of given keywords used for other posts other than the given post_id.
105	 *
106	 * @return array The keyword and the associated posts that use it.
107	 */
108	private function get_focus_keyword_usage() {
109		$keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
110		$usage   = array( $keyword => $this->get_keyword_usage_for_current_post( $keyword ) );
111
112		if ( WPSEO_Utils::is_yoast_seo_premium() ) {
113			return $this->get_premium_keywords( $usage );
114		}
115
116		return $usage;
117	}
118
119	/**
120	 * Retrieves the additional keywords from Premium, that are associated with the post.
121	 *
122	 * @param array $usage The original keyword usage for the main keyword.
123	 *
124	 * @return array The keyword usage, including the additional keywords.
125	 */
126	protected function get_premium_keywords( $usage ) {
127		$additional_keywords = json_decode( WPSEO_Meta::get_value( 'focuskeywords', $this->post->ID ), true );
128
129		if ( empty( $additional_keywords ) ) {
130			return $usage;
131		}
132
133		foreach ( $additional_keywords as $additional_keyword ) {
134			$keyword = $additional_keyword['keyword'];
135
136			$usage[ $keyword ] = $this->get_keyword_usage_for_current_post( $keyword );
137		}
138
139		return $usage;
140	}
141
142	/**
143	 * Gets the keyword usage for the current post and the specified keyword.
144	 *
145	 * @param string $keyword The keyword to check the usage of.
146	 *
147	 * @return array The post IDs which use the passed keyword.
148	 */
149	protected function get_keyword_usage_for_current_post( $keyword ) {
150		return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
151	}
152
153	/**
154	 * Retrieves the title template.
155	 *
156	 * @return string
157	 */
158	private function get_title_template() {
159		return $this->get_template( 'title' );
160	}
161
162	/**
163	 * Retrieves the metadesc template.
164	 *
165	 * @return string
166	 */
167	private function get_metadesc_template() {
168		return $this->get_template( 'metadesc' );
169	}
170
171	/**
172	 * Retrieves a template.
173	 *
174	 * @param String $template_option_name The name of the option in which the template you want to get is saved.
175	 *
176	 * @return string
177	 */
178	private function get_template( $template_option_name ) {
179		$needed_option = $template_option_name . '-' . $this->post->post_type;
180
181		if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
182			return WPSEO_Options::get( $needed_option );
183		}
184
185		return '';
186	}
187
188	/**
189	 * Determines the date to be displayed in the snippet preview
190	 *
191	 * @return string
192	 */
193	private function get_metadesc_date() {
194		$date = '';
195
196		if ( $this->is_show_date_enabled() ) {
197			$date = date_i18n( 'M j, Y', mysql2date( 'U', $this->post->post_date ) );
198		}
199
200		return $date;
201	}
202
203	/**
204	 * Returns whether or not showing the date in the snippet preview is enabled.
205	 *
206	 * @return bool
207	 */
208	private function is_show_date_enabled() {
209		$key = sprintf( 'showdate-%s', $this->post->post_type );
210
211		return WPSEO_Options::get( $key, true );
212	}
213}