|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The builder taxonomy — one table describing every page builder Saddle |
| 4 | + * knows about. |
| 5 | + * |
| 6 | + * @package Saddle |
| 7 | + */ |
| 8 | + |
| 9 | +defined( 'ABSPATH' ) || exit; |
| 10 | + |
| 11 | +/** |
| 12 | + * Single source of truth for builder knowledge that used to live in three |
| 13 | + * unrelated lists: per-post detection (Saddle_Abilities::builder_signature), |
| 14 | + * site-wide plugin signals (Saddle_Context::builder_signals), and the block |
| 15 | + * namespaces builders own (Saddle_Blocks_Tree::builder_namespaces). Keeping |
| 16 | + * them here means a new builder is added in one row — and the labels the |
| 17 | + * accessor filters receive can never drift from the labels the context |
| 18 | + * warnings use. |
| 19 | + * |
| 20 | + * Two granularities on purpose: per-post rows are FORMATS ("Divi 5" vs |
| 21 | + * "Divi (classic)" — different storage, different tooling), while site |
| 22 | + * signals are keyed by PRODUCT ("Divi" — one install covers both formats). |
| 23 | + */ |
| 24 | +final class Saddle_Builders { |
| 25 | + |
| 26 | + /** |
| 27 | + * The taxonomy. Row order is detection order — first match wins. |
| 28 | + * |
| 29 | + * Per row: |
| 30 | + * - label Per-post format label, emitted to the accessor filters. |
| 31 | + * - product Product label used in site-wide context warnings. |
| 32 | + * - signals Constants/functions/classes that mean the product is |
| 33 | + * active site-wide (present on one row per product). |
| 34 | + * - content Substring of post_content that marks ownership. |
| 35 | + * - meta Meta key => expected value (true = any truthy value). |
| 36 | + * - namespaces Block namespaces the builder owns (invalid in a native |
| 37 | + * Gutenberg tree). |
| 38 | + * |
| 39 | + * @return array<string,array<string,mixed>> |
| 40 | + */ |
| 41 | + private static function table() { |
| 42 | + return array( |
| 43 | + 'divi5' => array( |
| 44 | + 'label' => 'Divi 5', |
| 45 | + 'product' => 'Divi', |
| 46 | + 'signals' => array( 'ET_BUILDER_VERSION', 'ET_CORE_VERSION', 'et_setup_theme' ), |
| 47 | + 'content' => '<!-- wp:divi/', |
| 48 | + 'namespaces' => array( 'divi' ), |
| 49 | + ), |
| 50 | + 'divi4' => array( |
| 51 | + 'label' => 'Divi (classic)', |
| 52 | + 'product' => 'Divi', |
| 53 | + 'meta' => array( '_et_pb_use_builder' => 'on' ), |
| 54 | + 'content' => '[et_pb_section', |
| 55 | + ), |
| 56 | + 'elementor' => array( |
| 57 | + 'label' => 'Elementor', |
| 58 | + 'product' => 'Elementor', |
| 59 | + 'signals' => array( 'ELEMENTOR_VERSION', '\\Elementor\\Plugin' ), |
| 60 | + 'meta' => array( '_elementor_edit_mode' => 'builder' ), |
| 61 | + ), |
| 62 | + 'beaver' => array( |
| 63 | + 'label' => 'Beaver Builder', |
| 64 | + 'product' => 'Beaver Builder', |
| 65 | + 'signals' => array( 'FL_BUILDER_VERSION', 'FLBuilderModel' ), |
| 66 | + 'meta' => array( '_fl_builder_enabled' => true ), |
| 67 | + ), |
| 68 | + 'bricks' => array( |
| 69 | + 'label' => 'Bricks', |
| 70 | + 'product' => 'Bricks', |
| 71 | + 'signals' => array( 'BRICKS_VERSION' ), |
| 72 | + 'meta' => array( '_bricks_page_content_2' => true ), |
| 73 | + ), |
| 74 | + 'wpbakery' => array( |
| 75 | + 'label' => 'WPBakery', |
| 76 | + 'product' => 'WPBakery', |
| 77 | + 'signals' => array( 'WPB_VC_VERSION', 'vc_map' ), |
| 78 | + 'content' => '[vc_row', |
| 79 | + ), |
| 80 | + 'oxygen' => array( |
| 81 | + 'label' => 'Oxygen', |
| 82 | + 'product' => 'Oxygen', |
| 83 | + 'signals' => array( 'CT_VERSION' ), |
| 84 | + 'meta' => array( 'ct_builder_shortcodes' => true ), |
| 85 | + ), |
| 86 | + 'breakdance' => array( |
| 87 | + 'label' => 'Breakdance', |
| 88 | + 'product' => 'Breakdance', |
| 89 | + 'signals' => array( '__BREAKDANCE_VERSION' ), |
| 90 | + 'meta' => array( 'breakdance_data' => true ), |
| 91 | + ), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Which builder format (if any) owns a post's content. First matching |
| 97 | + * row wins, mirroring the original if/elseif chain exactly. |
| 98 | + * |
| 99 | + * No result cache on purpose: WordPress's meta cache already makes the |
| 100 | + * repeated get_post_meta() probes O(1) per request, and a static result |
| 101 | + * cache would go stale the moment an ability (or test) flips builder |
| 102 | + * meta mid-request. |
| 103 | + * |
| 104 | + * @param WP_Post $post Post to inspect. |
| 105 | + * @return string|null Format label ("Divi 5"), or null for plain content. |
| 106 | + */ |
| 107 | + public static function detect( WP_Post $post ) { |
| 108 | + $content = (string) $post->post_content; |
| 109 | + |
| 110 | + foreach ( self::table() as $row ) { |
| 111 | + if ( isset( $row['content'] ) && false !== strpos( $content, $row['content'] ) ) { |
| 112 | + return $row['label']; |
| 113 | + } |
| 114 | + if ( isset( $row['meta'] ) ) { |
| 115 | + foreach ( $row['meta'] as $key => $expected ) { |
| 116 | + $value = get_post_meta( $post->ID, $key, true ); |
| 117 | + if ( true === $expected ? (bool) $value : $expected === $value ) { |
| 118 | + return $row['label']; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Site-wide detection signals, keyed by PRODUCT label — the shape the |
| 129 | + * context builder warnings consume. |
| 130 | + * |
| 131 | + * @return array<string,string[]> Product label => signal list. |
| 132 | + */ |
| 133 | + public static function site_signals() { |
| 134 | + $signals = array(); |
| 135 | + foreach ( self::table() as $row ) { |
| 136 | + if ( isset( $row['signals'] ) && ! isset( $signals[ $row['product'] ] ) ) { |
| 137 | + $signals[ $row['product'] ] = $row['signals']; |
| 138 | + } |
| 139 | + } |
| 140 | + return $signals; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Every block namespace owned by a builder — invalid inside a native |
| 145 | + * Gutenberg tree. |
| 146 | + * |
| 147 | + * @return string[] |
| 148 | + */ |
| 149 | + public static function namespaces() { |
| 150 | + $namespaces = array(); |
| 151 | + foreach ( self::table() as $row ) { |
| 152 | + if ( isset( $row['namespaces'] ) ) { |
| 153 | + $namespaces = array_merge( $namespaces, (array) $row['namespaces'] ); |
| 154 | + } |
| 155 | + } |
| 156 | + return array_values( array_unique( $namespaces ) ); |
| 157 | + } |
| 158 | +} |
0 commit comments