array( 'label' => 'Yoast SEO', 'test' => 'WPSEO_VERSION', 'title' => '_yoast_wpseo_title', 'desc' => '_yoast_wpseo_metadesc', 'focus' => '_yoast_wpseo_focuskw', ), 'rankmath' => array( 'label' => 'Rank Math', 'test' => 'RANK_MATH_VERSION', 'title' => 'rank_math_title', 'desc' => 'rank_math_description', 'focus' => 'rank_math_focus_keyword', ), 'seopress' => array( 'label' => 'SEOPress', 'test' => 'SEOPRESS_VERSION', 'title' => '_seopress_titles_title', 'desc' => '_seopress_titles_desc', 'focus' => '_seopress_analysis_target_kw', ), 'aioseo' => array( 'label' => 'All in One SEO', 'test' => 'AIOSEO_VERSION', 'title' => '_aioseo_title', 'desc' => '_aioseo_description', 'focus' => '', ), ); } /** * Slug of the active SEO plugin, or '' when none is detected. */ public static function active(): string { if ( null !== self::$detected ) { return self::$detected; } self::$detected = ''; foreach ( self::integrations() as $slug => $integration ) { if ( defined( $integration['test'] ) ) { self::$detected = $slug; break; } } self::$detected = (string) apply_filters( 'ai_seo_tools_seo_integration', self::$detected ); return self::$detected; } /** * Human readable name of the active integration. */ public static function active_label(): string { $active = self::active(); return $active ? ( self::integrations()[ $active ]['label'] ?? $active ) : ''; } /** * Reads the stored SEO title. */ public static function get_title( int $post_id ): string { $active = self::active(); if ( '' === $active ) { return (string) get_post_meta( $post_id, self::FALLBACK_TITLE, true ); } if ( 'aioseo' === $active ) { return (string) self::aioseo_field( $post_id, 'title' ); } $key = self::integrations()[ $active ]['title'] ?? ''; return $key ? (string) get_post_meta( $post_id, $key, true ) : ''; } /** * Reads the stored meta description. */ public static function get_description( int $post_id ): string { $active = self::active(); if ( '' === $active ) { return (string) get_post_meta( $post_id, self::FALLBACK_DESC, true ); } if ( 'aioseo' === $active ) { return (string) self::aioseo_field( $post_id, 'description' ); } $key = self::integrations()[ $active ]['desc'] ?? ''; return $key ? (string) get_post_meta( $post_id, $key, true ) : ''; } /** * Reads the configured focus keyword, when the SEO plugin stores one. */ public static function get_focus_keyword( int $post_id ): string { $active = self::active(); $key = self::integrations()[ $active ]['focus'] ?? ''; if ( ! $key ) { return ''; } $value = get_post_meta( $post_id, $key, true ); // Rank Math stores a comma separated list; the first one is the focus. if ( is_string( $value ) && str_contains( $value, ',' ) ) { $value = trim( explode( ',', $value )[0] ); } return is_string( $value ) ? $value : ''; } /** * Writes the SEO title. * * @return bool True when something was written. */ public static function set_title( int $post_id, string $title ): bool { $active = self::active(); if ( '' === $active ) { update_post_meta( $post_id, self::FALLBACK_TITLE, $title ); return true; } if ( 'aioseo' === $active ) { return self::aioseo_update( $post_id, array( 'title' => $title ) ); } $key = self::integrations()[ $active ]['title'] ?? ''; return $key ? (bool) update_post_meta( $post_id, $key, $title ) : false; } /** * Writes the meta description. */ public static function set_description( int $post_id, string $description ): bool { $active = self::active(); if ( '' === $active ) { update_post_meta( $post_id, self::FALLBACK_DESC, $description ); return true; } if ( 'aioseo' === $active ) { return self::aioseo_update( $post_id, array( 'description' => $description ) ); } $key = self::integrations()[ $active ]['desc'] ?? ''; return $key ? (bool) update_post_meta( $post_id, $key, $description ) : false; } /** * All in One SEO keeps its metadata in a dedicated table rather than post meta. */ private static function aioseo_field( int $post_id, string $field ) { global $wpdb; $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $value = $wpdb->get_var( $wpdb->prepare( "SELECT `{$field}` FROM {$table} WHERE post_id = %d", $post_id ) ); return $value ?? ''; } private static function aioseo_update( int $post_id, array $fields ): bool { global $wpdb; $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $exists = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE post_id = %d", $post_id ) ); $fields['updated'] = current_time( 'mysql' ); if ( $exists ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery return false !== $wpdb->update( $table, $fields, array( 'post_id' => $post_id ) ); } $fields['post_id'] = $post_id; $fields['created'] = current_time( 'mysql' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery return false !== $wpdb->insert( $table, $fields ); } /** * Recommended character budgets, used by the snippet preview. */ public static function limits(): array { return apply_filters( 'ai_seo_tools_meta_limits', array( 'title_min' => 30, 'title_max' => 60, 'desc_min' => 120, 'desc_max' => 155, ) ); } } }