| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 |
|
| 5 |
use WP_Taxonomy; |
| 6 |
use WPSEO_Admin_Editor_Specific_Replace_Vars; |
| 7 |
use WPSEO_Admin_Recommended_Replace_Vars; |
| 8 |
use WPSEO_Admin_Utils; |
| 9 |
use WPSEO_Replacevar_Editor; |
| 10 |
use WPSEO_Shortlinker; |
| 11 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 12 |
use Yoast\WP\SEO\Conditionals\Open_Graph_Conditional; |
| 13 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 14 |
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; |
| 15 |
use Yoast\WP\SEO\Presenters\Admin\Badge_Presenter; |
| 16 |
use Yoast\WP\SEO\Presenters\Admin\Premium_Badge_Presenter; |
| 17 |
use Yoast_Form; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Social_Templates_Integration. |
| 21 |
* |
| 22 |
* Adds the social fields to the meta tabs for post types, taxonomies and archives. |
| 23 |
*/ |
| 24 |
class Social_Templates_Integration implements Integration_Interface { |
| 25 |
|
| 26 |
/** |
| 27 |
* Service that can be used to recommend a set of variables for a WPSEO_Replacevar_Editor. |
| 28 |
* |
| 29 |
* @var WPSEO_Admin_Recommended_Replace_Vars |
| 30 |
*/ |
| 31 |
private $recommended_replace_vars; |
| 32 |
|
| 33 |
/** |
| 34 |
* Service that can be used to recommend an editor specific set of variables for a WPSEO_Replacevar_Editor. |
| 35 |
* |
| 36 |
* @var WPSEO_Admin_Editor_Specific_Replace_Vars |
| 37 |
*/ |
| 38 |
private $editor_specific_replace_vars; |
| 39 |
|
| 40 |
/** |
| 41 |
* Group to which the 'New' badges belong to. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
private $group = 'global-templates'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns the conditionals based in which this loadable should be active. |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public static function get_conditionals() { |
| 53 |
return [ Admin_Conditional::class, Open_Graph_Conditional::class ]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Initializes the integration. |
| 58 |
*/ |
| 59 |
public function register_hooks() { |
| 60 |
\add_action( 'Yoast\WP\SEO\admin_author_archives_meta', [ $this, 'social_author_archives' ] ); |
| 61 |
\add_action( 'Yoast\WP\SEO\admin_date_archives_meta', [ $this, 'social_date_archives' ] ); |
| 62 |
\add_action( 'Yoast\WP\SEO\admin_post_types_beforearchive', [ $this, 'social_post_type' ], \PHP_INT_MAX, 2 ); |
| 63 |
\add_action( 'Yoast\WP\SEO\admin_post_types_archive', [ $this, 'social_post_types_archive' ], 10, 2 ); |
| 64 |
\add_action( 'Yoast\WP\SEO\admin_taxonomies_meta', [ $this, 'social_taxonomies' ], 10, 2 ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the recommended replacements variables object, creating it if needed. |
| 69 |
* |
| 70 |
* @return WPSEO_Admin_Recommended_Replace_Vars |
| 71 |
*/ |
| 72 |
protected function get_admin_recommended_replace_vars() { |
| 73 |
if ( \is_null( $this->recommended_replace_vars ) ) { |
| 74 |
$this->recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars(); |
| 75 |
} |
| 76 |
|
| 77 |
return $this->recommended_replace_vars; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the editor specific replacements variables object, creating it if needed. |
| 82 |
* |
| 83 |
* @return WPSEO_Admin_Editor_Specific_Replace_Vars |
| 84 |
*/ |
| 85 |
protected function get_admin_editor_specific_replace_vars() { |
| 86 |
if ( \is_null( $this->editor_specific_replace_vars ) ) { |
| 87 |
$this->editor_specific_replace_vars = new WPSEO_Admin_Editor_Specific_Replace_Vars(); |
| 88 |
} |
| 89 |
|
| 90 |
return $this->editor_specific_replace_vars; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Build a set of social fields for the author archives in the Search Appearance section. |
| 95 |
* |
| 96 |
* @param Yoast_Form $yform The form builder. |
| 97 |
*/ |
| 98 |
public function social_author_archives( $yform ) { |
| 99 |
$identifier = 'author-wpseo'; |
| 100 |
$page_type_recommended = $this->get_admin_recommended_replace_vars()->determine_for_archive( 'author' ); |
| 101 |
$page_type_specific = $this->get_admin_editor_specific_replace_vars()->determine_for_archive( 'author' ); |
| 102 |
|
| 103 |
$this->build_social_fields( $yform, $identifier, $page_type_recommended, $page_type_specific ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Build a set of social fields for the date archives in the Search Appearance section. |
| 108 |
* |
| 109 |
* @param Yoast_Form $yform The form builder. |
| 110 |
*/ |
| 111 |
public function social_date_archives( $yform ) { |
| 112 |
$identifier = 'archive-wpseo'; |
| 113 |
$page_type_recommended = $this->get_admin_recommended_replace_vars()->determine_for_archive( 'date' ); |
| 114 |
$page_type_specific = $this->get_admin_editor_specific_replace_vars()->determine_for_archive( 'date' ); |
| 115 |
|
| 116 |
$this->build_social_fields( $yform, $identifier, $page_type_recommended, $page_type_specific ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Build a set of social fields for the post types in the Search Appearance section. |
| 121 |
* |
| 122 |
* @param Yoast_Form $yform The form builder. |
| 123 |
* @param string $post_type_name The name of the current post_type that gets the social fields added. |
| 124 |
*/ |
| 125 |
public function social_post_type( $yform, $post_type_name ) { |
| 126 |
if ( $post_type_name === 'attachment' ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$page_type_recommended = $this->get_admin_recommended_replace_vars()->determine_for_post_type( $post_type_name ); |
| 131 |
$page_type_specific = $this->get_admin_editor_specific_replace_vars()->determine_for_post_type( $post_type_name ); |
| 132 |
|
| 133 |
$this->build_social_fields( $yform, $post_type_name, $page_type_recommended, $page_type_specific ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Build a set of social fields for the post types archives in the Search Appearance section. |
| 138 |
* |
| 139 |
* @param Yoast_Form $yform The form builder. |
| 140 |
* @param string $post_type_name The name of the current post_type that gets the social fields added. |
| 141 |
*/ |
| 142 |
public function social_post_types_archive( $yform, $post_type_name ) { |
| 143 |
$identifier = 'ptarchive-' . $post_type_name; |
| 144 |
$page_type_recommended = $this->get_admin_recommended_replace_vars()->determine_for_archive( $post_type_name ); |
| 145 |
$page_type_specific = $this->get_admin_editor_specific_replace_vars()->determine_for_archive( $post_type_name ); |
| 146 |
|
| 147 |
$this->build_social_fields( $yform, $identifier, $page_type_recommended, $page_type_specific ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Build a set of social fields for the taxonomies in the Search Appearance section. |
| 152 |
* |
| 153 |
* @param Yoast_Form $yform The form builder. |
| 154 |
* @param WP_Taxonomy $taxonomy The taxonomy that gets the social fields added. |
| 155 |
*/ |
| 156 |
public function social_taxonomies( $yform, $taxonomy ) { |
| 157 |
$identifier = 'tax-' . $taxonomy->name; |
| 158 |
$page_type_recommended = $this->get_admin_recommended_replace_vars()->determine_for_term( $taxonomy->name ); |
| 159 |
$page_type_specific = $this->get_admin_editor_specific_replace_vars()->determine_for_term( $taxonomy->name ); |
| 160 |
|
| 161 |
$this->build_social_fields( $yform, $identifier, $page_type_recommended, $page_type_specific ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Build a set of social fields for the Search Appearance section. |
| 166 |
* |
| 167 |
* @param Yoast_Form $yform The form builder. |
| 168 |
* @param string $identifier A page-wide unique identifier for data storage and unique DOM elements. |
| 169 |
* @param string $page_type_recommended Recommended type of page for a list of replaceable variables. |
| 170 |
* @param string $page_type_specific Editor specific type of page for a list of replaceable variables. |
| 171 |
*/ |
| 172 |
protected function build_social_fields( Yoast_Form $yform, $identifier, $page_type_recommended, $page_type_specific ) { |
| 173 |
$image_url_field_id = 'social-image-url-' . $identifier; |
| 174 |
$image_id_field_id = 'social-image-id-' . $identifier; |
| 175 |
$is_premium = \YoastSEO()->helpers->product->is_premium(); |
| 176 |
$is_premium_16_5_or_up = \defined( '\WPSEO_PREMIUM_VERSION' ) && \version_compare( \WPSEO_PREMIUM_VERSION, '16.5-RC0', '>=' ); |
| 177 |
$is_form_enabled = $is_premium && $is_premium_16_5_or_up; |
| 178 |
|
| 179 |
$section_class = 'yoast-settings-section'; |
| 180 |
|
| 181 |
if ( ! $is_form_enabled ) { |
| 182 |
$section_class .= ' yoast-settings-section-disabled'; |
| 183 |
} |
| 184 |
|
| 185 |
\printf( '<div class="%s">', \esc_attr( $section_class ) ); |
| 186 |
|
| 187 |
echo '<div class="social-settings-heading-container">'; |
| 188 |
echo '<h3 class="social-settings-heading">' . \esc_html__( 'Social settings', 'wordpress-seo' ) . '</h3>'; |
| 189 |
if ( $is_form_enabled ) { |
| 190 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Is correctly escaped in the Premium_Badge_Presenter. |
| 191 |
echo new Premium_Badge_Presenter( 'global-templates-' . $identifier ); |
| 192 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Is correctly escaped in the Badge_Presenter. |
| 193 |
echo new Badge_Presenter( 'global-templates-' . $identifier, '', $this->group ); |
| 194 |
} |
| 195 |
echo '</div>'; |
| 196 |
|
| 197 |
$yform->hidden( $image_url_field_id, $image_url_field_id ); |
| 198 |
$yform->hidden( $image_id_field_id, $image_id_field_id ); |
| 199 |
\printf( |
| 200 |
'<div |
| 201 |
id="%1$s" |
| 202 |
data-react-image-portal |
| 203 |
data-react-image-portal-target-image="%2$s" |
| 204 |
data-react-image-portal-target-image-id="%3$s" |
| 205 |
data-react-image-portal-is-disabled="%4$s" |
| 206 |
data-react-image-portal-has-image-validation="%5$s" |
| 207 |
></div>', |
| 208 |
\esc_attr( 'yoast-social-' . $identifier . '-image-select' ), |
| 209 |
\esc_attr( $image_url_field_id ), |
| 210 |
\esc_attr( $image_id_field_id ), |
| 211 |
\esc_attr( ! $is_form_enabled ), |
| 212 |
true |
| 213 |
); |
| 214 |
|
| 215 |
$editor = new WPSEO_Replacevar_Editor( |
| 216 |
$yform, |
| 217 |
[ |
| 218 |
'title' => 'social-title-' . $identifier, |
| 219 |
'description' => 'social-description-' . $identifier, |
| 220 |
'page_type_recommended' => $page_type_recommended, |
| 221 |
'page_type_specific' => $page_type_specific, |
| 222 |
'paper_style' => false, |
| 223 |
'label_title' => \__( 'Social title', 'wordpress-seo' ), |
| 224 |
'label_description' => \__( 'Social description', 'wordpress-seo' ), |
| 225 |
'description_placeholder' => '', |
| 226 |
'is_disabled' => ! $is_form_enabled, |
| 227 |
] |
| 228 |
); |
| 229 |
$editor->render(); |
| 230 |
|
| 231 |
if ( $is_premium && ! $is_premium_16_5_or_up ) { |
| 232 |
echo '<div class="yoast-settings-section-upsell">'; |
| 233 |
|
| 234 |
$unlock_alert = \sprintf( |
| 235 |
/* translators: %s expands to 'Yoast SEO Premium'. */ |
| 236 |
\esc_html__( 'To unlock this feature please update %s to the latest version.', 'wordpress-seo' ), |
| 237 |
'Yoast SEO Premium' |
| 238 |
); |
| 239 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped above. |
| 240 |
echo new Alert_Presenter( $unlock_alert ); |
| 241 |
|
| 242 |
echo '</div>'; |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! $is_premium ) { |
| 246 |
$wpseo_page = \filter_input( \INPUT_GET, 'page' ); |
| 247 |
|
| 248 |
echo '<div class="yoast-settings-section-upsell">'; |
| 249 |
|
| 250 |
echo '<a class="yoast-button-upsell" href="' . \esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/4e0' ) ) . '" target="_blank">' |
| 251 |
. \esc_html__( 'Unlock with Premium', 'wordpress-seo' ) |
| 252 |
// phpcs:ignore WordPress.Security.EscapeOutput -- Already escapes correctly. |
| 253 |
. WPSEO_Admin_Utils::get_new_tab_message() |
| 254 |
. '<span aria-hidden="true" class="yoast-button-upsell__caret"></span>' |
| 255 |
. '</a>'; |
| 256 |
echo '</div>'; |
| 257 |
} |
| 258 |
|
| 259 |
echo '</div>'; |
| 260 |
} |
| 261 |
} |
| 262 |
|