CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_Integration_Box
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils

Hooks

  • Hook Reference
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 
<?php

/**
 * CMB2 ajax methods
 * (i.e. a lot of work to get oEmbeds to work with non-post objects)
 *
 * @since  0.9.5
 *
 * @category  WordPress_Plugin
 * @package   CMB2
 * @author    CMB2 team
 * @license   GPL-2.0+
 */
class CMB2_Ajax {

    // Whether to hijack the oembed cache system.
    protected $hijack      = false;
    protected $object_id   = 0;
    protected $embed_args  = array();
    protected $object_type = 'post';
    protected $ajax_update = false;

    /**
     * Instance of this class.
     *
     * @since 2.2.2
     * @var object
     */
    protected static $instance;

    /**
     * Get the singleton instance of this class.
     *
     * @since 2.2.2
     * @return CMB2_Ajax
     */
    public static function get_instance() {
        if ( ! ( self::$instance instanceof self ) ) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Constructor
     *
     * @since 2.2.0
     */
    protected function __construct() {
        add_action( 'wp_ajax_cmb2_oembed_handler', array( $this, 'oembed_handler' ) );
        add_action( 'wp_ajax_nopriv_cmb2_oembed_handler', array( $this, 'oembed_handler' ) );
        // Need to occasionally clean stale oembed cache data from the option value.
        add_action( 'cmb2_save_options-page_fields', array( __CLASS__, 'clean_stale_options_page_oembeds' ) );
    }

    /**
     * Handles our oEmbed ajax request
     *
     * @since  0.9.5
     * @return mixed oEmbed embed code | fallback | error message
     */
    public function oembed_handler() {

        // Verify our nonce.
        if ( ! ( isset( $_REQUEST['cmb2_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb2_ajax_nonce'], 'ajax_nonce' ) ) ) {
            die();
        }

        // Sanitize our search string.
        $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] );

        // Send back error if empty.
        if ( empty( $oembed_string ) ) {
            wp_send_json_error( '<p class="ui-state-error-text">' . esc_html__( 'Please Try Again', 'cmb2' ) . '</p>' );
        }

        // Set width of embed.
        $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640';

        // Set url.
        $oembed_url = esc_url( $oembed_string );

        // Set args.
        $embed_args = array(
            'width' => $embed_width,
        );

        $this->ajax_update = true;

        // Get embed code (or fallback link).
        $html = $this->get_oembed( array(
            'url'         => $oembed_url,
            'object_id'   => $_REQUEST['object_id'],
            'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post',
            'oembed_args' => $embed_args,
            'field_id'    => $_REQUEST['field_id'],
        ) );

        wp_send_json_success( $html );
    }

    /**
     * Retrieves oEmbed from url/object ID
     *
     * @since  0.9.5
     * @param  array $args Arguments for method.
     * @return mixed HTML markup with embed or fallback.
     */
    public function get_oembed_no_edit( $args ) {
        global $wp_embed;

        $oembed_url = esc_url( $args['url'] );

        // Sanitize object_id.
        $this->object_id = is_numeric( $args['object_id'] ) ? absint( $args['object_id'] ) : sanitize_text_field( $args['object_id'] );

        $args = wp_parse_args( $args, array(
            'object_type' => 'post',
            'oembed_args' => array(),
            'field_id'    => false,
            'wp_error'    => false,
        ) );

        $this->embed_args =& $args;

        /*
         * Set the post_ID so oEmbed won't fail
         * wp-includes/class-wp-embed.php, WP_Embed::shortcode()
         */
        $wp_embed->post_ID = $this->object_id;

        // Special scenario if NOT a post object.
        if ( isset( $args['object_type'] ) && 'post' != $args['object_type'] ) {

            if ( 'options-page' == $args['object_type'] ) {

                // Bogus id to pass some numeric checks. Issue with a VERY large WP install?
                $wp_embed->post_ID = 1987645321;
            }

            // Ok, we need to hijack the oembed cache system.
            $this->hijack = true;
            $this->object_type = $args['object_type'];

            // Gets ombed cache from our object's meta (vs postmeta).
            add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 );

            // Sets ombed cache in our object's meta (vs postmeta).
            add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 );

        }

        $embed_args = '';

        foreach ( $args['oembed_args'] as $key => $val ) {
            $embed_args .= " $key=\"$val\"";
        }

        // Ping WordPress for an embed.
        $embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' );

        // Fallback that WordPress creates when no oEmbed was found.
        $fallback = $wp_embed->maybe_make_link( $oembed_url );

        return compact( 'embed', 'fallback', 'args' );
    }

    /**
     * Retrieves oEmbed from url/object ID
     *
     * @since  0.9.5
     * @param  array $args Arguments for method.
     * @return string HTML markup with embed or fallback.
     */
    public function get_oembed( $args ) {
        $oembed = $this->get_oembed_no_edit( $args );

        // Send back our embed.
        if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) {
            return '<div class="cmb2-oembed embed-status">' . $oembed['embed'] . '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="' . $oembed['args']['field_id'] . '">' . esc_html__( 'Remove Embed', 'cmb2' ) . '</a></p></div>';
        }

        // Otherwise, send back error info that no oEmbeds were found.
        return sprintf(
            '<p class="ui-state-error-text">%s</p>',
            sprintf(
                /* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */
                esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ),
                $oembed['fallback'],
                '<a href="https://wordpress.org/support/article/embeds/" target="_blank">codex.wordpress.org/Embeds</a>'
            )
        );
    }

    /**
     * Hijacks retrieving of cached oEmbed.
     * Returns cached data from relevant object metadata (vs postmeta)
     *
     * @since  0.9.5
     * @param  boolean $check     Whether to retrieve postmeta or override.
     * @param  int     $object_id Object ID.
     * @param  string  $meta_key  Object metakey.
     * @return mixed              Object's oEmbed cached data.
     */
    public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) {
        if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) {
            return $check;
        }

        if ( $this->ajax_update ) {
            return false;
        }

        return $this->cache_action( $meta_key );
    }

    /**
     * Hijacks saving of cached oEmbed.
     * Saves cached data to relevant object metadata (vs postmeta)
     *
     * @since  0.9.5
     * @param  boolean $check      Whether to continue setting postmeta.
     * @param  int     $object_id  Object ID to get postmeta from.
     * @param  string  $meta_key   Postmeta's key.
     * @param  mixed   $meta_value Value of the postmeta to be saved.
     * @return boolean             Whether to continue setting.
     */
    public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) {

        if (
            ! $this->hijack
            || ( $this->object_id != $object_id && 1987645321 !== $object_id )
            // Only want to hijack oembed meta values.
            || 0 !== strpos( $meta_key, '_oembed_' )
        ) {
            return $check;
        }

        $this->cache_action( $meta_key, $meta_value );

        // Anything other than `null` to cancel saving to postmeta.
        return true;
    }

    /**
     * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta).
     *
     * @since 1.3.0
     *
     * @param string $meta_key Postmeta's key.
     * @return mixed
     */
    protected function cache_action( $meta_key ) {
        $func_args = func_get_args();
        $action    = isset( $func_args[1] ) ? 'update' : 'get';

        if ( 'options-page' === $this->object_type ) {

            $args = array( $meta_key );

            if ( 'update' === $action ) {
                $args[] = $func_args[1];
                $args[] = true;
            }

            // Cache the result to our options.
            $status = call_user_func_array( array( cmb2_options( $this->object_id ), $action ), $args );
        } else {

            $args = array( $this->object_type, $this->object_id, $meta_key );
            $args[] = 'update' === $action ? $func_args[1] : true;

            // Cache the result to our metadata.
            $status = call_user_func_array( $action . '_metadata', $args );
        }

        return $status;
    }

    /**
     * Hooks in when options-page data is saved to clean stale
     * oembed cache data from the option value.
     *
     * @since  2.2.0
     * @param  string $option_key The options-page option key.
     * @return void
     */
    public static function clean_stale_options_page_oembeds( $option_key ) {
        $options = cmb2_options( $option_key )->get_options();
        $modified = false;
        if ( is_array( $options ) ) {

            $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, '', array(), 0 );
            $now = time();

            foreach ( $options as $key => $value ) {
                // Check for cached oembed data.
                if ( 0 === strpos( $key, '_oembed_time_' ) ) {
                    $cached_recently = ( $now - $value ) < $ttl;

                    if ( ! $cached_recently ) {
                        $modified = true;
                        // Remove the the cached ttl expiration, and the cached oembed value.
                        unset( $options[ $key ] );
                        unset( $options[ str_replace( '_oembed_time_', '_oembed_', $key ) ] );
                    }
                } // End if.
                // Remove the cached unknown values.
                elseif ( '{{unknown}}' === $value ) {
                    $modified = true;
                    unset( $options[ $key ] );
                }
            }
        }

        // Update the option and remove stale cache data.
        if ( $modified ) {
            $updated = cmb2_options( $option_key )->set( $options );
        }
    }

}
CMB2 Documentation API documentation generated by ApiGen