|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Accessor resolution — one place where a post's builder is detected and the |
| 4 | + * matching lint/render accessor is resolved through the integration filters. |
| 5 | + * |
| 6 | + * @package Saddle |
| 7 | + */ |
| 8 | + |
| 9 | +defined( 'ABSPATH' ) || exit; |
| 10 | + |
| 11 | +/** |
| 12 | + * The build-native-accessor → filter → instanceof-gate sequence used to be |
| 13 | + * copy-pasted across lint-page, render-node, and verify-page, with three |
| 14 | + * hand-synced copies of the same 409 error. It lives here once, so the |
| 15 | + * resolution semantics (and the "needs Saddle Pro" message) cannot drift |
| 16 | + * between the three abilities. |
| 17 | + */ |
| 18 | +class Saddle_Accessors { |
| 19 | + |
| 20 | + /** |
| 21 | + * Resolve the lint accessor for a post: Gutenberg for native pages, the |
| 22 | + * `saddle_lint_accessor` filter for builder pages. |
| 23 | + * |
| 24 | + * @param WP_Post $post The post. |
| 25 | + * @param string $code Error code for the unsupported case. |
| 26 | + * @param string|null $message Optional full error message (sprintf template |
| 27 | + * with %1$d post ID and %2$s builder); defaults |
| 28 | + * to the lint-flavored one. |
| 29 | + * @return Saddle_Lint_Accessor|WP_Error |
| 30 | + */ |
| 31 | + public static function lint( WP_Post $post, $code = 'saddle_lint_unsupported', $message = null ) { |
| 32 | + $builder = Saddle_Abilities::builder_signature( $post ); |
| 33 | + $accessor = null === $builder ? new Saddle_Lint_Gutenberg_Accessor() : null; |
| 34 | + |
| 35 | + /** |
| 36 | + * Filter the lint accessor for a page. |
| 37 | + * |
| 38 | + * Builder integrations (Saddle Pro's Divi driver, Elementor/Bricks |
| 39 | + * later) return their Saddle_Lint_Accessor implementation when they |
| 40 | + * own $builder. Null means the page cannot be linted here. |
| 41 | + * |
| 42 | + * @param Saddle_Lint_Accessor|null $accessor Accessor (Gutenberg's for native pages). |
| 43 | + * @param string|null $builder Detected builder, null = native. |
| 44 | + * @param WP_Post $post The post. |
| 45 | + */ |
| 46 | + $accessor = apply_filters( 'saddle_lint_accessor', $accessor, $builder, $post ); |
| 47 | + |
| 48 | + if ( ! $accessor instanceof Saddle_Lint_Accessor ) { |
| 49 | + return self::unsupported( |
| 50 | + $post, |
| 51 | + $builder, |
| 52 | + $code, |
| 53 | + null !== $message |
| 54 | + ? $message |
| 55 | + /* translators: 1: post ID, 2: builder name. */ |
| 56 | + : __( 'Post #%1$d is built with %2$s, and no lint accessor for that builder is installed. Divi 5 pages need Saddle Pro.', 'saddle' ) |
| 57 | + ); |
| 58 | + } |
| 59 | + return $accessor; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Resolve the render accessor for a post: Gutenberg for native pages, the |
| 64 | + * `saddle_render_accessor` filter for builder pages. |
| 65 | + * |
| 66 | + * @param WP_Post $post The post. |
| 67 | + * @return Saddle_Render_Accessor|WP_Error |
| 68 | + */ |
| 69 | + public static function render( WP_Post $post ) { |
| 70 | + $builder = Saddle_Abilities::builder_signature( $post ); |
| 71 | + $accessor = null === $builder ? new Saddle_Render_Gutenberg_Accessor() : null; |
| 72 | + |
| 73 | + /** |
| 74 | + * Filter the render accessor for a page. |
| 75 | + * |
| 76 | + * Builder integrations (Saddle Pro's Divi driver, Elementor/Bricks |
| 77 | + * later) return their Saddle_Render_Accessor implementation when |
| 78 | + * they own $builder. Null means the page cannot be rendered here. |
| 79 | + * |
| 80 | + * @param Saddle_Render_Accessor|null $accessor Accessor (Gutenberg's for native pages). |
| 81 | + * @param string|null $builder Detected builder, null = native. |
| 82 | + * @param WP_Post $post The post. |
| 83 | + */ |
| 84 | + $accessor = apply_filters( 'saddle_render_accessor', $accessor, $builder, $post ); |
| 85 | + |
| 86 | + if ( ! $accessor instanceof Saddle_Render_Accessor ) { |
| 87 | + return self::unsupported( |
| 88 | + $post, |
| 89 | + $builder, |
| 90 | + 'saddle_render_unsupported', |
| 91 | + /* translators: 1: post ID, 2: builder name. */ |
| 92 | + __( 'Post #%1$d is built with %2$s, and no render accessor for that builder is installed. Divi 5 pages need Saddle Pro.', 'saddle' ) |
| 93 | + ); |
| 94 | + } |
| 95 | + return $accessor; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * The shared 409 for "this builder has no installed accessor". |
| 100 | + * |
| 101 | + * @param WP_Post $post The post. |
| 102 | + * @param string|null $builder Detected builder. |
| 103 | + * @param string $code Error code. |
| 104 | + * @param string $message sprintf template (%1$d post ID, %2$s builder). |
| 105 | + * @return WP_Error |
| 106 | + */ |
| 107 | + private static function unsupported( WP_Post $post, $builder, $code, $message ) { |
| 108 | + return new WP_Error( |
| 109 | + $code, |
| 110 | + sprintf( $message, $post->ID, (string) $builder ), |
| 111 | + array( 'status' => 409 ) |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments