localize_script() ); } /** * Loads the custom fields translations. * * @return array The fields to localize. */ public function localize_script() { return [ 'custom_field_names' => $this->get_custom_field_names(), ]; } /** * Retrieve all custom field names set in SEO -> * * @return array The custom field names. */ protected function get_custom_field_names() { $custom_field_names = []; $post = $this->get_post(); if ( ! is_object( $post ) ) { return $custom_field_names; } $options = $this->get_titles_from_options(); $target_option = 'page-analyse-extra-' . $post->post_type; if ( array_key_exists( $target_option, $options ) ) { $custom_field_names = explode( ',', $options[ $target_option ] ); } return $custom_field_names; } /** * Retrieves post data given a post ID or the global. * * @codeCoverageIgnore Method relies on dependencies. * * @return WP_Post|array|null Returns a post if found, otherwise returns an empty array. */ protected function get_post() { $post = filter_input( INPUT_GET, 'post' ); if ( isset( $post ) && $post !== false ) { $post_id = (int) WPSEO_Utils::validate_int( $post ); return get_post( $post_id ); } if ( isset( $GLOBALS['post'] ) ) { return $GLOBALS['post']; } return []; } /** * Retrieves the value of the WPSEO_Titles option. * * @codeCoverageIgnore Method relies on the options. * * @return array The value from WPSEO_Titles option. */ protected function get_titles_from_options() { $option_name = WPSEO_Options::get_option_instance( 'wpseo_titles' )->option_name; return get_option( $option_name, [] ); } }