| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Formatter |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This class provides data for the term metabox by return its values for localization. |
| 10 |
*/ |
| 11 |
class WPSEO_Term_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface { |
| 12 |
|
| 13 |
/** |
| 14 |
* The term the metabox formatter is for. |
| 15 |
* |
| 16 |
* @var WP_Term|stdClass |
| 17 |
*/ |
| 18 |
private $term; |
| 19 |
|
| 20 |
/** |
| 21 |
* The term's taxonomy. |
| 22 |
* |
| 23 |
* @var stdClass |
| 24 |
*/ |
| 25 |
private $taxonomy; |
| 26 |
|
| 27 |
/** |
| 28 |
* Whether we must return social templates values. |
| 29 |
* |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
private $use_social_templates = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Array with the WPSEO_Titles options. |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $options; |
| 40 |
|
| 41 |
/** |
| 42 |
* WPSEO_Taxonomy_Scraper constructor. |
| 43 |
* |
| 44 |
* @param stdClass $taxonomy Taxonomy. |
| 45 |
* @param WP_Term|stdClass $term Term. |
| 46 |
*/ |
| 47 |
public function __construct( $taxonomy, $term ) { |
| 48 |
$this->taxonomy = $taxonomy; |
| 49 |
$this->term = $term; |
| 50 |
|
| 51 |
$this->use_social_templates = $this->use_social_templates(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Determines whether the social templates should be used. |
| 56 |
* |
| 57 |
* @return bool Whether the social templates should be used. |
| 58 |
*/ |
| 59 |
public function use_social_templates() { |
| 60 |
return YoastSEO()->helpers->product->is_premium() |
| 61 |
&& defined( 'WPSEO_PREMIUM_VERSION' ) |
| 62 |
&& version_compare( WPSEO_PREMIUM_VERSION, '16.5-RC0', '>=' ) |
| 63 |
&& WPSEO_Options::get( 'opengraph', false ) === true; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns the translated values. |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function get_values() { |
| 72 |
$values = []; |
| 73 |
|
| 74 |
// Todo: a column needs to be added on the termpages to add a filter for the keyword, so this can be used in the focus keyphrase doubles. |
| 75 |
if ( is_object( $this->term ) && property_exists( $this->term, 'taxonomy' ) ) { |
| 76 |
$values = [ |
| 77 |
'search_url' => $this->search_url(), |
| 78 |
'post_edit_url' => $this->edit_url(), |
| 79 |
'base_url' => $this->base_url_for_js(), |
| 80 |
'taxonomy' => $this->term->taxonomy, |
| 81 |
'keyword_usage' => $this->get_focus_keyword_usage(), |
| 82 |
'title_template' => $this->get_title_template(), |
| 83 |
'title_template_no_fallback' => $this->get_title_template( false ), |
| 84 |
'metadesc_template' => $this->get_metadesc_template(), |
| 85 |
'first_content_image' => $this->get_image_url(), |
| 86 |
'semrushIntegrationActive' => 0, |
| 87 |
'social_title_template' => $this->get_social_title_template(), |
| 88 |
'social_description_template' => $this->get_social_description_template(), |
| 89 |
'social_image_template' => $this->get_social_image_template(), |
| 90 |
'wincherIntegrationActive' => 0, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
return $values; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Gets the image URL for the term's social preview. |
| 99 |
* |
| 100 |
* @return string|null The image URL for the social preview. |
| 101 |
*/ |
| 102 |
protected function get_image_url() { |
| 103 |
return WPSEO_Image_Utils::get_first_content_image_for_term( $this->term->term_id ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns the url to search for keyword for the taxonomy. |
| 108 |
* |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
private function search_url() { |
| 112 |
return admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns the url to edit the taxonomy. |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
private function edit_url() { |
| 121 |
global $wp_version; |
| 122 |
$script_filename = version_compare( $wp_version, '4.5', '<' ) ? 'edit-tags' : 'term'; |
| 123 |
return admin_url( $script_filename . '.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns a base URL for use in the JS, takes permalink structure into account. |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
private function base_url_for_js() { |
| 132 |
|
| 133 |
$base_url = home_url( '/', null ); |
| 134 |
if ( ! WPSEO_Options::get( 'stripcategorybase', false ) ) { |
| 135 |
$base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] ); |
| 136 |
} |
| 137 |
|
| 138 |
return $base_url; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Counting the number of given keyword used for other term than given term_id. |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
private function get_focus_keyword_usage() { |
| 147 |
$focuskw = WPSEO_Taxonomy_Meta::get_term_meta( $this->term, $this->term->taxonomy, 'focuskw' ); |
| 148 |
|
| 149 |
return WPSEO_Taxonomy_Meta::get_keyword_usage( $focuskw, $this->term->term_id, $this->term->taxonomy ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Retrieves the title template. |
| 154 |
* |
| 155 |
* @param bool $fallback Whether to return the hardcoded fallback if the template value is empty. |
| 156 |
* |
| 157 |
* @return string The title template. |
| 158 |
*/ |
| 159 |
private function get_title_template( $fallback = true ) { |
| 160 |
$title = $this->get_template( 'title' ); |
| 161 |
|
| 162 |
if ( $title === '' && $fallback === true ) { |
| 163 |
/* translators: %s expands to the variable used for term title. */ |
| 164 |
$archives = sprintf( __( '%s Archives', 'wordpress-seo' ), '%%term_title%%' ); |
| 165 |
return $archives . ' %%page%% %%sep%% %%sitename%%'; |
| 166 |
} |
| 167 |
|
| 168 |
return $title; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Retrieves the metadesc template. |
| 173 |
* |
| 174 |
* @return string The metadesc template. |
| 175 |
*/ |
| 176 |
private function get_metadesc_template() { |
| 177 |
return $this->get_template( 'metadesc' ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Retrieves the social title template. |
| 182 |
* |
| 183 |
* @return string The social title template. |
| 184 |
*/ |
| 185 |
private function get_social_title_template() { |
| 186 |
if ( $this->use_social_templates ) { |
| 187 |
return $this->get_template( 'social-title' ); |
| 188 |
} |
| 189 |
|
| 190 |
return ''; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Retrieves the social description template. |
| 195 |
* |
| 196 |
* @return string The social description template. |
| 197 |
*/ |
| 198 |
private function get_social_description_template() { |
| 199 |
if ( $this->use_social_templates ) { |
| 200 |
return $this->get_template( 'social-description' ); |
| 201 |
} |
| 202 |
|
| 203 |
return ''; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Retrieves the social image template. |
| 208 |
* |
| 209 |
* @return string The social description template. |
| 210 |
*/ |
| 211 |
private function get_social_image_template() { |
| 212 |
if ( $this->use_social_templates ) { |
| 213 |
return $this->get_template( 'social-image-url' ); |
| 214 |
} |
| 215 |
|
| 216 |
return ''; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Retrieves a template. |
| 221 |
* |
| 222 |
* @param string $template_option_name The name of the option in which the template you want to get is saved. |
| 223 |
* |
| 224 |
* @return string |
| 225 |
*/ |
| 226 |
private function get_template( $template_option_name ) { |
| 227 |
$needed_option = $template_option_name . '-tax-' . $this->term->taxonomy; |
| 228 |
return WPSEO_Options::get( $needed_option, '' ); |
| 229 |
} |
| 230 |
} |
| 231 |
|