last_error || ! aafm_object_absent( 'post', $post_id ) ) ) { return AAFM_BUILDER_OWNERSHIP_UNKNOWN; } $wpbakery = $post instanceof WP_Post && 1 === preg_match( aafm_page_builder_content_pattern(), (string) $post->post_content ); if ( $pure_read ) { foreach ( $markers as $meta_key => $builder ) { $value = aafm_meta_get( 'post', $post_id, (string) $meta_key, true ); if ( '' === $value || false === $value || null === $value || '0' === $value || 'off' === $value ) { continue; // Present-but-falsy (e.g. Divi toggled off) is not current ownership. } return (string) $builder; } $views = array(); foreach ( $bricks as $key ) { $views[ $key ] = array( aafm_meta_get( 'post', $post_id, $key, true ) ); } if ( aafm_page_builder_bricks_owned( $views[ $bricks[0] ], array_merge( $views[ $bricks[1] ], $views[ $bricks[2] ], $views[ $bricks[3] ] ) ) ) { return 'bricks'; } return $wpbakery ? 'wpbakery' : false; } $stored = aafm_meta_rows( 'post', $post_id, array_merge( array_map( 'strval', array_keys( $markers ) ), $bricks ) ); if ( ! $stored['ok'] ) { return AAFM_BUILDER_OWNERSHIP_UNKNOWN; } foreach ( $stored['by_key'] as $rows ) { if ( $rows['aliased'] > 0 ) { return AAFM_BUILDER_OWNERSHIP_UNKNOWN; } } // Bricks and WPBakery count as one more owner each, so two builders still answer unknown. $views = array(); foreach ( $bricks as $key ) { $views[ $key ] = array( $stored['by_key'][ $key ]['value'] ?? null, aafm_meta_get( 'post', $post_id, $key, true ) ); } $owners = array(); if ( aafm_page_builder_bricks_owned( $views[ $bricks[0] ], array_merge( $views[ $bricks[1] ], $views[ $bricks[2] ], $views[ $bricks[3] ] ) ) ) { $owners[] = 'bricks'; } if ( $wpbakery ) { $owners[] = 'wpbakery'; } $owner = false; foreach ( $markers as $meta_key => $builder ) { $meta_key = (string) $meta_key; $on = false; foreach ( array( $stored['by_key'][ $meta_key ]['value'] ?? null, aafm_meta_get( 'post', $post_id, $meta_key, true ) ) as $value ) { if ( ! ( '' === $value || false === $value || null === $value || '0' === $value || 'off' === $value ) ) { $on = true; // Either view holding a value that is not off counts. } } if ( ! $on ) { continue; } if ( false !== $owner && (string) $builder !== $owner ) { return AAFM_BUILDER_OWNERSHIP_UNKNOWN; } $owner = (string) $builder; } foreach ( $owners as $builder ) { if ( false !== $owner && $builder !== $owner ) { return AAFM_BUILDER_OWNERSHIP_UNKNOWN; } $owner = $builder; } return $owner; } /** * The WPBakery layout pattern: a root row, section or container shortcode in post_content. * * Matched as text, never through has_shortcode(), because WPBakery's tags may not be registered in * an ability request. `\b` stops at the underscore of [vc_row_inner], and an inner row always sits * inside a root this matches. The `_wpb_vc_js_status` meta is never read: it can say 'false' while * the shortcodes still render. * * @return string */ function aafm_page_builder_content_pattern(): string { return '/\[vc_(row|section|flexbox_container|grid_container)\b/'; } /** * The Bricks keys: the editor mode first, then the three stored areas. * * Bricks keeps its data after a page is switched back to Render with WordPress, so the mode vetoes * it. Read in the same query as the markers, and hard-blocked from the meta tools beside them, since * an agent clearing the mode would pass the guard. * * @return string[] */ function aafm_page_builder_bricks_keys(): array { return array( '_bricks_editor_mode', '_bricks_page_content_2', '_bricks_page_header_2', '_bricks_page_footer_2' ); } /** * Whether Bricks owns a post, from every view of its mode and area values. * * False when every mode seen is the Render with WordPress value. Otherwise true when any area holds * a non-empty array. * * @param array $modes Every view's editor mode value. * @param array $areas Every view's area values. * @return bool */ function aafm_page_builder_bricks_owned( array $modes, array $areas ): bool { $seen = array_values( array_filter( $modes, static fn( $mode ): bool => '' !== (string) ( is_scalar( $mode ) ? $mode : '' ) ) ); if ( array() !== $seen && array() === array_diff( array_map( 'strval', $seen ), array( 'wordpress' ) ) ) { return false; } foreach ( $areas as $area ) { $area = maybe_unserialize( $area ); if ( is_array( $area ) && array() !== $area ) { return true; } } return false; } /** * The page-builder ownership marker map: meta key => detected builder's short name. * * Extracted to its own function so aafm_hard_blocked_meta_key() (includes/helpers.php) can hard- * block every configured marker key from the generic post-meta abilities * HIGH: `et_pb_use_builder`/`fusion_builder_status`/`fusion_builder_converted` are NOT `_`-prefixed * so is_protected_meta() never protects them, and neither did the built-in hard-block list, so an * operator who exposes `*` (or one of these keys by name) via the meta allowlist let the agent * clear the marker with update-post-meta/delete-post-meta and then have aafm_exec_update_post()'s * ownership check pass on the very next call, writing through the guard entirely. * `_elementor_data`/`_fl_builder_data` were already covered by is_protected_meta()'s leading- * underscore rule; blocking the whole map here rather than only the two gap keys keeps this * automatically correct for any marker added later through the aafm_page_builder_markers filter. * * `vcv-pageContent`'s marker was confirmed against the real plugin zip from wordpress.org (Visual * Composer Website Builder, free edition, slug `visualcomposer`, stable 45.16.2): read at * Helpers/PostType.php:67, written at Modules/Editors/DataAjax/Controller.php:399, non-empty JSON * means the builder owns the post. This builder is a hybrid: it also writes rendered output into * post_content on every editor save, so an unguarded write here would not simply be invisible - * it desyncs from vcv-pageContent and is reverted the next time someone opens the builder and * saves. The guard still refuses the write; the failure mode is just "the change disappears * later" rather than "the change never appears". * * @return array Meta key => builder short name. */ function aafm_page_builder_markers(): array { return apply_filters( 'aafm_page_builder_markers', aafm_page_builder_default_markers() ); } /** * The markers this plugin ships, before any site filter. The one array both the guard and the * builder names in the ability descriptions read, so the text cannot drift from what is enforced. * * @return array Meta key => builder short name. */ function aafm_page_builder_default_markers(): array { return array( '_elementor_data' => 'elementor', '_et_pb_use_builder' => 'divi', // What Divi writes (Divi 4.27.8 includes/builder/core.php:5049). 'et_pb_use_builder' => 'divi', // Only Divi's form field name; kept so a site that set it still refuses. '_fl_builder_data' => 'beaver-builder', 'fusion_builder_status' => 'avada', 'fusion_builder_converted' => 'avada', 'vcv-pageContent' => 'visual-composer', '_ct_builder_json' => 'oxygen', '_ct_builder_shortcodes' => 'oxygen', 'ct_builder_json' => 'oxygen', 'ct_builder_shortcodes' => 'oxygen', '_oxygen_data' => 'oxygen', '_breakdance_data' => 'breakdance', 'breakdance_data' => 'breakdance', 'brizy_enabled' => 'brizy', ); } /** * The display name of a builder short name ("beaver-builder" becomes "Beaver Builder"). * * @param string $slug Builder short name from the marker map. * @return string */ function aafm_page_builder_label( string $slug ): string { if ( 'wpbakery' === $slug ) { return 'WPBakery'; } return ucwords( str_replace( '-', ' ', $slug ) ); } /** * The builders the shipped guard covers as a sentence list, for ability descriptions. Built from the * unfiltered defaults on purpose: a site filter that adds a marker must not change the registry text * that is catalogued and translated for every site. * * @return string For example "Elementor, Divi, Beaver Builder, and WPBakery". */ function aafm_page_builder_names(): string { $labels = array(); foreach ( aafm_page_builder_slugs() as $slug ) { $labels[] = aafm_page_builder_label( $slug ); } return wp_sprintf_l( '%l', $labels ); } /** * Every builder short name the shipped guard covers: the marker builders, then Bricks and WPBakery, * which are detected without a flat marker. * * @return string[] */ function aafm_page_builder_slugs(): array { return array_merge( array_map( 'strval', array_unique( array_values( aafm_page_builder_default_markers() ) ) ), array( 'bricks', 'wpbakery' ) ); } /** * The shared refusal error every content-write call site returns identically when the target * post is owned by a foreign page builder. * * Wording covers BOTH outcomes an unguarded write could have, not only "no visible effect": * Avada/Fusion Builder is the one covered builder that DOES render from post_content (Fusion * shortcodes), so an unguarded generic write there would alter or corrupt the shortcode tree * rather than silently do nothing. * * @param string $builder The detected builder's short name, or AAFM_BUILDER_OWNERSHIP_UNKNOWN (from * aafm_post_has_foreign_builder_ownership()). * @return WP_Error */ function aafm_page_builder_owned_error( string $builder ): WP_Error { if ( AAFM_BUILDER_OWNERSHIP_UNKNOWN === $builder ) { return new WP_Error( 'aafm_page_builder_owned', __( 'This content may belong to a page builder, and the plugin could not tell which one, so it refused the write. Edit the content in the page builder directly, or try again.', 'agent-abilities-for-mcp' ), array( 'status' => 409 ) ); } $label = aafm_page_builder_label( $builder ); return new WP_Error( 'aafm_page_builder_owned', sprintf( /* translators: 1: detected page builder name, e.g. Elementor. 2: same name, repeated for readability. 3: same name, repeated for readability. */ __( 'This content belongs to %1$s. Writing to it through this ability could have no visible effect or damage %2$s\'s layout, because the builder controls how this content is stored and shown. Edit it in %3$s directly.', 'agent-abilities-for-mcp' ), $label, $label, $label ), array( 'status' => 409 ) ); } /** * Whether an update input asks for an excerpt and nothing else. * * The one write a builder-owned post accepts. The keys are an allowlist: the id keys update-post, * update-cpt-item (post_id) and update-page (page_id, plus the post_id it adds) pass, and excerpt. * Any other key, including one a later schema adds, keeps the builder refusal. * * @param array $input Update input. * @return bool */ function aafm_page_builder_excerpt_only_input( array $input ): bool { return isset( $input['excerpt'] ) && array() === array_diff_key( $input, array_flip( array( 'post_id', 'page_id', 'excerpt' ) ) ); } /** * The columns an excerpt-only edit keeps exactly as stored. wp_update_post() re-sanitizes every * column it carries over, and for a user without unfiltered_html core runs KSES on all three. * * @return string[] */ function aafm_page_builder_pinned_columns(): array { return array( 'post_content', 'post_content_filtered', 'post_title' ); } /** * The post's pinned columns, excerpt and modified dates, read from the posts table past any cache. * * @param int $post_id Post id. * @return array|null Column => stored value, or null when the read failed or found no row. */ function aafm_page_builder_stored_row( int $post_id ): ?array { global $wpdb; // The stored row itself, not a cached copy, is what this proves. $read = aafm_wpdb_row( $wpdb->prepare( 'SELECT post_content, post_content_filtered, post_title, post_excerpt, post_modified, post_modified_gmt FROM %i WHERE ID = %d', $wpdb->posts, $post_id ) ); $row = $read['value']; if ( ! $read['ok'] || ! is_array( $row ) ) { return null; } return array_map( 'strval', $row ); } /** * The stored row an excerpt-only edit starts from, or an error when the cached post does not match it. * * Core's wp_update_post() merges the update into the cached post, so a cached copy that differs from the * stored row cannot be trusted. The stale entry is cleared, so the next call reads the real row. * * @param WP_Post $post The post as loaded for the update. * @return array|WP_Error */ function aafm_page_builder_excerpt_start( WP_Post $post ) { $row = aafm_page_builder_stored_row( (int) $post->ID ); if ( null !== $row ) { $matches = true; foreach ( aafm_page_builder_pinned_columns() as $column ) { $matches = $matches && (string) $post->$column === $row[ $column ]; } if ( $matches ) { return $row; } } clean_post_cache( (int) $post->ID ); return new WP_Error( 'aafm_page_builder_content_unverified', __( 'The plugin could not confirm this post\'s stored content before saving the excerpt, so it saved nothing. Try again.', 'agent-abilities-for-mcp' ), array( 'status' => 409 ) ); } /** * Write an excerpt-only update to a builder-owned post with its stored content pinned. * * Core's wp_update_post() re-runs sanitize_post() over the whole row, so for a user without * unfiltered_html core would put the stored body, title and post_content_filtered through KSES. A * wp_insert_post_data filter at PHP_INT_MAX, scoped to this one call, reads those columns from the * stored row at write time (so a builder save that landed since the caller's check is kept, not * overwritten) and puts them back for the post and for the revision the save creates. The filter * sees slashed data, so the values are slashed. * * Another plugin can still rewrite the post after the row is saved (save_post and later). So the * stored row is read back, and when a pinned column differs, the pinned columns, the excerpt and * the modified dates are put back with one direct row update (a second wp_update_post() would re-run * the same filters and hooks), and the revisions this call saved for the current user are deleted. * The caller is told nothing changed only when the read-back proves it. A revision WordPress pruned * to honor WP_POST_REVISIONS during the write is not brought back. * * @param int $post_id Post id. * @param array $postarr ID and post_excerpt. * @param array $before The stored row before the write (aafm_page_builder_excerpt_start()). * @return int|WP_Error The post id, or an error. */ function aafm_page_builder_write_excerpt( int $post_id, array $postarr, array $before ) { global $wpdb; if ( ! aafm_exact_object_chain( 'post', $post_id ) instanceof WP_Post ) { return aafm_generic_error(); } $columns = array_flip( aafm_page_builder_pinned_columns() ); $pinned = array_intersect_key( $before, $columns ); $created = array(); $pin = static function ( array $data, array $raw ) use ( $post_id, $columns, &$pinned ): array { if ( isset( $raw['ID'] ) && (int) $raw['ID'] === $post_id ) { $now = aafm_page_builder_stored_row( $post_id ); $pinned = null === $now ? $pinned : array_intersect_key( $now, $columns ); } elseif ( 'revision' !== ( $data['post_type'] ?? '' ) || (int) ( $data['post_parent'] ?? 0 ) !== $post_id ) { return $data; } return array_merge( $data, wp_slash( $pinned ) ); }; $track = static function ( $revision_id ) use ( $post_id, &$created ): void { if ( wp_get_post_parent_id( (int) $revision_id ) === $post_id ) { $created[] = (int) $revision_id; } }; add_filter( 'wp_insert_post_data', $pin, PHP_INT_MAX, 2 ); add_action( '_wp_put_post_revision', $track ); try { $result = wp_update_post( wp_slash( $postarr ), true ); } finally { remove_filter( 'wp_insert_post_data', $pin, PHP_INT_MAX ); remove_action( '_wp_put_post_revision', $track ); } if ( is_wp_error( $result ) || (int) $result !== $post_id ) { return aafm_generic_error(); } $after = aafm_page_builder_stored_row( $post_id ); if ( null !== $after && array_intersect_key( $after, $columns ) === $pinned ) { return $post_id; } $restore = array_merge( $before, $pinned ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- restores the exact stored bytes, which a filtered save cannot. $wpdb->update( $wpdb->posts, $restore, array( 'ID' => $post_id ) ); foreach ( $created as $revision_id ) { $revision = aafm_exact_object( 'post', $revision_id ); if ( $revision instanceof WP_Post && get_current_user_id() === (int) $revision->post_author ) { wp_delete_post_revision( $revision_id ); } } clean_post_cache( $post_id ); if ( aafm_page_builder_stored_row( $post_id ) !== $restore ) { return new WP_Error( 'aafm_page_builder_content_changed', __( 'Saving the excerpt changed this page builder\'s stored content, and the plugin could not put it back. Check the post in the page builder.', 'agent-abilities-for-mcp' ), array( 'status' => 500 ) ); } return new WP_Error( 'aafm_page_builder_content_changed', __( 'Saving the excerpt would have changed this page builder\'s stored content, so the plugin put the post back the way it was. Nothing was changed.', 'agent-abilities-for-mcp' ), array( 'status' => 409 ) ); }