/`, a link's address as * `/link`, and repeater items as `///`. * * Writes change one setting inside `_elementor_data`, stored as the same JSON Elementor writes, straight * to the row with a compare-and-swap (a save that landed meanwhile is never overwritten), after the * same markup rules Elementor applies; the rest of the tree is checked to be unchanged. Elementor's * own Document::save() is not used: it re-processes every widget's settings, which would change * settings nobody asked ADI to touch. */ final class Elementor implements Adapter { const META = '_elementor_data'; /** * Editable settings per widget type: key => array( label, type ). A `link` type edits * settings[key]['url']; `image` edits settings[key] = { id, url }. */ const WIDGETS = array( 'heading' => array( 'title' => array( 'Title', 'html' ), 'link' => array( 'Link', 'link' ) ), 'text-editor' => array( 'editor' => array( 'Text', 'html' ) ), 'button' => array( 'text' => array( 'Text', 'text' ), 'link' => array( 'Link', 'link' ) ), 'image' => array( 'image' => array( 'Image', 'image' ), 'caption' => array( 'Caption', 'text' ), 'link' => array( 'Link', 'link' ) ), 'icon-box' => array( 'title_text' => array( 'Title', 'text' ), 'description_text' => array( 'Description', 'html' ), 'link' => array( 'Link', 'link' ) ), 'image-box' => array( 'image' => array( 'Image', 'image' ), 'title_text' => array( 'Title', 'text' ), 'description_text' => array( 'Description', 'html' ), 'link' => array( 'Link', 'link' ) ), 'testimonial' => array( 'testimonial_content' => array( 'Quote', 'html' ), 'testimonial_name' => array( 'Name', 'text' ), 'testimonial_job' => array( 'Title', 'text' ), 'testimonial_image' => array( 'Photo', 'image' ) ), 'alert' => array( 'alert_title' => array( 'Title', 'text' ), 'alert_description' => array( 'Description', 'html' ) ), 'counter' => array( 'title' => array( 'Title', 'text' ) ), 'progress' => array( 'title' => array( 'Title', 'text' ) ), 'html' => array( 'html' => array( 'Custom HTML', 'html' ) ), 'shortcode' => array( 'shortcode' => array( 'Shortcode', 'textarea' ) ), 'call-to-action' => array( 'title' => array( 'Title', 'text' ), 'description' => array( 'Description', 'html' ), 'button' => array( 'Button text', 'text' ), 'link' => array( 'Link', 'link' ) ), ); /** Repeaters per widget type: repeater key => item settings (key => array( label, type )). */ const REPEATERS = array( 'icon-list' => array( 'icon_list' => array( 'text' => array( 'Text', 'text' ), 'link' => array( 'Link', 'link' ) ) ), 'accordion' => array( 'tabs' => array( 'tab_title' => array( 'Title', 'text' ), 'tab_content' => array( 'Content', 'html' ) ) ), 'toggle' => array( 'tabs' => array( 'tab_title' => array( 'Title', 'text' ), 'tab_content' => array( 'Content', 'html' ) ) ), 'tabs' => array( 'tabs' => array( 'tab_title' => array( 'Title', 'text' ), 'tab_content' => array( 'Content', 'html' ) ) ), // Elementor's default "nested elements" versions: titles here, item content in child containers. 'nested-accordion' => array( 'items' => array( 'item_title' => array( 'Title', 'text' ) ) ), 'nested-tabs' => array( 'tabs' => array( 'tab_title' => array( 'Title', 'text' ) ) ), ); public function id() { return 'elementor'; } public function name() { return 'Elementor'; } public function available() { return class_exists( '\Elementor\Plugin' ); } public function detect( \WP_Post $post ) { if ( 'builder' !== get_post_meta( $post->ID, '_elementor_edit_mode', true ) ) { return false; } $raw = get_post_meta( $post->ID, self::META, true ); return is_array( $raw ) ? (bool) $raw : '' !== (string) $raw; } public function post_column( $locator ) { return null; // _elementor_data is post meta } /** * The element tree, decoded from the bytes stored right now — never a cached copy, so what ADI * edits is exactly what the compare-and-swap protects. A Tool_Error when it doesn't decode. */ private static function data( $post_id, $raw = null ) { $raw = null === $raw ? self::raw( $post_id ) : $raw; $raw = is_string( $raw ) && is_serialized( $raw ) ? maybe_unserialize( $raw ) : $raw; $data = is_string( $raw ) ? json_decode( $raw, true ) : $raw; if ( ! is_array( $data ) ) { throw new Tool_Error( esc_html( "The Elementor data of #$post_id can't be read (it isn't valid JSON), so ADI won't edit it. Open the page in Elementor and save it once." ) ); } return $data; } /** Every element, depth-first, with its trail of parent labels. */ private static function walk( array $elements, array $trail = array() ) { $out = array(); foreach ( $elements as $el ) { if ( ! is_array( $el ) || empty( $el['id'] ) ) { continue; } $out[] = array( 'el' => $el, 'trail' => $trail ); if ( ! empty( $el['elements'] ) && is_array( $el['elements'] ) ) { $out = array_merge( $out, self::walk( $el['elements'], array_merge( $trail, array( self::el_label( $el ) ) ) ) ); } } return $out; } private static function el_label( array $el ) { if ( 'widget' === ( isset( $el['elType'] ) ? $el['elType'] : '' ) ) { return ucwords( str_replace( '-', ' ', isset( $el['widgetType'] ) ? $el['widgetType'] : 'widget' ) ); } return ucfirst( isset( $el['elType'] ) ? $el['elType'] : 'element' ); } public function list_fields( \WP_Post $post ) { try { $data = self::data( $post->ID ); } catch ( Tool_Error $e ) { return array( array( 'locator' => 'data', 'label' => 'Elementor content', 'type' => 'readonly', 'value' => null, 'writable' => false, 'note' => Tool_Error::text( $e ) ) ); } $fields = array(); $can = ! class_exists( '\Elementor\User' ) || \Elementor\User::is_current_user_can_edit( $post->ID ); // A page any save by this user would strip (an embed elsewhere): nothing is writable for them. $stable = current_user_can( 'unfiltered_html' ) || self::kses_stable( $data ); foreach ( self::walk( $data ) as $item ) { foreach ( self::element_fields( $item['el'], $item['trail'] ) as $field ) { if ( ! $stable && $field['writable'] ) { $field['writable'] = false; $field['note'] = 'This page has markup elsewhere (embeds, iframes, scripts or forms) that WordPress would strip on any save by this WordPress user. Connect ADI as an Editor or Administrator to edit it.'; } if ( ! $can && $field['writable'] ) { // Elementor's Role Manager keeps this user out of the Elementor editor: say so up front. $field['writable'] = false; $field['note'] = "Elementor doesn't let this WordPress user edit this page (Elementor's Role Manager or its post-type settings)."; } $fields[] = $field; } } return $fields; } private static function element_fields( array $el, array $trail ) { if ( 'widget' !== ( isset( $el['elType'] ) ? $el['elType'] : '' ) ) { return array(); } $type = isset( $el['widgetType'] ) ? $el['widgetType'] : ''; $settings = isset( $el['settings'] ) && is_array( $el['settings'] ) ? $el['settings'] : array(); $dynamic = isset( $settings['__dynamic__'] ) && is_array( $settings['__dynamic__'] ) ? $settings['__dynamic__'] : array(); $where = ( $trail ? implode( ' › ', $trail ) . ' › ' : '' ) . self::el_label( $el ); $base = array( 'widget' => $type ); $out = array(); if ( in_array( $type, array( 'global', 'template' ), true ) || isset( $el['templateID'] ) ) { return array( $base + array( 'locator' => $el['id'] . '/template', 'label' => "$where (shared template)", 'type' => 'readonly', 'value' => isset( $el['templateID'] ) ? (int) $el['templateID'] : ( isset( $settings['template_id'] ) ? (int) $settings['template_id'] : 0 ), 'writable' => false, 'note' => 'A global widget or template: its content is shared with other pages, so ADI does not edit it from here.', ), ); } if ( isset( self::WIDGETS[ $type ] ) ) { foreach ( self::WIDGETS[ $type ] as $key => $spec ) { $field = self::field_for( $el['id'] . '/' . $key, "$where: {$spec[0]}", $spec[1], $settings, $key, $dynamic ); if ( $field && 'html' === $type && ! self::can_use_custom_html() ) { // Elementor makes the HTML widget opt-in per role (a security setting): honour it. $field['writable'] = false; $field['note'] = "Elementor's Role Manager doesn't let this WordPress user edit HTML widgets."; } if ( $field ) { $out[] = $base + $field; } } } if ( isset( self::REPEATERS[ $type ] ) ) { foreach ( self::REPEATERS[ $type ] as $rep => $item_spec ) { $items = isset( $settings[ $rep ] ) && is_array( $settings[ $rep ] ) ? $settings[ $rep ] : array(); $n = 0; foreach ( $items as $row ) { if ( ! is_array( $row ) || empty( $row['_id'] ) ) { continue; } ++$n; $row_dynamic = isset( $row['__dynamic__'] ) && is_array( $row['__dynamic__'] ) ? $row['__dynamic__'] : array(); foreach ( $item_spec as $key => $spec ) { $field = self::field_for( "{$el['id']}/$rep/{$row['_id']}/$key", "$where › Item $n: {$spec[0]}", $spec[1], $row, $key, $row_dynamic ); if ( $field ) { $out[] = $base + $field; } } } } } if ( ! $out && ! isset( self::WIDGETS[ $type ] ) && ! isset( self::REPEATERS[ $type ] ) ) { // Unknown widget (third-party or Pro): show its text so ADI can read the page, but don't edit it. $text = array(); foreach ( $settings as $key => $value ) { // Elementor 4 (atomic) widgets store text as {"$$type":"string","value":"…"}. if ( is_array( $value ) && isset( $value['$$type'], $value['value'] ) && is_string( $value['value'] ) ) { $value = $value['value']; } if ( is_string( $value ) && '' !== trim( $value ) && preg_match( '/(title|text|heading|description|content|editor|caption|label|paragraph)/', $key ) && 0 !== strpos( $key, '_' ) && ! preg_match( '/_(color|colour|align|alignment|tag|size|typography.*|position|width|height|spacing|gap|padding|margin|weight|style|transform|decoration|type|animation|effect|transition|shadow.*|stroke.*|border.*|radius)$/', $key ) ) { $text[] = wp_strip_all_tags( $value ); } } if ( $text ) { $out[] = $base + array( 'locator' => $el['id'] . '/content', 'label' => $where, 'type' => 'readonly', 'value' => implode( "\n", $text ), 'writable' => false, 'note' => 0 === strpos( $type, 'e-' ) ? "An Elementor 4 \"$type\" widget: shown for reference; ADI doesn't edit these yet. Edit it in Elementor." : "Shown for reference; ADI doesn't edit \"$type\" widgets. Edit it in Elementor.", ); } } return $out; } private static function can_use_custom_html() { return ! class_exists( '\Elementor\User' ) || ! method_exists( '\Elementor\User', 'is_current_user_can_use_custom_html' ) || \Elementor\User::is_current_user_can_use_custom_html(); } private static function field_for( $locator, $label, $type, array $settings, $key, array $dynamic ) { if ( isset( $dynamic[ $key ] ) ) { return array( 'locator' => $locator, 'label' => $label, 'type' => 'readonly', 'value' => null, 'writable' => false, 'note' => 'Filled in automatically by an Elementor dynamic tag; change its source instead.', ); } if ( 'link' === $type ) { if ( ! isset( $settings[ $key ] ) || ! is_array( $settings[ $key ] ) ) { return null; } return array( 'locator' => $locator, 'label' => $label, 'type' => 'url', 'value' => isset( $settings[ $key ]['url'] ) ? (string) $settings[ $key ]['url'] : '', 'writable' => true ); } if ( 'image' === $type ) { if ( ! isset( $settings[ $key ] ) || ! is_array( $settings[ $key ] ) ) { return null; } return array( 'locator' => $locator, 'label' => $label, 'type' => 'image', 'value' => isset( $settings[ $key ]['id'] ) ? (int) $settings[ $key ]['id'] : 0, 'writable' => true ); } if ( ! isset( $settings[ $key ] ) || ! is_string( $settings[ $key ] ) ) { return null; // not set on this widget (Elementor shows its default): nothing stored to edit } return array( 'locator' => $locator, 'label' => $label, 'type' => $type, 'value' => $settings[ $key ], 'writable' => true ); } public function read( \WP_Post $post, $locator ) { foreach ( $this->list_fields( $post ) as $field ) { if ( $field['locator'] === $locator ) { return $field; } } return null; } public function write( \WP_Post $post, $locator, $value ) { $field = $this->read( $post, $locator ); if ( ! $field || ! $field['writable'] ) { throw new Tool_Error( esc_html( "elementor:$locator doesn't exist or can't be edited." ) ); } if ( class_exists( '\Elementor\User' ) && ! \Elementor\User::is_current_user_can_edit( $post->ID ) ) { throw new Tool_Error( "Elementor doesn't let this WordPress user edit this page (Elementor's Role Manager or its post-type settings)." ); } if ( 'html' === ( $field['widget'] ?? '' ) && ! self::can_use_custom_html() ) { throw new Tool_Error( "Elementor's Role Manager doesn't let this WordPress user edit HTML widgets." ); } // Elementor kses-filters its data for users without unfiltered_html; the new value must // survive that unchanged… if ( is_string( $value ) && ! current_user_can( 'unfiltered_html' ) && ! Html::equivalent( wp_kses_post( $value ), $value ) ) { $user = wp_get_current_user(); throw new Tool_Error( esc_html( "This text has markup (scripts, iframes, forms) the WordPress user {$user->user_login} isn't allowed to save. Nothing was changed." ) ); } // …and Elementor sanitises headings further for anyone who isn't an administrator. if ( is_string( $value ) && 'heading' === ( $field['widget'] ?? '' ) && '/title' === substr( $locator, -6 ) && ! current_user_can( 'manage_options' ) ) { $heading = \Elementor\Plugin::$instance->widgets_manager->get_widget_types( 'heading' ); if ( $heading && method_exists( $heading, 'sanitize' ) && ! Html::equivalent( $heading->sanitize( $value ), $value ) ) { throw new Tool_Error( 'Elementor only allows plain formatting in headings for this WordPress user (no images or attributes). Nothing was changed.' ); } } $raw = self::raw( $post->ID ); // exactly as stored: the compare-and-swap base and the restore bytes $data = self::data( $post->ID, $raw ); // decoded from those same bytes $parts = explode( '/', $locator ); $id = array_shift( $parts ); $found = false; $new = self::set( $data, $id, $parts, $field['type'], $value, $found ); if ( ! $found ) { throw new Tool_Error( esc_html( "elementor:$locator is no longer there; call wp_get_content again." ) ); } // For users without unfiltered_html, Elementor's own save path (and its REST meta sanitiser) // runs kses over the WHOLE page: an embed elsewhere would be stripped. Refuse that up front. if ( ! current_user_can( 'unfiltered_html' ) && ! self::kses_stable( $new ) ) { $user = wp_get_current_user(); throw new Tool_Error( esc_html( "This page has markup elsewhere (embeds, iframes, scripts or forms) that WordPress would strip on any save by the WordPress user {$user->user_login}. Nothing was changed. Connect ADI as an Editor or Administrator to edit this page." ) ); } // Store the JSON the way Elementor does, but straight to the row: the bridge has already // applied the same rules, and the REST meta sanitiser would re-spell entities in other widgets. $json = wp_json_encode( $new ); self::store_raw( $post->ID, $json, $raw ); // Elementor caches rendered widget HTML per page; drop it so the new text shows. delete_post_meta( $post->ID, '_elementor_element_cache' ); self::assert_only_target_changed( $data, $raw, $json, $post->ID, $id, $parts ); } /** * Would Elementor's kses pass (run over the whole tree on any save by a user without * unfiltered_html) leave stored _elementor_data $raw unchanged? A restore uses the same rule as a write. */ public static function markup_survives_kses( $raw ) { $tree = is_string( $raw ) ? json_decode( $raw, true ) : $raw; if ( is_array( $tree ) ) { return self::kses_stable( $tree ); } return ! is_string( $raw ) || false === strpos( $raw, '<' ) || Html::equivalent( wp_kses_post( $raw ), $raw ); } /** * May the current user turn stored _elementor_data $now into $to as far as HTML widgets go? * Yes if Elementor's Role Manager lets them use custom HTML, or no HTML widget differs. */ public static function html_widgets_may_change( $to, $now ) { if ( self::can_use_custom_html() ) { return true; } return self::html_widgets( $to ) === self::html_widgets( $now ); } /** HTML widgets in stored _elementor_data: element id => its html setting, in id order. */ private static function html_widgets( $raw ) { $tree = is_string( $raw ) ? json_decode( $raw, true ) : $raw; $out = array(); $walk = static function ( $elements ) use ( &$walk, &$out ) { foreach ( (array) $elements as $el ) { if ( ! is_array( $el ) ) { continue; } if ( 'html' === ( isset( $el['widgetType'] ) ? $el['widgetType'] : '' ) ) { $out[ (string) ( isset( $el['id'] ) ? $el['id'] : '' ) ] = isset( $el['settings']['html'] ) ? $el['settings']['html'] : null; } if ( isset( $el['elements'] ) ) { $walk( $el['elements'] ); } } }; $walk( is_array( $tree ) ? $tree : array() ); ksort( $out ); return $out; } /** Would kses_post_deep leave every string of $tree equivalent? */ private static function kses_stable( array $tree ) { $ok = true; array_walk_recursive( $tree, static function ( $v ) use ( &$ok ) { if ( $ok && is_string( $v ) && false !== strpos( $v, '<' ) && ! Html::equivalent( wp_kses_post( $v ), $v ) ) { $ok = false; } } ); return $ok; } /** _elementor_data exactly as stored (no cache, no filters). */ private static function raw( $post_id ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the stored bytes, uncached: the compare-and-set below must compare against what is in the database now return $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", $post_id, self::META ) ); } /** * Write _elementor_data directly (no meta filters) — only if it still holds $expected, so a save * that landed since ADI read the page is never overwritten — and drop the meta cache. */ private static function store_raw( $post_id, $json, $expected ) { global $wpdb; // MySQL compares text by collation (case- and accent-insensitive): compare bytes. SQLite already does. $binary = defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ? '' : 'BINARY '; // Suppressed: with WP_DEBUG_DISPLAY on, wpdb would otherwise print the error, the table and the page JSON into the REST response. $prev = $wpdb->suppress_errors( true ); $done = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE post_id = %d AND meta_key = %s AND {$binary}meta_value = %s", $json, $post_id, self::META, $expected ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $binary is the fixed keyword 'BINARY ' or ''; a compare-and-set on the stored bytes, which update_post_meta() cannot do (its cache is cleared below) $wpdb->suppress_errors( $prev ); wp_cache_delete( $post_id, 'post_meta' ); if ( false === $done ) { // The detail stays in the server log (one line): database errors never travel to ADI, the model or the token holder. Log::line( "Elementor save of #$post_id failed: " . preg_replace( '/\s+/', ' ', (string) $wpdb->last_error ) ); throw new Tool_Error( 'The database refused to save the Elementor page. Nothing was changed.' ); } if ( 0 === (int) $done && $json !== $expected ) { throw new Tool_Error( 'Someone else saved this Elementor page while ADI was editing it, so ADI didn\'t overwrite their change. Nothing was changed; read the page again and retry.' ); } // The page changed: say so (Elementor compares autosaves against this; sitemaps show it). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- post_modified changes with the compare-and-set write above; clean_post_cache() follows $wpdb->update( $wpdb->posts, array( 'post_modified' => current_time( 'mysql' ), 'post_modified_gmt' => current_time( 'mysql', true ) ), array( 'ID' => $post_id ) ); clean_post_cache( $post_id ); } /** $elements with the setting at element $id / $parts set to $value. */ private static function set( array $elements, $id, array $parts, $type, $value, &$found ) { foreach ( $elements as &$el ) { if ( ! is_array( $el ) ) { continue; } if ( isset( $el['id'] ) && $el['id'] === $id ) { $found = true; if ( 1 === count( $parts ) ) { self::assign( $el['settings'], $parts[0], $type, $value ); } else { list( $rep, $item_id, $key ) = $parts; foreach ( $el['settings'][ $rep ] as &$row ) { if ( isset( $row['_id'] ) && $row['_id'] === $item_id ) { self::assign( $row, $key, $type, $value ); } } unset( $row ); } return $elements; } if ( ! empty( $el['elements'] ) && is_array( $el['elements'] ) ) { $el['elements'] = self::set( $el['elements'], $id, $parts, $type, $value, $found ); if ( $found ) { return $elements; } } } unset( $el ); return $elements; } private static function assign( array &$settings, $key, $type, $value ) { if ( 'url' === $type ) { $settings[ $key ]['url'] = $value; } elseif ( 'image' === $type ) { $settings[ $key ]['id'] = $value; $settings[ $key ]['url'] = $value ? (string) wp_get_attachment_url( $value ) : ''; unset( $settings[ $key ]['alt'] ); // the new image's own alt text applies } else { $settings[ $key ] = $value; } } /** * Re-read the stored tree. It must be exactly what this request wrote (otherwise another save * landed at the same moment: refuse, and don't overwrite it), and apart from the target setting * it must equal the tree before. Otherwise put the old tree back, and check that held. */ private static function assert_only_target_changed( array $before, $raw_before, $json, $post_id, $id, array $parts ) { $stored = self::raw( $post_id ); if ( $stored !== $json ) { throw new Tool_Error( 'Someone else saved this Elementor page at the same moment, so ADI\'s change may not be there. Nothing was overwritten; check the page in Elementor and try again.' ); } $after = json_decode( $stored, true ); $found = false; $old = self::get_setting( $before, $id, $parts ); $reset = self::set( $after, $id, $parts, 'raw', $old, $found ); if ( $reset !== $before ) { self::store_raw( $post_id, $raw_before, $json ); // the original bytes, exactly if ( self::raw( $post_id ) !== $raw_before ) { Log::line( "could not restore #$post_id Elementor data after a refused write" ); throw new Tool_Error( esc_html( "Saving changed other parts of this Elementor page, and putting the old version back didn't fully work either. Check #$post_id in Elementor before making more changes." ) ); } throw new Tool_Error( 'Saving would have changed other parts of this Elementor page, so it was put back. Nothing was changed.' ); } } private static function get_setting( array $elements, $id, array $parts ) { foreach ( self::walk( $elements ) as $item ) { if ( $item['el']['id'] !== $id ) { continue; } $s = $item['el']['settings']; if ( 1 === count( $parts ) ) { return isset( $s[ $parts[0] ] ) ? $s[ $parts[0] ] : null; } foreach ( $s[ $parts[0] ] as $row ) { if ( isset( $row['_id'] ) && $row['_id'] === $parts[1] ) { return isset( $row[ $parts[2] ] ) ? $row[ $parts[2] ] : null; } } } return null; } }