is it SEO data. */ private static $keys = array(); public static function boot() { add_filter( 'adibridge_snapshot_skip_key', array( self::class, 'skip_key' ), 10, 4 ); add_action( 'adibridge_check_restore', array( self::class, 'check_restore' ), 10, 2 ); } /** * Restore points neither record nor put back SEO data: the SEO plugins' meta, and the stored rows * of ACF fields the SEO rule marks (seo_title, a "Social sharing" group…), on posts and options pages. */ public static function skip_key( $skip, $key, $kind, $target = null ) { if ( $skip ) { return true; } $key = (string) $key; // An ACF row (value or its _name field-key reference) is judged by its field, whatever it is // called: "redirect" or "_twitter_handle" for an ordinary field must stay in restore points. $ref = self::acf_reference( $key, $kind, $target ); if ( null !== $ref ) { return self::is_seo_acf_field( $ref ); } return 'meta' === $kind && self::is_seo_meta( $key ); } /** The ACF field key stored beside $key (in _), or null when $key isn't an ACF row. */ private static function acf_reference( $key, $kind, $target ) { if ( ! function_exists( 'acf_get_field' ) || null === $target ) { return null; } $name = ltrim( $key, '_' ); $ref = 'option' === $kind ? get_option( '_' . $name ) : ( is_int( $target ) ? get_post_meta( $target, '_' . $name, true ) : '' ); return is_string( $ref ) && 0 === strpos( $ref, 'field_' ) ? $ref : null; } /** * Is the ACF field with this key SEO data? Decided from the field definitions alone (its name and * its parents' names and labels), so it works for any post status and for rows a restore point * recorded — no content walk, no permission gate. */ private static function is_seo_acf_field( $field_key ) { if ( isset( self::$keys[ $field_key ] ) ) { return self::$keys[ $field_key ]; } $names = array(); $labels = array(); $field = acf_get_field( $field_key ); for ( $depth = 0; $field && $depth < 20; $depth++ ) { array_unshift( $names, (string) $field['name'] ); array_unshift( $labels, (string) $field['label'] ); $in_layout = empty( $field['parent_layout'] ) ? null : $field['parent_layout']; $parent = isset( $field['parent'] ) ? $field['parent'] : 0; $field = $parent ? acf_get_field( $parent ) : false; // a field group isn't a field: the walk stops there // Inside a flexible content layout: the layout is a container too (a layout labelled "SEO"). $layout = $field && $in_layout ? self::layout( $field, $in_layout ) : null; if ( $layout ) { array_unshift( $names, (string) $layout['name'] ); array_unshift( $labels, (string) $layout['label'] ); } } $seo = $names && self::is_seo_path( implode( '/', $names ) ); foreach ( array_slice( $labels, 0, -1 ) as $label ) { // container labels; the field's own label is just its name $seo = $seo || 1 === preg_match( self::LABEL, trim( $label ) ); } return self::$keys[ $field_key ] = $seo; } private static function layout( array $flexible, $key ) { foreach ( isset( $flexible['layouts'] ) ? (array) $flexible['layouts'] : array() as $layout ) { if ( isset( $layout['key'] ) && $layout['key'] === $key ) { return $layout; } } return null; } /** * A restore puts the whole body back; if that would change FAQ / How-to schema blocks (edited in * the SEO tool or wp-admin since), refuse rather than revert SEO data. */ public static function check_restore( \WP_Post $post, array $row ) { if ( isset( $row['post_content'] ) && self::seo_blocks( $row['post_content'] ) !== self::seo_blocks( $post->post_content ) ) { throw new Tool_Error( esc_html( "Restoring #{$post->ID} would also change its FAQ / How-to schema blocks, which are SEO data managed in " . self::owner() . '. Change the text with wp_update_field instead. Nothing was changed.' ) ); } } /** The schema blocks in a post body, as stored, in order. */ private static function seo_blocks( $content ) { $out = array(); foreach ( self::BLOCKS as $name ) { $q = preg_quote( $name, '#' ); $found = preg_match_all( '#|.*?#s', (string) $content, $m ); if ( false === $found ) { // Too large to scan (backtrack limit): can't tell, so don't let a restore through. throw new Tool_Error( 'This page is too large to check its FAQ / How-to schema blocks, so it can\'t be restored automatically. Nothing was changed.' ); } if ( $found ) { $out = array_merge( $out, $m[0] ); } } return $out; } public static function owner() { return (string) apply_filters( 'adibridge_seo_owner', 'Reflect' ); } public static function note() { return 'SEO (titles, descriptions, social previews, schema) is managed in ' . self::owner() . ', not through this connection.'; } /** Is this field SEO data? Called on every field the registry returns. */ public static function is_seo_field( array $field ) { // A schema block, or any block inside one (AIOSEO keeps FAQ answers in inner paragraphs). $blocks = array_merge( isset( $field['within'] ) ? (array) $field['within'] : array(), empty( $field['block'] ) ? array() : array( $field['block'] ) ); if ( array_intersect( $blocks, self::BLOCKS ) ) { return true; } if ( 'acf' !== $field['editor'] ) { return false; } // One rule for listing and for restore points: the field's definition (names, container and // layout labels), not the breadcrumb text (which carries row numbers). if ( ! empty( $field['acf_key'] ) && function_exists( 'acf_get_field' ) && acf_get_field( $field['acf_key'] ) ) { return self::is_seo_acf_field( $field['acf_key'] ); } if ( self::is_seo_path( (string) preg_replace( '/^[a-z]+:/', '', $field['path'] ) ) ) { return true; } // "Page fields › SEO › Title": a group field the site labels SEO / Social sharing holds SEO data, // whatever its fields are called. The first crumb is the field group's own title (the box on the // edit screen), which says where fields are shown, not what they are: it is not used. $crumbs = array_slice( array_map( 'trim', explode( '›', isset( $field['label'] ) ? (string) $field['label'] : '' ) ), 1, -1 ); foreach ( $crumbs as $crumb ) { if ( preg_match( self::LABEL, (string) preg_replace( '/^\d+\.\s*/', '', $crumb ) ) ) { return true; } } return false; } /** An ACF locator such as seo/title, meta/description, sections/0/og_image or seoTitle. */ public static function is_seo_path( $locator ) { $segments = array(); foreach ( explode( '/', $locator ) as $segment ) { if ( '' === $segment || ctype_digit( $segment ) ) { continue; } // seoTitle → seo_title; "Meta Description" / meta-description → meta_description. $segment = strtolower( (string) preg_replace( array( '/([a-z0-9])([A-Z])/', '/[\s:.-]+/' ), array( '$1_$2', '_' ), $segment ) ); $segments[] = $segment; } $last = count( $segments ) - 1; foreach ( $segments as $i => $segment ) { if ( preg_match( self::SEGMENT, $segment ) ) { return true; } if ( $i === $last && preg_match( self::LEAF_SEGMENT, $segment ) ) { return true; } if ( $i < $last && preg_match( self::CONTAINER, $segment ) && preg_match( '/^(title|desc|description|keywords?|image|img|robots|canonical)$/', $segments[ $i + 1 ] ) ) { return true; } } return false; } /** Apply the rule to a finished field: SEO fields are read-only, with a note saying who owns them. */ public static function apply( array $field ) { if ( self::is_seo_field( $field ) ) { $field['writable'] = false; $field['seo'] = true; $field['note'] = self::note(); } return $field; } public static function is_seo_meta( $key ) { if ( in_array( $key, self::META_KEYS, true ) || ( defined( 'THE_SEO_FRAMEWORK_VERSION' ) && in_array( $key, self::TSF_KEYS, true ) ) ) { return true; } foreach ( self::META_PREFIXES as $prefix ) { if ( 0 === strpos( $key, $prefix ) ) { return true; } } return false; } /** Active SEO plugins with versions (reported for information only). */ public static function plugins() { $known = array( 'Yoast SEO' => defined( 'WPSEO_VERSION' ) ? WPSEO_VERSION : null, 'Rank Math' => defined( 'RANK_MATH_VERSION' ) ? RANK_MATH_VERSION : ( class_exists( 'RankMath' ) ? '' : null ), 'All in One SEO' => defined( 'AIOSEO_VERSION' ) ? AIOSEO_VERSION : null, 'SEOPress' => defined( 'SEOPRESS_VERSION' ) ? SEOPRESS_VERSION : null, 'The SEO Framework' => defined( 'THE_SEO_FRAMEWORK_VERSION' ) ? THE_SEO_FRAMEWORK_VERSION : null, 'Slim SEO' => defined( 'SLIM_SEO_VER' ) ? SLIM_SEO_VER : null, ); $out = array(); foreach ( $known as $name => $version ) { if ( null !== $version ) { $out[] = array( 'name' => $name, 'version' => $version ); } } return $out; } }