class-metabox.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class generates the metabox on the edit post / page as well as contains all page analysis functionality.
  9. */
  10. class WPSEO_Metabox extends WPSEO_Meta {
  11. /**
  12. * @var WPSEO_Social_Admin
  13. */
  14. protected $social_admin;
  15. /**
  16. * @var WPSEO_Metabox_Analysis_SEO
  17. */
  18. protected $analysis_seo;
  19. /**
  20. * @var WPSEO_Metabox_Analysis_Readability
  21. */
  22. protected $analysis_readability;
  23. /**
  24. * Class constructor.
  25. */
  26. public function __construct() {
  27. add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
  28. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
  29. add_action( 'wp_insert_post', array( $this, 'save_postdata' ) );
  30. add_action( 'edit_attachment', array( $this, 'save_postdata' ) );
  31. add_action( 'add_attachment', array( $this, 'save_postdata' ) );
  32. add_action( 'post_submitbox_start', array( $this, 'publish_box' ) );
  33. add_action( 'admin_init', array( $this, 'setup_page_analysis' ) );
  34. add_action( 'admin_init', array( $this, 'translate_meta_boxes' ) );
  35. // Check if one of the social settings is checked in the options, if so, initialize the social_admin object.
  36. if ( WPSEO_Options::get( 'opengraph', false ) || WPSEO_Options::get( 'twitter', false ) ) {
  37. $this->social_admin = new WPSEO_Social_Admin();
  38. }
  39. $this->editor = new WPSEO_Metabox_Editor();
  40. $this->editor->register_hooks();
  41. $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  42. $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  43. }
  44. /**
  45. * Translate text strings for use in the meta box.
  46. *
  47. * IMPORTANT: if you want to add a new string (option) somewhere, make sure you add that array key to
  48. * the main meta box definition array in the class WPSEO_Meta() as well!!!!
  49. */
  50. public static function translate_meta_boxes() {
  51. self::$meta_fields['general']['title']['title'] = __( 'SEO title', 'wordpress-seo' );
  52. self::$meta_fields['general']['metadesc']['title'] = __( 'Meta description', 'wordpress-seo' );
  53. /* translators: %s expands to the post type name. */
  54. self::$meta_fields['advanced']['meta-robots-noindex']['title'] = __( 'Allow search engines to show this %s in search results?', 'wordpress-seo' );
  55. if ( '0' === (string) get_option( 'blog_public' ) ) {
  56. self::$meta_fields['advanced']['meta-robots-noindex']['description'] = '<p class="error-message">' . __( 'Warning: even though you can set the meta robots setting here, the entire site is set to noindex in the sitewide privacy settings, so these settings won\'t have an effect.', 'wordpress-seo' ) . '</p>';
  57. }
  58. /* translators: %1$s expands to Yes or No, %2$s expands to the post type name.*/
  59. self::$meta_fields['advanced']['meta-robots-noindex']['options']['0'] = __( 'Default for %2$s, currently: %1$s', 'wordpress-seo' );
  60. self::$meta_fields['advanced']['meta-robots-noindex']['options']['2'] = __( 'Yes', 'wordpress-seo' );
  61. self::$meta_fields['advanced']['meta-robots-noindex']['options']['1'] = __( 'No', 'wordpress-seo' );
  62. /* translators: %1$s expands to the post type name.*/
  63. self::$meta_fields['advanced']['meta-robots-nofollow']['title'] = __( 'Should search engines follow links on this %1$s?', 'wordpress-seo' );
  64. self::$meta_fields['advanced']['meta-robots-nofollow']['options']['0'] = __( 'Yes', 'wordpress-seo' );
  65. self::$meta_fields['advanced']['meta-robots-nofollow']['options']['1'] = __( 'No', 'wordpress-seo' );
  66. self::$meta_fields['advanced']['meta-robots-adv']['title'] = __( 'Meta robots advanced', 'wordpress-seo' );
  67. self::$meta_fields['advanced']['meta-robots-adv']['description'] = __( 'Advanced <code>meta</code> robots settings for this page.', 'wordpress-seo' );
  68. /* translators: %s expands to the advanced robots settings default as set in the site-wide settings.*/
  69. self::$meta_fields['advanced']['meta-robots-adv']['options']['-'] = __( 'Site-wide default: %s', 'wordpress-seo' );
  70. self::$meta_fields['advanced']['meta-robots-adv']['options']['none'] = __( 'None', 'wordpress-seo' );
  71. self::$meta_fields['advanced']['meta-robots-adv']['options']['noimageindex'] = __( 'No Image Index', 'wordpress-seo' );
  72. self::$meta_fields['advanced']['meta-robots-adv']['options']['noarchive'] = __( 'No Archive', 'wordpress-seo' );
  73. self::$meta_fields['advanced']['meta-robots-adv']['options']['nosnippet'] = __( 'No Snippet', 'wordpress-seo' );
  74. self::$meta_fields['advanced']['bctitle']['title'] = __( 'Breadcrumbs Title', 'wordpress-seo' );
  75. self::$meta_fields['advanced']['bctitle']['description'] = __( 'Title to use for this page in breadcrumb paths', 'wordpress-seo' );
  76. self::$meta_fields['advanced']['canonical']['title'] = __( 'Canonical URL', 'wordpress-seo' );
  77. /* translators: 1: link open tag; 2: link close tag. */
  78. self::$meta_fields['advanced']['canonical']['description'] = sprintf( __( 'The canonical URL that this page should point to, leave empty to default to permalink. %1$sCross domain canonical%2$s supported too.', 'wordpress-seo' ), '<a target="_blank" href="http://googlewebmastercentral.blogspot.com/2009/12/handling-legitimate-cross-domain.html">', '</a>' );
  79. self::$meta_fields['advanced']['redirect']['title'] = __( '301 Redirect', 'wordpress-seo' );
  80. self::$meta_fields['advanced']['redirect']['description'] = __( 'The URL that this page should redirect to.', 'wordpress-seo' );
  81. do_action( 'wpseo_tab_translate' );
  82. }
  83. /**
  84. * Determines whether the metabox should be shown for the passed identifier.
  85. *
  86. * By default the check is done for post types, but can also be used for taxonomies.
  87. *
  88. * @param string|null $identifier The identifier to check.
  89. * @param string $type The type of object to check. Defaults to post_type.
  90. *
  91. * @return bool Whether or not the metabox should be displayed.
  92. */
  93. public function display_metabox( $identifier = null, $type = 'post_type' ) {
  94. return WPSEO_Utils::is_metabox_active( $identifier, $type );
  95. }
  96. /**
  97. * Sets up all the functionality related to the prominence of the page analysis functionality.
  98. */
  99. public function setup_page_analysis() {
  100. if ( apply_filters( 'wpseo_use_page_analysis', true ) === true ) {
  101. add_action( 'post_submitbox_start', array( $this, 'publish_box' ) );
  102. }
  103. }
  104. /**
  105. * Outputs the page analysis score in the Publish Box.
  106. */
  107. public function publish_box() {
  108. if ( $this->display_metabox() === false ) {
  109. return;
  110. }
  111. $post = $this->get_metabox_post();
  112. if ( self::get_value( 'meta-robots-noindex', $post->ID ) === '1' ) {
  113. $score_label = 'noindex';
  114. $title = __( 'Post is set to noindex.', 'wordpress-seo' );
  115. $score_title = $title;
  116. }
  117. else {
  118. $score = self::get_value( 'linkdex', $post->ID );
  119. if ( $score === '' ) {
  120. $score_label = 'na';
  121. $title = __( 'No focus keyword set.', 'wordpress-seo' );
  122. }
  123. else {
  124. $score_label = WPSEO_Utils::translate_score( $score );
  125. }
  126. $score_title = WPSEO_Utils::translate_score( $score, false );
  127. if ( ! isset( $title ) ) {
  128. $title = $score_title;
  129. }
  130. }
  131. }
  132. /**
  133. * Adds the Yoast SEO meta box to the edit boxes in the edit post, page,
  134. * attachment, and custom post types pages.
  135. *
  136. * @return void
  137. */
  138. public function add_meta_box() {
  139. $post_types = WPSEO_Post_Type::get_accessible_post_types();
  140. if ( ! is_array( $post_types ) || $post_types === array() ) {
  141. return;
  142. }
  143. $tab_registered = false;
  144. foreach ( $post_types as $post_type ) {
  145. if ( $this->display_metabox( $post_type ) === false ) {
  146. continue;
  147. }
  148. $product_title = 'Yoast SEO';
  149. if ( file_exists( WPSEO_PATH . 'premium/' ) ) {
  150. $product_title .= ' Premium';
  151. }
  152. if ( get_current_screen() !== null ) {
  153. $screen_id = get_current_screen()->id;
  154. add_filter( "postbox_classes_{$screen_id}_wpseo_meta", array( $this, 'wpseo_metabox_class' ) );
  155. }
  156. if ( ! $tab_registered ) {
  157. // Add template variables tab to the Help Center.
  158. $tab = new WPSEO_Help_Center_Template_Variables_Tab();
  159. $tab->register_hooks();
  160. $tab_registered = true;
  161. }
  162. add_meta_box( 'wpseo_meta', $product_title, array(
  163. $this,
  164. 'meta_box',
  165. ), $post_type, 'normal', apply_filters( 'wpseo_metabox_prio', 'high' ) );
  166. }
  167. }
  168. /**
  169. * Adds CSS classes to the meta box.
  170. *
  171. * @param array $classes An array of postbox CSS classes.
  172. *
  173. * @return array List of classes that will be applied to the editbox container.
  174. */
  175. public function wpseo_metabox_class( $classes ) {
  176. $classes[] = 'yoast wpseo-metabox';
  177. return $classes;
  178. }
  179. /**
  180. * Pass variables to js for use with the post-scraper.
  181. *
  182. * @return array
  183. */
  184. public function localize_post_scraper_script() {
  185. $post = $this->get_metabox_post();
  186. $permalink = '';
  187. if ( is_object( $post ) ) {
  188. $permalink = get_sample_permalink( $post->ID );
  189. $permalink = $permalink[0];
  190. }
  191. $post_formatter = new WPSEO_Metabox_Formatter(
  192. new WPSEO_Post_Metabox_Formatter( $post, array(), $permalink )
  193. );
  194. return $post_formatter->get_values();
  195. }
  196. /**
  197. * Pass some variables to js for replacing variables.
  198. */
  199. public function localize_replace_vars_script() {
  200. return array(
  201. 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
  202. 'replace_vars' => $this->get_replace_vars(),
  203. 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
  204. 'scope' => $this->determine_scope(),
  205. );
  206. }
  207. /**
  208. * Determines the scope based on the post type.
  209. * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
  210. *
  211. * @return string String decribing the current scope.
  212. */
  213. private function determine_scope() {
  214. $post_type = get_post_type( $this->get_metabox_post() );
  215. if ( $post_type === 'page' ) {
  216. return 'page';
  217. }
  218. return 'post';
  219. }
  220. /**
  221. * Pass some variables to js for the edit / post page overview, etc.
  222. *
  223. * @return array
  224. */
  225. public function localize_shortcode_plugin_script() {
  226. return array(
  227. 'wpseo_filter_shortcodes_nonce' => wp_create_nonce( 'wpseo-filter-shortcodes' ),
  228. 'wpseo_shortcode_tags' => $this->get_valid_shortcode_tags(),
  229. );
  230. }
  231. /**
  232. * Output a tab in the Yoast SEO Metabox.
  233. *
  234. * @param string $id CSS ID of the tab.
  235. * @param string $heading Heading for the tab.
  236. * @param string $content Content of the tab. This content should be escaped.
  237. */
  238. public function do_tab( $id, $heading, $content ) {
  239. ?>
  240. <div id="<?php echo esc_attr( 'wpseo_' . $id ); ?>" class="wpseotab <?php echo esc_attr( $id ); ?>">
  241. <?php echo $content; ?>
  242. </div>
  243. <?php
  244. }
  245. /**
  246. * Output the meta box.
  247. */
  248. public function meta_box() {
  249. $content_sections = $this->get_content_sections();
  250. $helpcenter_tab = new WPSEO_Option_Tab( 'metabox', __( 'Meta box', 'wordpress-seo' ),
  251. array( 'video_url' => WPSEO_Shortlinker::get( 'https://yoa.st/metabox-screencast' ) ) );
  252. $help_center = new WPSEO_Help_Center( '', $helpcenter_tab, WPSEO_Utils::is_yoast_seo_premium() );
  253. $help_center->localize_data();
  254. $help_center->mount();
  255. if ( ! defined( 'WPSEO_PREMIUM_FILE' ) ) {
  256. echo $this->get_buy_premium_link();
  257. }
  258. echo '<div class="wpseo-metabox-content">';
  259. echo '<div class="wpseo-metabox-sidebar"><ul>';
  260. foreach ( $content_sections as $content_section ) {
  261. if ( $content_section->name === 'premium' ) {
  262. continue;
  263. }
  264. $content_section->display_link();
  265. }
  266. echo '</ul></div>';
  267. foreach ( $content_sections as $content_section ) {
  268. $content_section->display_content();
  269. }
  270. echo '</div>';
  271. }
  272. /**
  273. * Determines whether the React section should be rendered.
  274. *
  275. * @param string $section_name The name of the section.
  276. *
  277. * @return bool Whether the React section should be rendered.
  278. */
  279. private function should_load_react_section( $section_name ) {
  280. return $section_name === 'content';
  281. }
  282. /**
  283. * Returns the relevant metabox sections for the current view.
  284. *
  285. * @return WPSEO_Metabox_Section[]
  286. */
  287. private function get_content_sections() {
  288. $content_sections = array();
  289. $content_sections[] = $this->get_content_meta_section();
  290. // Check if social_admin is an instance of WPSEO_Social_Admin.
  291. if ( $this->social_admin instanceof WPSEO_Social_Admin ) {
  292. $content_sections[] = $this->social_admin->get_meta_section();
  293. }
  294. if ( WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta' ) === false ) {
  295. $content_sections[] = $this->get_advanced_meta_section();
  296. }
  297. if ( has_action( 'wpseo_tab_header' ) || has_action( 'wpseo_tab_content' ) ) {
  298. $content_sections[] = $this->get_addons_meta_section();
  299. }
  300. return $content_sections;
  301. }
  302. /**
  303. * Returns the metabox section for the content analysis.
  304. *
  305. * @return WPSEO_Metabox_Section
  306. */
  307. private function get_content_meta_section() {
  308. $content = $this->get_tab_content( 'general' );
  309. /**
  310. * Filter: 'wpseo_content_meta_section_content' - Allow filtering the metabox content before outputting.
  311. *
  312. * @api string $post_content The metabox content string.
  313. */
  314. $content = apply_filters( 'wpseo_content_meta_section_content', $content );
  315. return new WPSEO_Metabox_Section_React(
  316. 'content',
  317. '<span class="screen-reader-text">' . __( 'Content optimization', 'wordpress-seo' ) . '</span><span class="yst-traffic-light-container">' . WPSEO_Utils::traffic_light_svg() . '</span>',
  318. $content,
  319. array(
  320. 'link_aria_label' => __( 'Content optimization', 'wordpress-seo' ),
  321. 'link_class' => 'yoast-tooltip yoast-tooltip-e',
  322. )
  323. );
  324. }
  325. /**
  326. * Returns the metabox section for the advanced settings.
  327. *
  328. * @return WPSEO_Metabox_Section
  329. */
  330. private function get_advanced_meta_section() {
  331. $content = $this->get_tab_content( 'advanced' );
  332. $tab = new WPSEO_Metabox_Form_Tab(
  333. 'advanced',
  334. $content,
  335. __( 'Advanced', 'wordpress-seo' ),
  336. array(
  337. 'single' => true,
  338. )
  339. );
  340. return new WPSEO_Metabox_Tab_Section(
  341. 'advanced',
  342. '<span class="screen-reader-text">' . __( 'Advanced', 'wordpress-seo' ) . '</span><span class="dashicons dashicons-admin-generic"></span>',
  343. array( $tab ),
  344. array(
  345. 'link_aria_label' => __( 'Advanced', 'wordpress-seo' ),
  346. 'link_class' => 'yoast-tooltip yoast-tooltip-e',
  347. )
  348. );
  349. }
  350. /**
  351. * Returns a link to activate the Buy Premium tab.
  352. *
  353. * @return string
  354. */
  355. private function get_buy_premium_link() {
  356. return sprintf( '<div class="%1$s"><a target="_blank" rel="noopener noreferrer" href="%2$s"><span class="dashicons dashicons-star-filled wpseo-buy-premium"></span>%3$s</a></div>',
  357. 'wpseo-metabox-buy-premium',
  358. esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/pe-premium-page' ) ),
  359. __( 'Go Premium', 'wordpress-seo' )
  360. );
  361. }
  362. /**
  363. * Returns a metabox section dedicated to hosting metabox tabs that have been added by other plugins through the
  364. * `wpseo_tab_header` and `wpseo_tab_content` actions.
  365. *
  366. * @return WPSEO_Metabox_Section
  367. */
  368. private function get_addons_meta_section() {
  369. return new WPSEO_Metabox_Addon_Tab_Section(
  370. 'addons',
  371. '<span class="screen-reader-text">' . __( 'Add-ons', 'wordpress-seo' ) . '</span><span class="dashicons dashicons-admin-plugins"></span>',
  372. array(),
  373. array(
  374. 'link_aria_label' => __( 'Add-ons', 'wordpress-seo' ),
  375. 'link_class' => 'yoast-tooltip yoast-tooltip-e',
  376. )
  377. );
  378. }
  379. /**
  380. * Retrieves the contents for the metabox tab.
  381. *
  382. * @param string $tab_name Tab for which to retrieve the field definitions.
  383. *
  384. * @return string
  385. */
  386. private function get_tab_content( $tab_name ) {
  387. $content = '';
  388. foreach ( $this->get_meta_field_defs( $tab_name ) as $key => $meta_field ) {
  389. $content .= $this->do_meta_box( $meta_field, $key );
  390. }
  391. return $content;
  392. }
  393. /**
  394. * Retrieves the hidden fields for the metabox tab.
  395. *
  396. * @param string $tab_name Tab for which to retrieve the field definitions.
  397. *
  398. * @return string
  399. */
  400. private function get_hidden_tab_fields( $tab_name ) {
  401. $hidden_fields = '';
  402. foreach ( $this->get_meta_field_defs( $tab_name ) as $key => $meta_field ) {
  403. if ( $meta_field['type'] !== 'hidden' ) {
  404. continue;
  405. }
  406. $hidden_fields .= $this->do_meta_box( $meta_field, $key );
  407. }
  408. return $hidden_fields;
  409. }
  410. /**
  411. * Adds a line in the meta box.
  412. *
  413. * @todo [JRF] Check if $class is added appropriately everywhere.
  414. *
  415. * @param array $meta_field_def Contains the vars based on which output is generated.
  416. * @param string $key Internal key (without prefix).
  417. *
  418. * @return string
  419. */
  420. public function do_meta_box( $meta_field_def, $key = '' ) {
  421. $content = '';
  422. $esc_form_key = esc_attr( self::$form_prefix . $key );
  423. $meta_value = self::get_value( $key, $this->get_metabox_post()->ID );
  424. $class = '';
  425. if ( isset( $meta_field_def['class'] ) && $meta_field_def['class'] !== '' ) {
  426. $class = ' ' . $meta_field_def['class'];
  427. }
  428. $placeholder = '';
  429. if ( isset( $meta_field_def['placeholder'] ) && $meta_field_def['placeholder'] !== '' ) {
  430. $placeholder = $meta_field_def['placeholder'];
  431. }
  432. $aria_describedby = '';
  433. $description = '';
  434. if ( isset( $meta_field_def['description'] ) ) {
  435. $aria_describedby = ' aria-describedby="' . $esc_form_key . '-desc"';
  436. $description = '<p id="' . $esc_form_key . '-desc" class="yoast-metabox__description">' . $meta_field_def['description'] . '</p>';
  437. }
  438. switch ( $meta_field_def['type'] ) {
  439. case 'text':
  440. $ac = '';
  441. if ( isset( $meta_field_def['autocomplete'] ) && $meta_field_def['autocomplete'] === false ) {
  442. $ac = 'autocomplete="off" ';
  443. }
  444. if ( $placeholder !== '' ) {
  445. $placeholder = ' placeholder="' . esc_attr( $placeholder ) . '"';
  446. }
  447. $content .= '<input type="text"' . $placeholder . ' id="' . $esc_form_key . '" ' . $ac . 'name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '" class="large-text' . $class . '"' . $aria_describedby . '/>';
  448. break;
  449. case 'textarea':
  450. $rows = 3;
  451. if ( isset( $meta_field_def['rows'] ) && $meta_field_def['rows'] > 0 ) {
  452. $rows = $meta_field_def['rows'];
  453. }
  454. $content .= '<textarea class="large-text' . $class . '" rows="' . esc_attr( $rows ) . '" id="' . $esc_form_key . '" name="' . $esc_form_key . '"' . $aria_describedby . '>' . esc_textarea( $meta_value ) . '</textarea>';
  455. break;
  456. case 'hidden':
  457. $content .= '<input type="hidden" id="' . $esc_form_key . '" name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '"/>' . "\n";
  458. break;
  459. case 'select':
  460. if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== array() ) {
  461. $content .= '<select name="' . $esc_form_key . '" id="' . $esc_form_key . '" class="yoast' . $class . '">';
  462. foreach ( $meta_field_def['options'] as $val => $option ) {
  463. $selected = selected( $meta_value, $val, false );
  464. $content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
  465. }
  466. unset( $val, $option, $selected );
  467. $content .= '</select>';
  468. }
  469. break;
  470. case 'multiselect':
  471. if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== array() ) {
  472. // Set $meta_value as $selected_arr.
  473. $selected_arr = $meta_value;
  474. // If the multiselect field is 'meta-robots-adv' we should explode on ,.
  475. if ( 'meta-robots-adv' === $key ) {
  476. $selected_arr = explode( ',', $meta_value );
  477. }
  478. if ( ! is_array( $selected_arr ) ) {
  479. $selected_arr = (array) $selected_arr;
  480. }
  481. $options_count = count( $meta_field_def['options'] );
  482. // This select now uses Select2.
  483. $content .= '<select multiple="multiple" size="' . esc_attr( $options_count ) . '" name="' . $esc_form_key . '[]" id="' . $esc_form_key . '" class="yoast' . $class . '"' . $aria_describedby . '>';
  484. foreach ( $meta_field_def['options'] as $val => $option ) {
  485. $selected = '';
  486. if ( in_array( $val, $selected_arr ) ) {
  487. $selected = ' selected="selected"';
  488. }
  489. $content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
  490. }
  491. $content .= '</select>';
  492. unset( $val, $option, $selected, $selected_arr, $options_count );
  493. }
  494. break;
  495. case 'checkbox':
  496. $checked = checked( $meta_value, 'on', false );
  497. $expl = ( isset( $meta_field_def['expl'] ) ) ? esc_html( $meta_field_def['expl'] ) : '';
  498. $content .= '<input type="checkbox" id="' . $esc_form_key . '" name="' . $esc_form_key . '" ' . $checked . ' value="on" class="yoast' . $class . '"' . $aria_describedby . '/> <label for="' . $esc_form_key . '">' . $expl . '</label>';
  499. unset( $checked, $expl );
  500. break;
  501. case 'radio':
  502. if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== array() ) {
  503. foreach ( $meta_field_def['options'] as $val => $option ) {
  504. $checked = checked( $meta_value, $val, false );
  505. $content .= '<input type="radio" ' . $checked . ' id="' . $esc_form_key . '_' . esc_attr( $val ) . '" name="' . $esc_form_key . '" value="' . esc_attr( $val ) . '"/> <label for="' . $esc_form_key . '_' . esc_attr( $val ) . '">' . esc_html( $option ) . '</label> ';
  506. }
  507. unset( $val, $option, $checked );
  508. }
  509. break;
  510. case 'upload':
  511. $content .= '<input id="' . $esc_form_key . '" type="text" size="36" class="' . $class . '" name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '"' . $aria_describedby . ' />';
  512. $content .= '<input id="' . $esc_form_key . '_button" class="wpseo_image_upload_button button" type="button" value="' . esc_attr__( 'Upload Image', 'wordpress-seo' ) . '" />';
  513. break;
  514. }
  515. $html = '';
  516. if ( $content === '' ) {
  517. $content = apply_filters( 'wpseo_do_meta_box_field_' . $key, $content, $meta_value, $esc_form_key, $meta_field_def, $key );
  518. }
  519. if ( $content !== '' ) {
  520. $title = esc_html( $meta_field_def['title'] );
  521. // By default, use the field title as a label element.
  522. $label = '<label for="' . $esc_form_key . '">' . $title . '</label>';
  523. // Set the inline help and help panel, if any.
  524. $help_button = '';
  525. $help_panel = '';
  526. if ( isset( $meta_field_def['help'] ) && $meta_field_def['help'] !== '' ) {
  527. $help = new WPSEO_Admin_Help_Panel( $key, $meta_field_def['help-button'], $meta_field_def['help'] );
  528. $help_button = $help->get_button_html();
  529. $help_panel = $help->get_panel_html();
  530. }
  531. // If it's a set of radio buttons, output proper fieldset and legend.
  532. if ( 'radio' === $meta_field_def['type'] ) {
  533. return '<fieldset><legend>' . $title . '</legend>' . $help_button . $help_panel . $content . $description . '</fieldset>';
  534. }
  535. // If it's a single checkbox, ignore the title.
  536. if ( 'checkbox' === $meta_field_def['type'] ) {
  537. $label = '';
  538. }
  539. // Other meta box content or form fields.
  540. if ( $meta_field_def['type'] === 'hidden' ) {
  541. $html = $content;
  542. }
  543. else {
  544. $html = $label . $help_button . $help_panel . $content . $description;
  545. }
  546. }
  547. return $html;
  548. }
  549. /**
  550. * Save the WP SEO metadata for posts.
  551. *
  552. * {@internal $_POST parameters are validated via sanitize_post_meta().}}
  553. *
  554. * @param int $post_id Post ID.
  555. *
  556. * @return bool|void Boolean false if invalid save post request.
  557. */
  558. public function save_postdata( $post_id ) {
  559. // Bail if this is a multisite installation and the site has been switched.
  560. if ( is_multisite() && ms_is_switched() ) {
  561. return false;
  562. }
  563. if ( $post_id === null ) {
  564. return false;
  565. }
  566. if ( wp_is_post_revision( $post_id ) ) {
  567. $post_id = wp_is_post_revision( $post_id );
  568. }
  569. /**
  570. * Determine we're not accidentally updating a different post.
  571. * We can't use filter_input here as the ID isn't available at this point, other than in the $_POST data.
  572. */
  573. // @codingStandardsIgnoreStart
  574. if ( ! isset( $_POST['ID'] ) || $post_id !== (int) $_POST['ID'] ) {
  575. return false;
  576. }
  577. // @codingStandardsIgnoreEnd
  578. clean_post_cache( $post_id );
  579. $post = get_post( $post_id );
  580. if ( ! is_object( $post ) ) {
  581. // Non-existent post.
  582. return false;
  583. }
  584. do_action( 'wpseo_save_compare_data', $post );
  585. $meta_boxes = apply_filters( 'wpseo_save_metaboxes', array() );
  586. $meta_boxes = array_merge( $meta_boxes, $this->get_meta_field_defs( 'general', $post->post_type ), $this->get_meta_field_defs( 'advanced' ) );
  587. foreach ( $meta_boxes as $key => $meta_box ) {
  588. // If analysis is disabled remove that analysis score value from the DB.
  589. if ( $this->is_meta_value_disabled( $key ) ) {
  590. self::delete( $key, $post_id );
  591. continue;
  592. }
  593. $data = null;
  594. if ( 'checkbox' === $meta_box['type'] ) {
  595. // @codingStandardsIgnoreLine
  596. $data = isset( $_POST[ self::$form_prefix . $key ] ) ? 'on' : 'off';
  597. }
  598. else {
  599. // @codingStandardsIgnoreLine
  600. if ( isset( $_POST[ self::$form_prefix . $key ] ) ) {
  601. // @codingStandardsIgnoreLine
  602. $data = $_POST[ self::$form_prefix . $key ];
  603. }
  604. }
  605. if ( isset( $data ) ) {
  606. self::set_value( $key, $data, $post_id );
  607. }
  608. }
  609. do_action( 'wpseo_saved_postdata' );
  610. }
  611. /**
  612. * Determines if the given meta value key is disabled.
  613. *
  614. * @param string $key The key of the meta value.
  615. *
  616. * @return bool Whether the given meta value key is disabled.
  617. */
  618. public function is_meta_value_disabled( $key ) {
  619. if ( 'linkdex' === $key && ! $this->analysis_seo->is_enabled() ) {
  620. return true;
  621. }
  622. if ( 'content_score' === $key && ! $this->analysis_readability->is_enabled() ) {
  623. return true;
  624. }
  625. return false;
  626. }
  627. /**
  628. * Enqueues all the needed JS and CSS.
  629. *
  630. * @todo [JRF => whomever] Create css/metabox-mp6.css file and add it to the below allowed colors array when done.
  631. */
  632. public function enqueue() {
  633. global $pagenow;
  634. $asset_manager = new WPSEO_Admin_Asset_Manager();
  635. $is_editor = self::is_post_overview( $pagenow ) || self::is_post_edit( $pagenow );
  636. /* Filter 'wpseo_always_register_metaboxes_on_admin' documented in wpseo-main.php */
  637. if ( ( $is_editor === false && apply_filters( 'wpseo_always_register_metaboxes_on_admin', false ) === false ) || $this->display_metabox() === false ) {
  638. return;
  639. }
  640. if ( self::is_post_overview( $pagenow ) ) {
  641. $asset_manager->enqueue_style( 'edit-page' );
  642. $asset_manager->enqueue_script( 'edit-page-script' );
  643. return;
  644. }
  645. if ( get_queried_object_id() !== 0 ) {
  646. // Enqueue files needed for upload functionality.
  647. wp_enqueue_media( array( 'post' => get_queried_object_id() ) );
  648. }
  649. $asset_manager->enqueue_style( 'metabox-css' );
  650. $asset_manager->enqueue_style( 'scoring' );
  651. $asset_manager->enqueue_style( 'select2' );
  652. $asset_manager->enqueue_script( 'metabox' );
  653. $asset_manager->enqueue_script( 'help-center' );
  654. $asset_manager->enqueue_script( 'admin-media' );
  655. $asset_manager->enqueue_script( 'post-scraper' );
  656. $asset_manager->enqueue_script( 'replacevar-plugin' );
  657. $asset_manager->enqueue_script( 'shortcode-plugin' );
  658. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', $this->localize_media_script() );
  659. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'post-scraper', 'wpseoPostScraperL10n', $this->localize_post_scraper_script() );
  660. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  661. $yoast_components_l10n->localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'post-scraper' );
  662. $analysis_worker_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ) );
  663. $used_keywords_assessment_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ), 'used-keywords-assessment' );
  664. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'post-scraper', 'wpseoAnalysisWorkerL10n', array(
  665. 'url' => $analysis_worker_location->get_url( $analysis_worker_location->get_asset(), WPSEO_Admin_Asset::TYPE_JS ),
  666. 'keywords_assessment_url' => $used_keywords_assessment_location->get_url( $used_keywords_assessment_location->get_asset(), WPSEO_Admin_Asset::TYPE_JS ),
  667. ) );
  668. /**
  669. * Remove the emoji script as it is incompatible with both React and any
  670. * contenteditable fields.
  671. */
  672. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  673. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'replacevar-plugin', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  674. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'shortcode-plugin', 'wpseoShortcodePluginL10n', $this->localize_shortcode_plugin_script() );
  675. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
  676. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoSelect2Locale', WPSEO_Utils::get_language( WPSEO_Utils::get_user_locale() ) );
  677. if ( post_type_supports( get_post_type(), 'thumbnail' ) ) {
  678. $asset_manager->enqueue_style( 'featured-image' );
  679. $asset_manager->enqueue_script( 'featured-image' );
  680. $featured_image_l10 = array( 'featured_image_notice' => __( 'SEO issue: The featured image should be at least 200 by 200 pixels to be picked up by Facebook and other social media sites.', 'wordpress-seo' ) );
  681. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoFeaturedImageL10n', $featured_image_l10 );
  682. }
  683. }
  684. /**
  685. * Pass some variables to js for upload module.
  686. *
  687. * @return array
  688. */
  689. public function localize_media_script() {
  690. return array(
  691. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  692. );
  693. }
  694. /**
  695. * Returns post in metabox context.
  696. *
  697. * @returns WP_Post|array
  698. */
  699. protected function get_metabox_post() {
  700. $post = filter_input( INPUT_GET, 'post' );
  701. if ( ! empty( $post ) ) {
  702. $post_id = (int) WPSEO_Utils::validate_int( $post );
  703. return get_post( $post_id );
  704. }
  705. if ( isset( $GLOBALS['post'] ) ) {
  706. return $GLOBALS['post'];
  707. }
  708. return array();
  709. }
  710. /**
  711. * Returns an array with shortcode tags for all registered shortcodes.
  712. *
  713. * @return array
  714. */
  715. private function get_valid_shortcode_tags() {
  716. $shortcode_tags = array();
  717. foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
  718. array_push( $shortcode_tags, $tag );
  719. }
  720. return $shortcode_tags;
  721. }
  722. /**
  723. * Prepares the replace vars for localization.
  724. *
  725. * @return array replace vars
  726. */
  727. private function get_replace_vars() {
  728. $post = $this->get_metabox_post();
  729. $cached_replacement_vars = array();
  730. $vars_to_cache = array(
  731. 'date',
  732. 'id',
  733. 'sitename',
  734. 'sitedesc',
  735. 'sep',
  736. 'page',
  737. 'currentyear',
  738. );
  739. foreach ( $vars_to_cache as $var ) {
  740. $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $post );
  741. }
  742. // Merge custom replace variables with the WordPress ones.
  743. return array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $post ) );
  744. }
  745. /**
  746. * Prepares the recommended replace vars for localization.
  747. *
  748. * @return array Recommended replacement variables.
  749. */
  750. private function get_recommended_replace_vars() {
  751. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  752. $post = $this->get_metabox_post();
  753. // What is recommended depends on the current context.
  754. $post_type = $recommended_replace_vars->determine_for_post( $post );
  755. return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
  756. }
  757. /**
  758. * Gets the custom replace variables for custom taxonomies and fields.
  759. *
  760. * @param WP_Post $post The post to check for custom taxonomies and fields.
  761. *
  762. * @return array Array containing all the replacement variables.
  763. */
  764. private function get_custom_replace_vars( $post ) {
  765. return array(
  766. 'custom_fields' => $this->get_custom_fields_replace_vars( $post ),
  767. 'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
  768. );
  769. }
  770. /**
  771. * Gets the custom replace variables for custom taxonomies.
  772. *
  773. * @param WP_Post $post The post to check for custom taxonomies.
  774. *
  775. * @return array Array containing all the replacement variables.
  776. */
  777. private function get_custom_taxonomies_replace_vars( $post ) {
  778. $taxonomies = get_object_taxonomies( $post, 'objects' );
  779. $custom_replace_vars = array();
  780. foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
  781. if ( is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
  782. $taxonomy_name = $taxonomy;
  783. $taxonomy = get_taxonomy( $taxonomy_name );
  784. }
  785. if ( $taxonomy->_builtin && $taxonomy->public ) {
  786. continue;
  787. }
  788. $custom_replace_vars[ $taxonomy_name ] = array(
  789. 'name' => $taxonomy->name,
  790. 'description' => $taxonomy->description,
  791. );
  792. }
  793. return $custom_replace_vars;
  794. }
  795. /**
  796. * Gets the custom replace variables for custom fields.
  797. *
  798. * @param WP_Post $post The post to check for custom fields.
  799. *
  800. * @return array Array containing all the replacement variables.
  801. */
  802. private function get_custom_fields_replace_vars( $post ) {
  803. $custom_replace_vars = array();
  804. // If no post object is passed, return the empty custom_replace_vars array.
  805. if ( ! is_object( $post ) ) {
  806. return $custom_replace_vars;
  807. }
  808. $custom_fields = get_post_custom( $post->ID );
  809. foreach ( $custom_fields as $custom_field_name => $custom_field ) {
  810. if ( substr( $custom_field_name, 0, 1 ) === '_' ) {
  811. continue;
  812. }
  813. $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
  814. }
  815. return $custom_replace_vars;
  816. }
  817. /**
  818. * @param string $page The page to check for the post overview page.
  819. *
  820. * @return bool Whether or not the given page is the post overview page.
  821. */
  822. public static function is_post_overview( $page ) {
  823. return 'edit.php' === $page;
  824. }
  825. /**
  826. * @param string $page The page to check for the post edit page.
  827. *
  828. * @return bool Whether or not the given page is the post edit page.
  829. */
  830. public static function is_post_edit( $page ) {
  831. return 'post.php' === $page
  832. || 'post-new.php' === $page;
  833. }
  834. }