LLMS_Blocks_Reusable

Manage customizations to reusable blocks


Source Source

File: libraries/lifterlms-blocks/includes/class-llms-blocks-reusable.php

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class LLMS_Blocks_Reusable {
 
    /**
     * Constructor
     *
     * @since 2.0.0
     *
     * @return void
     */
    public function __construct() {
 
        add_action( 'rest_api_init', array( $this, 'rest_register_fields' ) );
        add_filter( 'rest_wp_block_query', array( $this, 'mod_wp_block_query' ), 20, 2 );
 
    }
 
    /**
     * Read rest field read callback
     *
     * @since 2.0.0
     *
     * @param array           $obj     Associative array representing the `wp_block` post.
     * @param WP_REST_Request $request Request object.
     * @return WP_Error|array Error when current user isn't authorized to read the data or the post association array on success.
     */
    public function rest_callback_get( $obj, $request ) {
        return llms_parse_bool( get_post_meta( $obj['id'], '_is_llms_field', true ) ) ? 'yes' : 'no';
    }
 
    /**
     * Rest field update callback
     *
     * @since 2.0.0
     *
     * @param array   $value Post association array.
     * @param WP_Post $obj   Post object for the `wp_block` post.
     * @param string  $key   Field key.
     * @return WP_Error|boolean Returns an error object when current user lacks permission to update the form or `true` on success.
     */
    public function rest_callback_update( $value, $obj, $key ) {
        $value = llms_parse_bool( $value ) ? 'yes' : 'no';
        return update_post_meta( $obj->ID, '_is_llms_field', $value ) ? true : false;
    }
 
    /**
     * Register custom rest fields
     *
     * @since 2.0.0
     *
     * @return void
     */
    public function rest_register_fields() {
 
        register_rest_field(
            'wp_block',
            'is_llms_field',
            array(
                'get_callback'    => array( $this, 'rest_callback_get' ),
                'update_callback' => array( $this, 'rest_callback_update' ),
            )
        );
 
    }
 
    /**
     * Modify the rest request query used to list reusable blocks within the block editor
     *
     * Ensures that reusable blocks containing LifterLMS Form Fields can only be inserted/viewed
     * in the context that we allow them to be used within.
     *
     * + When viewing a `wp_block` post, all reusable blocks should be displayed.
     * + When viewing an `llms_form` post, only blocks that specify `is_llms_field` as 'yes' can be displayed.
     * + When viewing any other post, any post with `is_llms_field` of 'yes' is excluded.
     *
     * @since 2.0.0
     *
     * @see [Reference]
     * @link [URL]
     *
     * @param arrays          $args    WP_Query arguments.
     * @param WP_REST_Request $request Request object.
     * @return array
     */
    public function mod_wp_block_query( $args, $request ) {
 
        $referer = $request->get_header( 'referer' );
        $screen  = empty( $referer ) ? false : $this->get_screen_from_referer( $referer );
 
        // We don't care about the screen or it's a reusable block screen.
        if ( empty( $screen ) || 'wp_block' === $screen ) {
            return $args;
        }
 
        // Add a meta query if it doesn't already exist.
        if ( empty( $args['meta_query'] ) ) {
            $args['meta_query'] = array(
                'relation' => 'AND',
            );
        }
 
        // Forms should show only blocks with forms and everything else should exclude blocks with forms.
        $include_fields       = 'llms_form' === $screen;
        $args['meta_query'][] = $this->get_meta_query( $include_fields );
 
        return $args;
 
    }
 
    /**
     * Retrieve a meta query array depending on the post type of the referring rest request
     *
     * @since 2.0.0
     *
     * @param boolean $include_fields Whether or not to include form fields.
     * @return array
     */
    private function get_meta_query( $include_fields ) {
 
        // Default  query when including fields.
        $meta_query = array(
            'key'   => '_is_llms_field',
            'value' => 'yes',
        );
 
        // Excluding fields.
        if ( ! $include_fields ) {
 
            $meta_query = array(
                'relation' => 'OR',
                wp_parse_args(
                    array(
                        'compare' => '!=',
                    ),
                    $meta_query
                ),
                array(
                    'key'     => '_is_llms_field',
                    'compare' => 'NOT EXISTS',
                ),
            );
        }
 
        return $meta_query;
 
    }
 
    /**
     * Determine the screen where a reusable blocks rest query originated
     *
     * The screen name will either be "widgets" or the WP_Post name of a registered WP_Post type.
     *
     * For any other screen we return `false` because we don't care about it.
     *
     * @since 2.0.0
     * @since 2.3.1 Don't pass `null` to `basename()`.
     *
     * @param string $referer Referring URL for the REST request.
     * @return string|boolean Returns the screen name or `false` if we don't care about the screen.
     */
    private function get_screen_from_referer( $referer ) {
 
        // Blockified widgets screen.
        $url_path = wp_parse_url( $referer, PHP_URL_PATH );
        if ( $url_path && 'widgets.php' === basename( $url_path ) ) {
            return 'widgets';
        }
 
        $query_args = array();
        wp_parse_str( wp_parse_url( $referer, PHP_URL_QUERY ), $query_args );
 
        // Something else.
        if ( empty( $query_args['post'] ) ) {
            return false;
        }
 
        // Block editor for a WP_Post.
        return get_post_type( $query_args['post'] );
 
    }
 
}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.