> */ public static function controls(): array { return array( 'airanksuite_seo_title' => array( 'label' => __( 'SEO title', 'ai-rank-suite' ), 'type' => 'text', /* translators: %title% and %sitename% are literal template variable names, not placeholders — leave them untranslated. */ 'description' => __( 'Leave empty to use the site-wide template. Variables such as %title% and %sitename% work here too.', 'ai-rank-suite' ), ), 'airanksuite_seo_desc' => array( 'label' => __( 'Meta description', 'ai-rank-suite' ), 'type' => 'textarea', 'description' => __( 'What a search engine or an assistant quotes when it summarises this page.', 'ai-rank-suite' ), ), 'airanksuite_focus_keyword' => array( 'label' => __( 'Focus keyword', 'ai-rank-suite' ), 'type' => 'text', 'description' => __( 'What this page should be found for. The analysis scores the page against it.', 'ai-rank-suite' ), ), 'airanksuite_noindex' => array( 'label' => __( 'Hide from search results', 'ai-rank-suite' ), 'type' => 'switcher', 'description' => __( 'noindex — also tells AI crawlers this page is not for citing.', 'ai-rank-suite' ), ), ); } /** * What the panel shows when it opens: whatever this plugin already holds, * whichever surface wrote it. * * @return array */ public static function values( int $post_id ): array { $robots = get_post_meta( $post_id, '_airanksuite_robots', true ); $robots = is_array( $robots ) ? array_map( 'strval', $robots ) : array(); $keywords = (array) ( KeywordAnalyzer::get( $post_id )['shorttail'] ?? array() ); return array( 'airanksuite_seo_title' => (string) get_post_meta( $post_id, '_airanksuite_seo_title', true ), 'airanksuite_seo_desc' => (string) get_post_meta( $post_id, '_airanksuite_seo_desc', true ), 'airanksuite_focus_keyword' => (string) ( $keywords[0] ?? '' ), 'airanksuite_noindex' => in_array( 'noindex', $robots, true ) ? 'yes' : '', ); } /** * Write what the builder sent. * * A field that is not in the payload is left alone — Elementor posts the * settings it knows about, and a partial array must not wipe a value the * meta box or the block-editor sidebar wrote. A field that *is* there and * empty clears, because that is what emptying a field means. * * @param array $settings Elementor page settings. */ /** * What the builder was handed, so a save can tell an edit from an echo. * * Elementor keeps a copy of every control's value in its own page * settings and posts the whole set on every save, layout edits included. * Written back as-is, that copy overwrote a title applied minutes earlier * from the AI review, the block-editor sidebar or the content list: the * builder had loaded before the apply, and its stale copy won on the next * "Update". So the values handed to the controls are remembered per post * and user, and a field whose value is still exactly that is not an edit * and is not written. Only what changed inside Elementor reaches the page. */ private static function seen_key( int $post_id ): string { return 'airanksuite_el_seen_' . $post_id . '_' . get_current_user_id(); } public static function remember( int $post_id, array $values ): void { set_transient( self::seen_key( $post_id ), $values, 12 * HOUR_IN_SECONDS ); } /** * Drop every field the builder did not change. * * @param array $settings Elementor page settings, ours only. * @return array */ public static function changed( int $post_id, array $settings ): array { $seen = get_transient( self::seen_key( $post_id ) ); $current = self::values( $post_id ); $out = array(); foreach ( $settings as $name => $value ) { $value = (string) $value; if ( is_array( $seen ) && array_key_exists( $name, $seen ) && (string) $seen[ $name ] === $value ) { continue; // Still what the builder was given: an echo, not an edit. } if ( array_key_exists( $name, $current ) && (string) $current[ $name ] === $value ) { continue; // Already the page's value: nothing to write. } $out[ $name ] = $value; } return $out; } public static function save( int $post_id, array $settings ): void { if ( array_key_exists( 'airanksuite_seo_title', $settings ) ) { $title = Sanitize::template( (string) $settings['airanksuite_seo_title'] ); if ( '' === $title ) { delete_post_meta( $post_id, '_airanksuite_seo_title' ); } else { update_post_meta( $post_id, '_airanksuite_seo_title', $title ); } } if ( array_key_exists( 'airanksuite_seo_desc', $settings ) ) { $desc = Sanitize::template_multiline( (string) $settings['airanksuite_seo_desc'] ); if ( '' === $desc ) { delete_post_meta( $post_id, '_airanksuite_seo_desc' ); } else { update_post_meta( $post_id, '_airanksuite_seo_desc', $desc ); } } if ( array_key_exists( 'airanksuite_focus_keyword', $settings ) ) { $keyword = sanitize_text_field( (string) $settings['airanksuite_focus_keyword'] ); $current = KeywordAnalyzer::get( $post_id ); $have = (array) ( $current['shorttail'] ?? array() ); // The builder edits the focus keyword — the first of the group. // The rest of the group belongs to the full editor and stays. $rest = array_values( array_filter( $have, static fn( $word ): bool => (string) $word !== $keyword ) ); array_shift( $rest ); $current['shorttail'] = '' === $keyword ? $rest : array_merge( array( $keyword ), $rest ); KeywordAnalyzer::save( $post_id, $current ); } if ( array_key_exists( 'airanksuite_noindex', $settings ) ) { $robots = get_post_meta( $post_id, '_airanksuite_robots', true ); $robots = is_array( $robots ) ? array_values( array_map( 'strval', $robots ) ) : array(); $on = in_array( (string) $settings['airanksuite_noindex'], array( 'yes', '1', 'true' ), true ); $robots = array_values( array_filter( $robots, static fn( string $d ): bool => 'noindex' !== $d && in_array( $d, RobotsModule::DIRECTIVES, true ) ) ); if ( $on ) { $robots[] = 'noindex'; } if ( empty( $robots ) ) { delete_post_meta( $post_id, '_airanksuite_robots' ); } else { update_post_meta( $post_id, '_airanksuite_robots', $robots ); } } // The page's scores were computed from the old values. delete_post_meta( $post_id, '_airanksuite_analysis' ); } /** * Hook the builder up. Called from the module; does nothing without Elementor. */ public static function boot(): void { if ( ! self::available() ) { return; } add_action( 'elementor/documents/register_controls', static function ( $document ): void { if ( ! is_object( $document ) || ! method_exists( $document, 'start_controls_section' ) ) { return; } $post_id = method_exists( $document, 'get_main_id' ) ? (int) $document->get_main_id() : 0; if ( $post_id <= 0 ) { return; } $values = self::values( $post_id ); self::remember( $post_id, $values ); $document->start_controls_section( 'airanksuite_seo_section', array( 'label' => __( 'SEO — AI Rank Suite', 'ai-rank-suite' ), 'tab' => self::TAB_SETTINGS, ) ); foreach ( self::controls() as $name => $control ) { $args = array( 'label' => $control['label'], 'type' => $control['type'], 'description' => $control['description'], 'default' => $values[ $name ] ?? '', 'label_block' => true, ); if ( 'switcher' === $control['type'] ) { $args['label_on'] = __( 'Yes', 'ai-rank-suite' ); $args['label_off'] = __( 'No', 'ai-rank-suite' ); $args['return_value'] = 'yes'; unset( $args['label_block'] ); } $document->add_control( $name, $args ); } $document->end_controls_section(); }, 20, 1 ); add_action( 'elementor/document/after_save', static function ( $document, $data = array() ): void { if ( ! is_object( $document ) || ! method_exists( $document, 'get_main_id' ) ) { return; } $post_id = (int) $document->get_main_id(); if ( $post_id <= 0 || ! current_user_can( 'edit_post', $post_id ) ) { return; } $settings = array(); if ( is_array( $data ) && isset( $data['settings'] ) && is_array( $data['settings'] ) ) { $settings = $data['settings']; } elseif ( method_exists( $document, 'get_settings' ) ) { foreach ( array_keys( self::controls() ) as $name ) { $settings[ $name ] = $document->get_settings( $name ); } } $ours = self::changed( $post_id, array_intersect_key( $settings, self::controls() ) ); if ( ! empty( $ours ) ) { self::save( $post_id, $ours ); } // Whatever the builder holds now is what the next save compares against. self::remember( $post_id, self::values( $post_id ) ); }, 20, 2 ); } }