> */ private static $keys = array( 'yoast' => array( 'title' => '_yoast_wpseo_title', 'description' => '_yoast_wpseo_metadesc', 'keyword' => '_yoast_wpseo_focuskw', ), 'rankmath' => array( 'title' => 'rank_math_title', 'description' => 'rank_math_description', 'keyword' => 'rank_math_focus_keyword', ), 'seopress' => array( 'title' => '_seopress_titles_title', 'description' => '_seopress_titles_desc', 'keyword' => '_seopress_analysis_target_kw', ), 'tsf' => array( 'title' => '_genesis_title', 'description' => '_genesis_description', ), ); /** * The SEO plugins running on this site. * * @since 1.0.0 * * @return string[] */ public static function active() { $active = array(); if ( defined( 'WPSEO_VERSION' ) ) { $active[] = 'yoast'; } if ( defined( 'RANK_MATH_VERSION' ) || class_exists( 'RankMath', false ) ) { $active[] = 'rankmath'; } if ( defined( 'SEOPRESS_VERSION' ) ) { $active[] = 'seopress'; } if ( defined( 'AIOSEO_VERSION' ) && class_exists( 'AIOSEO\\Plugin\\Common\\Models\\Post' ) ) { $active[] = 'aioseo'; } if ( defined( 'THE_SEO_FRAMEWORK_VERSION' ) || function_exists( 'the_seo_framework' ) ) { $active[] = 'tsf'; } return $active; } /** * The values an article asks for. The search title only when it differs * from the title: otherwise the SEO plugin's own template is better. * * @since 1.0.0 * * @param string $title The article's title. * @param string $meta_title Its search title. * @param string $description Its search description. * @param string $keyword Its main search term. * @return array{title: string, description: string, keyword: string} */ public static function wanted( $title, $meta_title, $description, $keyword ) { return array( 'title' => '' !== $meta_title && $meta_title !== $title ? $meta_title : '', 'description' => (string) $description, 'keyword' => (string) $keyword, ); } /** * What to do with one value. * * @since 1.0.0 * * @param string $current The value there now; '' when there is none. * @param string $previous The hash of what the plugin wrote last time; '' for nothing. * @param string $wanted The value the article asks for; '' for none. * @return string 'set', 'delete' or 'keep'. */ public static function action_for( $current, $previous, $wanted ) { $ours = '' === $current || ( '' !== $previous && hash_equals( $previous, md5( $current ) ) ); if ( ! $ours ) { return 'keep'; } if ( '' === $wanted ) { return '' === $current ? 'keep' : 'delete'; } return 'set'; } /** * The post meta to write, the keys to remove and the hashes to keep. * * @since 1.0.0 * * @param array $current The values there now, by meta key. * @param array $hashes What the plugin wrote last time, by meta key. * @param array $wanted The article's values, by meta key. * @return array{set: array, delete: string[], hashes: array} */ public static function plan( $current, $hashes, $wanted ) { $plan = array( 'set' => array(), 'delete' => array(), 'hashes' => $hashes, ); foreach ( $wanted as $key => $value ) { $now = isset( $current[ $key ] ) ? (string) $current[ $key ] : ''; $previous = isset( $hashes[ $key ] ) ? (string) $hashes[ $key ] : ''; $decision = self::action_for( $now, $previous, (string) $value ); if ( 'set' === $decision ) { $plan['set'][ $key ] = (string) $value; $plan['hashes'][ $key ] = md5( (string) $value ); } elseif ( 'delete' === $decision ) { $plan['delete'][] = $key; unset( $plan['hashes'][ $key ] ); } } return $plan; } /** * The post meta changes for a post, for every active plugin that keeps its * values in post meta. * * @since 1.0.0 * * @param int $post_id The post; 0 for one not made yet. * @param array{title: string, description: string, keyword: string} $wanted What the article asks for. * @return array{set: array, delete: string[], hashes: array} */ public static function meta_plan( $post_id, $wanted ) { $current = array(); $values = array(); $hashes = $post_id ? get_post_meta( $post_id, self::HASHES, true ) : array(); $hashes = is_array( $hashes ) ? $hashes : array(); foreach ( self::active() as $plugin ) { if ( ! isset( self::$keys[ $plugin ] ) ) { continue; } foreach ( self::$keys[ $plugin ] as $field => $key ) { $values[ $key ] = $wanted[ $field ]; $current[ $key ] = $post_id ? (string) get_post_meta( $post_id, $key, true ) : ''; } } return self::plan( $current, $hashes, $values ); } /** * All in One SEO keeps its values in its own table, read and written * through its own model; its post meta is only a copy. * * @since 1.0.0 * * @param int $post_id The post. * @param array{title: string, description: string, keyword: string} $wanted What the article asks for. * @param array $hashes The hashes so far. * @return array The hashes after. */ public static function write_aioseo( $post_id, $wanted, $hashes ) { if ( ! in_array( 'aioseo', self::active(), true ) ) { return $hashes; } try { $get_post = array( 'AIOSEO\\Plugin\\Common\\Models\\Post', 'getPost' ); if ( ! is_callable( $get_post ) ) { return $hashes; } $model = call_user_func( $get_post, $post_id ); if ( ! is_object( $model ) ) { return $hashes; } $keyphrases = self::keyphrases( isset( $model->keyphrases ) ? $model->keyphrases : null ); $current = array( 'aioseo:title' => isset( $model->title ) ? (string) $model->title : '', 'aioseo:description' => isset( $model->description ) ? (string) $model->description : '', 'aioseo:keyword' => (string) $keyphrases->focus->keyphrase, ); $plan = self::plan( $current, $hashes, array( 'aioseo:title' => $wanted['title'], 'aioseo:description' => $wanted['description'], 'aioseo:keyword' => $wanted['keyword'], ) ); if ( empty( $plan['set'] ) && empty( $plan['delete'] ) ) { return $hashes; } foreach ( array( 'title', 'description', 'keyword' ) as $field ) { $key = 'aioseo:' . $field; if ( ! isset( $plan['set'][ $key ] ) && ! in_array( $key, $plan['delete'], true ) ) { continue; } $value = isset( $plan['set'][ $key ] ) ? $plan['set'][ $key ] : ''; if ( 'keyword' === $field ) { $keyphrases->focus->keyphrase = $value; $model->keyphrases = $keyphrases; $model->focus_keyword = '' === $value ? null : $value; } else { $model->$field = '' === $value ? null : $value; } } $model->save(); // All in One SEO's own hook after a post's values change: its caches and sitemaps listen to it. do_action( 'aioseo_insert_post', $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- another plugin's hook. return $plan['hashes']; } catch ( Exception $e ) { return $hashes; } catch ( Error $e ) { return $hashes; } } /** * All in One SEO's keyphrases, as an object with a focus keyphrase. * * @param mixed $keyphrases The stored value: an object, JSON or nothing. * @return object */ private static function keyphrases( $keyphrases ) { if ( is_string( $keyphrases ) && '' !== $keyphrases ) { $keyphrases = json_decode( $keyphrases ); } if ( ! is_object( $keyphrases ) ) { $keyphrases = new stdClass(); } if ( ! isset( $keyphrases->focus ) || ! is_object( $keyphrases->focus ) ) { $keyphrases->focus = new stdClass(); } if ( ! isset( $keyphrases->focus->keyphrase ) || ! is_string( $keyphrases->focus->keyphrase ) ) { $keyphrases->focus->keyphrase = ''; } if ( ! isset( $keyphrases->focus->score ) ) { $keyphrases->focus->score = 0; } if ( ! isset( $keyphrases->focus->analysis ) || ! is_object( $keyphrases->focus->analysis ) ) { $keyphrases->focus->analysis = new stdClass(); } if ( ! isset( $keyphrases->additional ) || ! is_array( $keyphrases->additional ) ) { $keyphrases->additional = array(); } return $keyphrases; } }