class-metabox.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. * An instance of the Social Admin class.
  13. *
  14. * @var WPSEO_Social_Admin
  15. */
  16. protected $social_admin;
  17. /**
  18. * An instance of the Metabox Analysis SEO class.
  19. *
  20. * @var WPSEO_Metabox_Analysis_SEO
  21. */
  22. protected $analysis_seo;
  23. /**
  24. * An instance of the Metabox Analysis Readability class.
  25. *
  26. * @var WPSEO_Metabox_Analysis_Readability
  27. */
  28. protected $analysis_readability;
  29. /**
  30. * The metabox editor object.
  31. *
  32. * @var WPSEO_Metabox_Editor
  33. */
  34. protected $editor;
  35. /**
  36. * Class constructor.
  37. */
  38. public function __construct() {
  39. if ( $this->is_internet_explorer() ) {
  40. add_action( 'add_meta_boxes', [ $this, 'internet_explorer_metabox' ] );
  41. return;
  42. }
  43. add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
  44. add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] );
  45. add_action( 'wp_insert_post', [ $this, 'save_postdata' ] );
  46. add_action( 'edit_attachment', [ $this, 'save_postdata' ] );
  47. add_action( 'add_attachment', [ $this, 'save_postdata' ] );
  48. add_action( 'admin_init', [ $this, 'translate_meta_boxes' ] );
  49. // Check if one of the social settings is checked in the options, if so, initialize the social_admin object.
  50. if ( WPSEO_Options::get( 'opengraph', false ) || WPSEO_Options::get( 'twitter', false ) ) {
  51. $this->social_admin = new WPSEO_Social_Admin();
  52. }
  53. $this->editor = new WPSEO_Metabox_Editor();
  54. $this->editor->register_hooks();
  55. $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  56. $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  57. }
  58. /**
  59. * Checks whether the request comes from an IE 11 browser.
  60. *
  61. * @return bool Whether the request comes from an IE 11 browser.
  62. */
  63. public static function is_internet_explorer() {
  64. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  65. if ( ! stripos( $user_agent, 'Trident/7.0' ) ) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * Adds an alternative metabox for internet explorer users.
  72. */
  73. public function internet_explorer_metabox() {
  74. $post_types = WPSEO_Post_Type::get_accessible_post_types();
  75. $post_types = array_filter( $post_types, [ $this, 'display_metabox' ] );
  76. if ( ! is_array( $post_types ) || $post_types === [] ) {
  77. return;
  78. }
  79. $product_title = $this->get_product_title();
  80. foreach ( $post_types as $post_type ) {
  81. add_filter( "postbox_classes_{$post_type}_wpseo_meta", [ $this, 'wpseo_metabox_class' ] );
  82. add_meta_box(
  83. 'wpseo_meta',
  84. $product_title,
  85. [ $this, 'render_internet_explorer_notice' ],
  86. $post_type,
  87. 'normal',
  88. apply_filters( 'wpseo_metabox_prio', 'high' ),
  89. [ '__block_editor_compatible_meta_box' => true ]
  90. );
  91. }
  92. }
  93. /**
  94. * Renders the content for the internet explorer metabox.
  95. */
  96. public function render_internet_explorer_notice() {
  97. echo '<div class="yoast-alert-box yoast-alert-box__warning">';
  98. echo '<span class="icon">';
  99. echo '<svg xmlns="http://www.w3.org/2000/svg" fill="#674E00" height="14px" width="14px" viewBox="0 0 576 512" role="img" aria-hidden="true" focusable="false"><path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"/></svg>';
  100. echo '</span>';
  101. echo '<div style="float: left">';
  102. printf(
  103. esc_html__( 'The browser you are currently using is unfortunately rather dated. Since we strive to give you the best experience possible, we no longer support this browser. Instead, please use %1$sFirefox%4$s, %2$sChrome%4$s or %3$sMicrosoft Edge%4$s.', 'wordpress-seo' ),
  104. '<a href="https://www.mozilla.org/firefox/new/">',
  105. '<a href="https://www.google.com/intl/nl/chrome/">',
  106. '<a href="https://www.microsoft.com/windows/microsoft-edge">',
  107. '</a>'
  108. );
  109. echo '</div></div>';
  110. }
  111. /**
  112. * Translates text strings for use in the meta box.
  113. *
  114. * IMPORTANT: if you want to add a new string (option) somewhere, make sure you add that array key to
  115. * the main meta box definition array in the class WPSEO_Meta() as well!!!!
  116. */
  117. public static function translate_meta_boxes() {
  118. WPSEO_Meta::$meta_fields['general']['title']['title'] = __( 'SEO title', 'wordpress-seo' );
  119. WPSEO_Meta::$meta_fields['general']['metadesc']['title'] = __( 'Meta description', 'wordpress-seo' );
  120. /* translators: %s expands to the post type name. */
  121. WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['title'] = __( 'Allow search engines to show this %s in search results?', 'wordpress-seo' );
  122. if ( '0' === (string) get_option( 'blog_public' ) ) {
  123. WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['description'] = '<span 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' ) . '</span>';
  124. }
  125. /* translators: %1$s expands to Yes or No, %2$s expands to the post type name.*/
  126. WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['0'] = __( 'Default for %2$s, currently: %1$s', 'wordpress-seo' );
  127. WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['2'] = __( 'Yes', 'wordpress-seo' );
  128. WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['1'] = __( 'No', 'wordpress-seo' );
  129. /* translators: %1$s expands to the post type name.*/
  130. WPSEO_Meta::$meta_fields['advanced']['meta-robots-nofollow']['title'] = __( 'Should search engines follow links on this %1$s?', 'wordpress-seo' );
  131. WPSEO_Meta::$meta_fields['advanced']['meta-robots-nofollow']['options']['0'] = __( 'Yes', 'wordpress-seo' );
  132. WPSEO_Meta::$meta_fields['advanced']['meta-robots-nofollow']['options']['1'] = __( 'No', 'wordpress-seo' );
  133. WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['title'] = __( 'Meta robots advanced', 'wordpress-seo' );
  134. WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['description'] = __( 'If you want to apply advanced <code>meta</code> robots settings for this page, please define them in the following field.', 'wordpress-seo' );
  135. WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['options']['noimageindex'] = __( 'No Image Index', 'wordpress-seo' );
  136. WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['options']['noarchive'] = __( 'No Archive', 'wordpress-seo' );
  137. WPSEO_Meta::$meta_fields['advanced']['meta-robots-adv']['options']['nosnippet'] = __( 'No Snippet', 'wordpress-seo' );
  138. WPSEO_Meta::$meta_fields['advanced']['bctitle']['title'] = __( 'Breadcrumbs Title', 'wordpress-seo' );
  139. WPSEO_Meta::$meta_fields['advanced']['bctitle']['description'] = __( 'Title to use for this page in breadcrumb paths', 'wordpress-seo' );
  140. WPSEO_Meta::$meta_fields['advanced']['canonical']['title'] = __( 'Canonical URL', 'wordpress-seo' );
  141. WPSEO_Meta::$meta_fields['advanced']['canonical']['description'] = sprintf(
  142. /* translators: 1: link open tag; 2: link close tag. */
  143. __( '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' ),
  144. '<a href="https://googlewebmastercentral.blogspot.com/2009/12/handling-legitimate-cross-domain.html" target="_blank" rel="noopener">',
  145. WPSEO_Admin_Utils::get_new_tab_message() . '</a>'
  146. );
  147. WPSEO_Meta::$meta_fields['advanced']['redirect']['title'] = __( '301 Redirect', 'wordpress-seo' );
  148. WPSEO_Meta::$meta_fields['advanced']['redirect']['description'] = __( 'The URL that this page should redirect to.', 'wordpress-seo' );
  149. do_action( 'wpseo_tab_translate' );
  150. }
  151. /**
  152. * Determines whether the metabox should be shown for the passed identifier.
  153. *
  154. * By default the check is done for post types, but can also be used for taxonomies.
  155. *
  156. * @param string|null $identifier The identifier to check.
  157. * @param string $type The type of object to check. Defaults to post_type.
  158. *
  159. * @return bool Whether or not the metabox should be displayed.
  160. */
  161. public function display_metabox( $identifier = null, $type = 'post_type' ) {
  162. return WPSEO_Utils::is_metabox_active( $identifier, $type );
  163. }
  164. /**
  165. * Adds the Yoast SEO meta box to the edit boxes in the edit post, page,
  166. * attachment, and custom post types pages.
  167. *
  168. * @return void
  169. */
  170. public function add_meta_box() {
  171. $post_types = WPSEO_Post_Type::get_accessible_post_types();
  172. $post_types = array_filter( $post_types, [ $this, 'display_metabox' ] );
  173. if ( ! is_array( $post_types ) || $post_types === [] ) {
  174. return;
  175. }
  176. $product_title = $this->get_product_title();
  177. foreach ( $post_types as $post_type ) {
  178. add_filter( "postbox_classes_{$post_type}_wpseo_meta", [ $this, 'wpseo_metabox_class' ] );
  179. add_meta_box(
  180. 'wpseo_meta',
  181. $product_title,
  182. [ $this, 'meta_box' ],
  183. $post_type,
  184. 'normal',
  185. apply_filters( 'wpseo_metabox_prio', 'high' ),
  186. [ '__block_editor_compatible_meta_box' => true ]
  187. );
  188. }
  189. }
  190. /**
  191. * Adds CSS classes to the meta box.
  192. *
  193. * @param array $classes An array of postbox CSS classes.
  194. *
  195. * @return array List of classes that will be applied to the editbox container.
  196. */
  197. public function wpseo_metabox_class( $classes ) {
  198. $classes[] = 'yoast wpseo-metabox';
  199. return $classes;
  200. }
  201. /**
  202. * Passes variables to js for use with the post-scraper.
  203. *
  204. * @return array
  205. */
  206. public function localize_post_scraper_script() {
  207. $post = $this->get_metabox_post();
  208. $permalink = '';
  209. if ( is_object( $post ) ) {
  210. $permalink = get_sample_permalink( $post->ID );
  211. $permalink = $permalink[0];
  212. }
  213. $post_formatter = new WPSEO_Metabox_Formatter(
  214. new WPSEO_Post_Metabox_Formatter( $post, [], $permalink )
  215. );
  216. $values = $post_formatter->get_values();
  217. /** This filter is documented in admin/filters/class-cornerstone-filter.php. */
  218. $post_types = apply_filters( 'wpseo_cornerstone_post_types', WPSEO_Post_Type::get_accessible_post_types() );
  219. if ( $values['cornerstoneActive'] && ! in_array( $post->post_type, $post_types, true ) ) {
  220. $values['cornerstoneActive'] = false;
  221. }
  222. return $values;
  223. }
  224. /**
  225. * Passes some variables to js for replacing variables.
  226. */
  227. public function localize_replace_vars_script() {
  228. return [
  229. 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
  230. 'replace_vars' => $this->get_replace_vars(),
  231. 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
  232. 'scope' => $this->determine_scope(),
  233. 'has_taxonomies' => $this->current_post_type_has_taxonomies(),
  234. ];
  235. }
  236. /**
  237. * Determines whether or not the current post type has registered taxonomies.
  238. *
  239. * @return bool Whether the current post type has taxonomies.
  240. */
  241. private function current_post_type_has_taxonomies() {
  242. $post_taxonomies = get_object_taxonomies( get_post_type() );
  243. return ! empty( $post_taxonomies );
  244. }
  245. /**
  246. * Determines the scope based on the post type.
  247. * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
  248. *
  249. * @return string String describing the current scope.
  250. */
  251. private function determine_scope() {
  252. $post_type = get_post_type( $this->get_metabox_post() );
  253. if ( $post_type === 'page' ) {
  254. return 'page';
  255. }
  256. return 'post';
  257. }
  258. /**
  259. * Passes some variables to js for the edit / post page overview, etc.
  260. *
  261. * @return array
  262. */
  263. public function localize_shortcode_plugin_script() {
  264. return [
  265. 'wpseo_filter_shortcodes_nonce' => wp_create_nonce( 'wpseo-filter-shortcodes' ),
  266. 'wpseo_shortcode_tags' => $this->get_valid_shortcode_tags(),
  267. ];
  268. }
  269. /**
  270. * Outputs the meta box.
  271. */
  272. public function meta_box() {
  273. $content_sections = $this->get_content_sections();
  274. echo '<div class="wpseo-metabox-content">';
  275. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $this->get_product_title is considered safe.
  276. printf( '<div class="wpseo-metabox-menu"><ul role="tablist" class="yoast-aria-tabs" aria-label="%s">', $this->get_product_title() );
  277. foreach ( $content_sections as $content_section ) {
  278. if ( $content_section->name === 'premium' ) {
  279. continue;
  280. }
  281. $content_section->display_link();
  282. }
  283. echo '</ul></div>';
  284. foreach ( $content_sections as $content_section ) {
  285. $content_section->display_content();
  286. }
  287. echo '</div>';
  288. }
  289. /**
  290. * Returns the relevant metabox sections for the current view.
  291. *
  292. * @return WPSEO_Metabox_Section[]
  293. */
  294. private function get_content_sections() {
  295. $content_sections = [];
  296. $content_sections[] = $this->get_seo_meta_section();
  297. if ( $this->analysis_readability->is_enabled() ) {
  298. $content_sections[] = $this->get_readability_meta_section();
  299. }
  300. // Check if social_admin is an instance of WPSEO_Social_Admin.
  301. if ( $this->social_admin instanceof WPSEO_Social_Admin ) {
  302. $content_sections[] = $this->social_admin->get_meta_section();
  303. }
  304. $content_sections = array_merge( $content_sections, $this->get_additional_meta_sections() );
  305. return $content_sections;
  306. }
  307. /**
  308. * Returns the metabox section for the seo analysis.
  309. *
  310. * @return WPSEO_Metabox_Section
  311. */
  312. private function get_seo_meta_section() {
  313. wp_nonce_field( 'yoast_free_metabox', 'yoast_free_metabox_nonce' );
  314. $content = $this->get_tab_content( 'general' );
  315. $label = __( 'SEO', 'wordpress-seo' );
  316. if ( $this->analysis_seo->is_enabled() ) {
  317. $label = '<span class="wpseo-score-icon-container" id="wpseo-seo-score-icon"></span>' . $label;
  318. }
  319. $html_after = '';
  320. if ( WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta' ) === false ) {
  321. $advanced_collapsible = new WPSEO_Paper_Presenter(
  322. __( 'Advanced', 'wordpress-seo' ),
  323. null,
  324. [
  325. 'collapsible' => true,
  326. 'class' => 'metabox wpseo-form wpseo-collapsible-container',
  327. 'content' => $this->get_tab_content( 'advanced' ),
  328. 'paper_id' => 'collapsible-advanced-settings',
  329. ]
  330. );
  331. $html_after = '<div class="wpseo_content_wrapper">' . $advanced_collapsible->get_output() . '</div>';
  332. }
  333. /**
  334. * Filter: 'wpseo_content_meta_section_content' - Allow filtering the metabox content before outputting.
  335. *
  336. * @api string $post_content The metabox content string.
  337. */
  338. $content = apply_filters( 'wpseo_content_meta_section_content', $content );
  339. return new WPSEO_Metabox_Section_React(
  340. 'content',
  341. $label,
  342. $content,
  343. [
  344. 'html_after' => $html_after,
  345. ]
  346. );
  347. }
  348. /**
  349. * Returns the metabox section for the readability analysis.
  350. *
  351. * @return WPSEO_Metabox_Section
  352. */
  353. private function get_readability_meta_section() {
  354. return new WPSEO_Metabox_Section_Readability();
  355. }
  356. /**
  357. * Returns the metabox sections that have been added by other plugins.
  358. *
  359. * @return WPSEO_Metabox_Section_Additional[]
  360. */
  361. protected function get_additional_meta_sections() {
  362. $sections = [];
  363. /**
  364. * Private filter: 'yoast_free_additional_metabox_sections'.
  365. *
  366. * Meant for internal use only. Allows adding additional tabs to the Yoast SEO metabox.
  367. *
  368. * @since 11.9
  369. *
  370. * @param array[] $sections {
  371. * An array of arrays with tab specifications.
  372. *
  373. * @type array $section {
  374. * A tab specification.
  375. *
  376. * @type string $name The name of the tab. Used in the HTML IDs, href and aria properties.
  377. * @type string $link_content The content of the tab link.
  378. * @type string $content The content of the tab.
  379. * @type array $options {
  380. * Optional. Extra options.
  381. *
  382. * @type string $link_class Optional. The class for the tab link.
  383. * @type string $link_aria_label Optional. The aria label of the tab link.
  384. * }
  385. * }
  386. * }
  387. */
  388. $requested_sections = apply_filters( 'yoast_free_additional_metabox_sections', [] );
  389. foreach ( $requested_sections as $section ) {
  390. if ( is_array( $section ) && array_key_exists( 'name', $section ) && array_key_exists( 'link_content', $section ) && array_key_exists( 'content', $section ) ) {
  391. $options = array_key_exists( 'options', $section ) ? $section['options'] : [];
  392. $sections[] = new WPSEO_Metabox_Section_Additional(
  393. $section['name'],
  394. $section['link_content'],
  395. $section['content'],
  396. $options
  397. );
  398. }
  399. }
  400. return $sections;
  401. }
  402. /**
  403. * Retrieves the contents for the metabox tab.
  404. *
  405. * @param string $tab_name Tab for which to retrieve the field definitions.
  406. *
  407. * @return string
  408. */
  409. private function get_tab_content( $tab_name ) {
  410. $content = '';
  411. foreach ( WPSEO_Meta::get_meta_field_defs( $tab_name ) as $key => $meta_field ) {
  412. $content .= $this->do_meta_box( $meta_field, $key );
  413. }
  414. return $content;
  415. }
  416. /**
  417. * Adds a line in the meta box.
  418. *
  419. * @todo [JRF] Check if $class is added appropriately everywhere.
  420. *
  421. * @param array $meta_field_def Contains the vars based on which output is generated.
  422. * @param string $key Internal key (without prefix).
  423. *
  424. * @return string
  425. */
  426. public function do_meta_box( $meta_field_def, $key = '' ) {
  427. $content = '';
  428. $esc_form_key = esc_attr( WPSEO_Meta::$form_prefix . $key );
  429. $meta_value = WPSEO_Meta::get_value( $key, $this->get_metabox_post()->ID );
  430. $class = '';
  431. if ( isset( $meta_field_def['class'] ) && $meta_field_def['class'] !== '' ) {
  432. $class = ' ' . $meta_field_def['class'];
  433. }
  434. $placeholder = '';
  435. if ( isset( $meta_field_def['placeholder'] ) && $meta_field_def['placeholder'] !== '' ) {
  436. $placeholder = $meta_field_def['placeholder'];
  437. }
  438. $aria_describedby = '';
  439. $description = '';
  440. if ( isset( $meta_field_def['description'] ) ) {
  441. $aria_describedby = ' aria-describedby="' . $esc_form_key . '-desc"';
  442. $description = '<p id="' . $esc_form_key . '-desc" class="yoast-metabox__description">' . $meta_field_def['description'] . '</p>';
  443. }
  444. switch ( $meta_field_def['type'] ) {
  445. case 'text':
  446. $ac = '';
  447. if ( isset( $meta_field_def['autocomplete'] ) && $meta_field_def['autocomplete'] === false ) {
  448. $ac = 'autocomplete="off" ';
  449. }
  450. if ( $placeholder !== '' ) {
  451. $placeholder = ' placeholder="' . esc_attr( $placeholder ) . '"';
  452. }
  453. $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 . '/>';
  454. break;
  455. case 'textarea':
  456. $rows = 3;
  457. if ( isset( $meta_field_def['rows'] ) && $meta_field_def['rows'] > 0 ) {
  458. $rows = $meta_field_def['rows'];
  459. }
  460. $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>';
  461. break;
  462. case 'hidden':
  463. $content .= '<input type="hidden" id="' . $esc_form_key . '" name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '"/>' . "\n";
  464. break;
  465. case 'select':
  466. if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
  467. $content .= '<select name="' . $esc_form_key . '" id="' . $esc_form_key . '" class="yoast' . $class . '">';
  468. foreach ( $meta_field_def['options'] as $val => $option ) {
  469. $selected = selected( $meta_value, $val, false );
  470. $content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
  471. }
  472. unset( $val, $option, $selected );
  473. $content .= '</select>';
  474. }
  475. break;
  476. case 'multiselect':
  477. if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
  478. // Set $meta_value as $selected_arr.
  479. $selected_arr = $meta_value;
  480. // If the multiselect field is 'meta-robots-adv' we should explode on ,.
  481. if ( 'meta-robots-adv' === $key ) {
  482. $selected_arr = explode( ',', $meta_value );
  483. }
  484. if ( ! is_array( $selected_arr ) ) {
  485. $selected_arr = (array) $selected_arr;
  486. }
  487. $options_count = count( $meta_field_def['options'] );
  488. // This select now uses Select2.
  489. $content .= '<select multiple="multiple" size="' . esc_attr( $options_count ) . '" name="' . $esc_form_key . '[]" id="' . $esc_form_key . '" class="yoast' . $class . '"' . $aria_describedby . '>';
  490. foreach ( $meta_field_def['options'] as $val => $option ) {
  491. $selected = '';
  492. if ( in_array( $val, $selected_arr, true ) ) {
  493. $selected = ' selected="selected"';
  494. }
  495. $content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
  496. }
  497. $content .= '</select>';
  498. unset( $val, $option, $selected, $selected_arr, $options_count );
  499. }
  500. break;
  501. case 'checkbox':
  502. $checked = checked( $meta_value, 'on', false );
  503. $expl = ( isset( $meta_field_def['expl'] ) ) ? esc_html( $meta_field_def['expl'] ) : '';
  504. $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>';
  505. unset( $checked, $expl );
  506. break;
  507. case 'radio':
  508. if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
  509. foreach ( $meta_field_def['options'] as $val => $option ) {
  510. $checked = checked( $meta_value, $val, false );
  511. $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> ';
  512. }
  513. unset( $val, $option, $checked );
  514. }
  515. break;
  516. case 'upload':
  517. $content .= '<input' .
  518. ' id="' . $esc_form_key . '"' .
  519. ' type="text"' .
  520. ' size="36"' .
  521. ' class="' . $class . '"' .
  522. ' name="' . $esc_form_key . '"' .
  523. ' value="' . esc_attr( $meta_value ) . '"' . $aria_describedby .
  524. ' readonly="readonly"' .
  525. ' /> ';
  526. $content .= '<input' .
  527. ' id="' . esc_attr( $esc_form_key ) . '_button"' .
  528. ' class="wpseo_image_upload_button button"' .
  529. ' data-target="' . esc_attr( $esc_form_key ) . '"' .
  530. ' data-target-id="' . esc_attr( $esc_form_key ) . '-id"' .
  531. ' type="button"' .
  532. ' value="' . esc_attr__( 'Upload Image', 'wordpress-seo' ) . '"' .
  533. ' /> ';
  534. $content .= '<input' .
  535. ' class="wpseo_image_remove_button button"' .
  536. ' type="button"' .
  537. ' value="' . esc_attr__( 'Clear Image', 'wordpress-seo' ) . '"' .
  538. ' />';
  539. break;
  540. }
  541. $html = '';
  542. if ( $content === '' ) {
  543. $content = apply_filters( 'wpseo_do_meta_box_field_' . $key, $content, $meta_value, $esc_form_key, $meta_field_def, $key );
  544. }
  545. if ( $content !== '' ) {
  546. $title = esc_html( $meta_field_def['title'] );
  547. // By default, use the field title as a label element.
  548. $label = '<label for="' . $esc_form_key . '">' . $title . '</label>';
  549. // Set the inline help and help panel, if any.
  550. $help_button = '';
  551. $help_panel = '';
  552. if ( isset( $meta_field_def['help'] ) && $meta_field_def['help'] !== '' ) {
  553. $help = new WPSEO_Admin_Help_Panel( $key, $meta_field_def['help-button'], $meta_field_def['help'] );
  554. $help_button = $help->get_button_html();
  555. $help_panel = $help->get_panel_html();
  556. }
  557. // If it's a set of radio buttons, output proper fieldset and legend.
  558. if ( 'radio' === $meta_field_def['type'] ) {
  559. return '<fieldset><legend>' . $title . '</legend>' . $help_button . $help_panel . $content . $description . '</fieldset>';
  560. }
  561. // If it's a single checkbox, ignore the title.
  562. if ( 'checkbox' === $meta_field_def['type'] ) {
  563. $label = '';
  564. }
  565. // Other meta box content or form fields.
  566. if ( $meta_field_def['type'] === 'hidden' ) {
  567. $html = $content;
  568. }
  569. else {
  570. $html = $label . $description . $help_button . $help_panel . $content;
  571. }
  572. }
  573. return $html;
  574. }
  575. /**
  576. * Saves the WP SEO metadata for posts.
  577. *
  578. * {@internal $_POST parameters are validated via sanitize_post_meta().}}
  579. *
  580. * @param int $post_id Post ID.
  581. *
  582. * @return bool|void Boolean false if invalid save post request.
  583. */
  584. public function save_postdata( $post_id ) {
  585. // Bail if this is a multisite installation and the site has been switched.
  586. if ( is_multisite() && ms_is_switched() ) {
  587. return false;
  588. }
  589. if ( $post_id === null ) {
  590. return false;
  591. }
  592. if ( ! isset( $_POST['yoast_free_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['yoast_free_metabox_nonce'], 'yoast_free_metabox' ) ) {
  593. return false;
  594. }
  595. if ( wp_is_post_revision( $post_id ) ) {
  596. $post_id = wp_is_post_revision( $post_id );
  597. }
  598. /**
  599. * Determine we're not accidentally updating a different post.
  600. * We can't use filter_input here as the ID isn't available at this point, other than in the $_POST data.
  601. */
  602. if ( ! isset( $_POST['ID'] ) || $post_id !== (int) $_POST['ID'] ) {
  603. return false;
  604. }
  605. clean_post_cache( $post_id );
  606. $post = get_post( $post_id );
  607. if ( ! is_object( $post ) ) {
  608. // Non-existent post.
  609. return false;
  610. }
  611. do_action( 'wpseo_save_compare_data', $post );
  612. $meta_boxes = apply_filters( 'wpseo_save_metaboxes', [] );
  613. $meta_boxes = array_merge( $meta_boxes, WPSEO_Meta::get_meta_field_defs( 'general', $post->post_type ), WPSEO_Meta::get_meta_field_defs( 'advanced' ) );
  614. foreach ( $meta_boxes as $key => $meta_box ) {
  615. // If analysis is disabled remove that analysis score value from the DB.
  616. if ( $this->is_meta_value_disabled( $key ) ) {
  617. WPSEO_Meta::delete( $key, $post_id );
  618. continue;
  619. }
  620. $data = null;
  621. $field_name = WPSEO_Meta::$form_prefix . $key;
  622. if ( 'checkbox' === $meta_box['type'] ) {
  623. $data = isset( $_POST[ $field_name ] ) ? 'on' : 'off';
  624. }
  625. else {
  626. if ( isset( $_POST[ $field_name ] ) ) {
  627. $data = wp_unslash( $_POST[ $field_name ] );
  628. // For multi-select.
  629. if ( is_array( $data ) ) {
  630. $data = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], $data );
  631. }
  632. if ( is_string( $data ) ) {
  633. $data = WPSEO_Utils::sanitize_text_field( $data );
  634. }
  635. }
  636. // Reset options when no entry is present with multiselect - only applies to `meta-robots-adv` currently.
  637. if ( ! isset( $_POST[ $field_name ] ) && ( $meta_box['type'] === 'multiselect' ) ) {
  638. $data = [];
  639. }
  640. }
  641. if ( $data !== null ) {
  642. WPSEO_Meta::set_value( $key, $data, $post_id );
  643. }
  644. }
  645. do_action( 'wpseo_saved_postdata' );
  646. }
  647. /**
  648. * Determines if the given meta value key is disabled.
  649. *
  650. * @param string $key The key of the meta value.
  651. *
  652. * @return bool Whether the given meta value key is disabled.
  653. */
  654. public function is_meta_value_disabled( $key ) {
  655. if ( 'linkdex' === $key && ! $this->analysis_seo->is_enabled() ) {
  656. return true;
  657. }
  658. if ( 'content_score' === $key && ! $this->analysis_readability->is_enabled() ) {
  659. return true;
  660. }
  661. return false;
  662. }
  663. /**
  664. * Enqueues all the needed JS and CSS.
  665. *
  666. * @todo [JRF => whomever] Create css/metabox-mp6.css file and add it to the below allowed colors array when done.
  667. */
  668. public function enqueue() {
  669. global $pagenow;
  670. $asset_manager = new WPSEO_Admin_Asset_Manager();
  671. $is_editor = self::is_post_overview( $pagenow ) || self::is_post_edit( $pagenow );
  672. if ( self::is_post_overview( $pagenow ) ) {
  673. $asset_manager->enqueue_style( 'edit-page' );
  674. $asset_manager->enqueue_script( 'edit-page-script' );
  675. return;
  676. }
  677. /* Filter 'wpseo_always_register_metaboxes_on_admin' documented in wpseo-main.php */
  678. if ( ( $is_editor === false && apply_filters( 'wpseo_always_register_metaboxes_on_admin', false ) === false ) || $this->display_metabox() === false ) {
  679. return;
  680. }
  681. $post_id = get_queried_object_id();
  682. if ( empty( $post_id ) && isset( $_GET['post'] ) ) {
  683. $post_id = sanitize_text_field( $_GET['post'] );
  684. }
  685. if ( $post_id !== 0 ) {
  686. // Enqueue files needed for upload functionality.
  687. wp_enqueue_media( [ 'post' => $post_id ] );
  688. }
  689. $asset_manager->enqueue_style( 'metabox-css' );
  690. $asset_manager->enqueue_style( 'scoring' );
  691. $asset_manager->enqueue_style( 'select2' );
  692. $asset_manager->enqueue_script( 'metabox' );
  693. $asset_manager->enqueue_script( 'admin-media' );
  694. $asset_manager->enqueue_script( 'post-scraper' );
  695. $asset_manager->enqueue_script( 'replacevar-plugin' );
  696. $asset_manager->enqueue_script( 'shortcode-plugin' );
  697. $asset_manager->enqueue_script( 'admin-script' );
  698. $asset_manager->enqueue_style( 'admin-css' );
  699. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', $this->localize_media_script() );
  700. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'post-scraper', 'wpseoPostScraperL10n', $this->localize_post_scraper_script() );
  701. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  702. $yoast_components_l10n->localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'post-scraper' );
  703. $analysis_worker_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ) );
  704. $used_keywords_assessment_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ), 'used-keywords-assessment' );
  705. $localization_data = [
  706. 'url' => $analysis_worker_location->get_url( $analysis_worker_location->get_asset(), WPSEO_Admin_Asset::TYPE_JS ),
  707. 'keywords_assessment_url' => $used_keywords_assessment_location->get_url( $used_keywords_assessment_location->get_asset(), WPSEO_Admin_Asset::TYPE_JS ),
  708. 'log_level' => WPSEO_Utils::get_analysis_worker_log_level(),
  709. // We need to make the feature flags separately available inside of the analysis web worker.
  710. 'enabled_features' => WPSEO_Utils::retrieve_enabled_features(),
  711. ];
  712. wp_localize_script(
  713. WPSEO_Admin_Asset_Manager::PREFIX . 'post-scraper',
  714. 'wpseoAnalysisWorkerL10n',
  715. $localization_data
  716. );
  717. /**
  718. * Removes the emoji script as it is incompatible with both React and any
  719. * contenteditable fields.
  720. */
  721. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  722. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'replacevar-plugin', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  723. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'shortcode-plugin', 'wpseoShortcodePluginL10n', $this->localize_shortcode_plugin_script() );
  724. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
  725. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoSelect2Locale', WPSEO_Language_Utils::get_language( WPSEO_Language_Utils::get_user_locale() ) );
  726. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
  727. if ( post_type_supports( get_post_type(), 'thumbnail' ) ) {
  728. $asset_manager->enqueue_style( 'featured-image' );
  729. $asset_manager->enqueue_script( 'featured-image' );
  730. $featured_image_l10 = [ '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' ) ];
  731. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoFeaturedImageL10n', $featured_image_l10 );
  732. }
  733. }
  734. /**
  735. * Passes some variables to js for upload module.
  736. *
  737. * @return array
  738. */
  739. public function localize_media_script() {
  740. return [
  741. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  742. ];
  743. }
  744. /**
  745. * Returns post in metabox context.
  746. *
  747. * @returns WP_Post|array
  748. */
  749. protected function get_metabox_post() {
  750. $post = filter_input( INPUT_GET, 'post' );
  751. if ( ! empty( $post ) ) {
  752. $post_id = (int) WPSEO_Utils::validate_int( $post );
  753. return get_post( $post_id );
  754. }
  755. if ( isset( $GLOBALS['post'] ) ) {
  756. return $GLOBALS['post'];
  757. }
  758. return [];
  759. }
  760. /**
  761. * Returns an array with shortcode tags for all registered shortcodes.
  762. *
  763. * @return array
  764. */
  765. private function get_valid_shortcode_tags() {
  766. $shortcode_tags = [];
  767. foreach ( $GLOBALS['shortcode_tags'] as $tag => $description ) {
  768. $shortcode_tags[] = $tag;
  769. }
  770. return $shortcode_tags;
  771. }
  772. /**
  773. * Prepares the replace vars for localization.
  774. *
  775. * @return array Replace vars.
  776. */
  777. private function get_replace_vars() {
  778. $post = $this->get_metabox_post();
  779. $cached_replacement_vars = [];
  780. $vars_to_cache = [
  781. 'date',
  782. 'id',
  783. 'sitename',
  784. 'sitedesc',
  785. 'sep',
  786. 'page',
  787. 'currentyear',
  788. ];
  789. foreach ( $vars_to_cache as $var ) {
  790. $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $post );
  791. }
  792. // Merge custom replace variables with the WordPress ones.
  793. return array_merge( $cached_replacement_vars, $this->get_custom_replace_vars( $post ) );
  794. }
  795. /**
  796. * Prepares the recommended replace vars for localization.
  797. *
  798. * @return array Recommended replacement variables.
  799. */
  800. private function get_recommended_replace_vars() {
  801. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  802. $post = $this->get_metabox_post();
  803. // What is recommended depends on the current context.
  804. $post_type = $recommended_replace_vars->determine_for_post( $post );
  805. return $recommended_replace_vars->get_recommended_replacevars_for( $post_type );
  806. }
  807. /**
  808. * Gets the custom replace variables for custom taxonomies and fields.
  809. *
  810. * @param WP_Post $post The post to check for custom taxonomies and fields.
  811. *
  812. * @return array Array containing all the replacement variables.
  813. */
  814. private function get_custom_replace_vars( $post ) {
  815. return [
  816. 'custom_fields' => $this->get_custom_fields_replace_vars( $post ),
  817. 'custom_taxonomies' => $this->get_custom_taxonomies_replace_vars( $post ),
  818. ];
  819. }
  820. /**
  821. * Gets the custom replace variables for custom taxonomies.
  822. *
  823. * @param WP_Post $post The post to check for custom taxonomies.
  824. *
  825. * @return array Array containing all the replacement variables.
  826. */
  827. private function get_custom_taxonomies_replace_vars( $post ) {
  828. $taxonomies = get_object_taxonomies( $post, 'objects' );
  829. $custom_replace_vars = [];
  830. foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
  831. if ( is_string( $taxonomy ) ) { // If attachment, see https://core.trac.wordpress.org/ticket/37368 .
  832. $taxonomy_name = $taxonomy;
  833. $taxonomy = get_taxonomy( $taxonomy_name );
  834. }
  835. if ( $taxonomy->_builtin && $taxonomy->public ) {
  836. continue;
  837. }
  838. $custom_replace_vars[ $taxonomy_name ] = [
  839. 'name' => $taxonomy->name,
  840. 'description' => $taxonomy->description,
  841. ];
  842. }
  843. return $custom_replace_vars;
  844. }
  845. /**
  846. * Gets the custom replace variables for custom fields.
  847. *
  848. * @param WP_Post $post The post to check for custom fields.
  849. *
  850. * @return array Array containing all the replacement variables.
  851. */
  852. private function get_custom_fields_replace_vars( $post ) {
  853. $custom_replace_vars = [];
  854. // If no post object is passed, return the empty custom_replace_vars array.
  855. if ( ! is_object( $post ) ) {
  856. return $custom_replace_vars;
  857. }
  858. $custom_fields = get_post_custom( $post->ID );
  859. foreach ( $custom_fields as $custom_field_name => $custom_field ) {
  860. if ( substr( $custom_field_name, 0, 1 ) === '_' ) {
  861. continue;
  862. }
  863. $custom_replace_vars[ $custom_field_name ] = $custom_field[0];
  864. }
  865. return $custom_replace_vars;
  866. }
  867. /**
  868. * Checks if the page is the post overview page.
  869. *
  870. * @param string $page The page to check for the post overview page.
  871. *
  872. * @return bool Whether or not the given page is the post overview page.
  873. */
  874. public static function is_post_overview( $page ) {
  875. return 'edit.php' === $page;
  876. }
  877. /**
  878. * Checks if the page is the post edit page.
  879. *
  880. * @param string $page The page to check for the post edit page.
  881. *
  882. * @return bool Whether or not the given page is the post edit page.
  883. */
  884. public static function is_post_edit( $page ) {
  885. return 'post.php' === $page
  886. || 'post-new.php' === $page;
  887. }
  888. /**
  889. * Retrieves the product title.
  890. *
  891. * @return string The product title.
  892. */
  893. protected function get_product_title() {
  894. $product_title = 'Yoast SEO';
  895. if ( WPSEO_Utils::is_yoast_seo_premium() ) {
  896. $product_title .= ' Premium';
  897. }
  898. return $product_title;
  899. }
  900. /* ********************* DEPRECATED METHODS ********************* */
  901. /**
  902. * Outputs the page analysis score in the Publish Box.
  903. *
  904. * @deprecated 9.6
  905. * @codeCoverageIgnore
  906. *
  907. * @return void
  908. */
  909. public function publish_box() {
  910. _deprecated_function( __METHOD__, 'WPSEO 9.6' );
  911. }
  912. /**
  913. * Sets up all the functionality related to the prominence of the page analysis functionality.
  914. *
  915. * @deprecated 9.6
  916. * @codeCoverageIgnore
  917. *
  918. * @return void
  919. */
  920. public function setup_page_analysis() {
  921. _deprecated_function( __METHOD__, 'WPSEO 9.6' );
  922. }
  923. /**
  924. * Outputs a tab in the Yoast SEO Metabox.
  925. *
  926. * @deprecated 12.2
  927. * @codeCoverageIgnore
  928. *
  929. * @param string $id CSS ID of the tab.
  930. * @param string $heading Heading for the tab.
  931. * @param string $content Content of the tab. This content should be escaped.
  932. */
  933. public function do_tab( $id, $heading, $content ) {
  934. _deprecated_function( __METHOD__, '12.2' );
  935. ?>
  936. <div id="<?php echo esc_attr( 'wpseo_' . $id ); ?>" class="wpseotab wpseo-form <?php echo esc_attr( $id ); ?>">
  937. <?php
  938. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: deprecated function.
  939. echo $content;
  940. ?>
  941. </div>
  942. <?php
  943. }
  944. }