CMB2_Ajax

CMB2 ajax methods (i.e.

Defined (2)

The class is defined in the following location(s).

/includes/libraries/metabox/includes/CMB2_Ajax.php  
  1. class CMB2_Ajax { 
  2.  
  3. // Whether to hijack the oembed cache system 
  4. protected $hijack = false; 
  5. protected $object_id = 0; 
  6. protected $embed_args = array(); 
  7. protected $object_type = 'post'; 
  8. protected $ajax_update = false; 
  9.  
  10. /** 
  11. * Handles our oEmbed ajax request 
  12. * @since 0.9.5 
  13. * @return object oEmbed embed code | fallback | error message 
  14. */ 
  15. public function oembed_handler() { 
  16.  
  17. // Verify our nonce 
  18. if ( ! ( isset( $_REQUEST['cmb2_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb2_ajax_nonce'], 'ajax_nonce' ) ) ) { 
  19. die(); 
  20.  
  21. // Sanitize our search string 
  22. $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] ); 
  23.  
  24. // Send back error if empty 
  25. if ( empty( $oembed_string ) ) { 
  26. wp_send_json_error( '<p class="ui-state-error-text">' . __( 'Please Try Again', 'cmb2' ) . '</p>' ); 
  27.  
  28. // Set width of embed 
  29. $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640'; 
  30.  
  31. // Set url 
  32. $oembed_url = esc_url( $oembed_string ); 
  33.  
  34. // Set args 
  35. $embed_args = array( 'width' => $embed_width ); 
  36.  
  37. $this->ajax_update = true; 
  38.  
  39. // Get embed code (or fallback link) 
  40. $html = $this->get_oembed( array( 
  41. 'url' => $oembed_url,  
  42. 'object_id' => $_REQUEST['object_id'],  
  43. 'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post',  
  44. 'oembed_args' => $embed_args,  
  45. 'field_id' => $_REQUEST['field_id'],  
  46. ) ); 
  47.  
  48. wp_send_json_success( $html ); 
  49.  
  50. /** 
  51. * Retrieves oEmbed from url/object ID 
  52. * @since 0.9.5 
  53. * @param array $args Arguments for method 
  54. * @return string html markup with embed or fallback 
  55. */ 
  56. public function get_oembed( $args ) { 
  57.  
  58. global $wp_embed; 
  59.  
  60. $oembed_url = esc_url( $args['url'] ); 
  61.  
  62. // Sanitize object_id 
  63. $this->object_id = is_numeric( $args['object_id'] ) ? absint( $args['object_id'] ) : sanitize_text_field( $args['object_id'] ); 
  64.  
  65. $args = wp_parse_args( $args, array( 
  66. 'object_type' => 'post',  
  67. 'oembed_args' => $this->embed_args,  
  68. 'field_id' => false,  
  69. 'cache_key' => false,  
  70. ) ); 
  71.  
  72. $this->embed_args =& $args; 
  73.  
  74.  
  75. /** 
  76. * Set the post_ID so oEmbed won't fail 
  77. * wp-includes/class-wp-embed.php, WP_Embed::shortcode() 
  78. */ 
  79. $wp_embed->post_ID = $this->object_id; 
  80.  
  81. // Special scenario if NOT a post object 
  82. if ( isset( $args['object_type'] ) && 'post' != $args['object_type'] ) { 
  83.  
  84. if ( 'options-page' == $args['object_type'] ) { 
  85.  
  86. // Bogus id to pass some numeric checks. Issue with a VERY large WP install? 
  87. $wp_embed->post_ID = 1987645321; 
  88.  
  89. // Use our own cache key to correspond to this field (vs one cache key per url) 
  90. $args['cache_key'] = $args['field_id'] . '_cache'; 
  91.  
  92. // Ok, we need to hijack the oembed cache system 
  93. $this->hijack = true; 
  94. $this->object_type = $args['object_type']; 
  95.  
  96. // Gets ombed cache from our object's meta (vs postmeta) 
  97. add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 ); 
  98.  
  99. // Sets ombed cache in our object's meta (vs postmeta) 
  100. add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 ); 
  101.  
  102.  
  103. $embed_args = ''; 
  104.  
  105. foreach ( $args['oembed_args'] as $key => $val ) { 
  106. $embed_args .= " $key=\"$val\""; 
  107.  
  108. // Ping WordPress for an embed 
  109. $check_embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' ); 
  110.  
  111. // Fallback that WordPress creates when no oEmbed was found 
  112. $fallback = $wp_embed->maybe_make_link( $oembed_url ); 
  113.  
  114. // Send back our embed 
  115. if ( $check_embed && $check_embed != $fallback ) { 
  116. return '<div class="embed-status">' . $check_embed . '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="' . $args['field_id'] . '">' . __( 'Remove Embed', 'cmb2' ) . '</a></p></div>'; 
  117.  
  118. // Otherwise, send back error info that no oEmbeds were found 
  119. return '<p class="ui-state-error-text">' . sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb2' ), $fallback ) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>'; 
  120.  
  121.  
  122. /** 
  123. * Hijacks retrieving of cached oEmbed. 
  124. * Returns cached data from relevant object metadata (vs postmeta) 
  125. * @since 0.9.5 
  126. * @param boolean $check Whether to retrieve postmeta or override 
  127. * @param int $object_id Object ID 
  128. * @param string $meta_key Object metakey 
  129. * @return mixed Object's oEmbed cached data 
  130. */ 
  131. public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { 
  132.  
  133. if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { 
  134. return $check; 
  135.  
  136. if ( $this->ajax_update ) { 
  137. return false; 
  138.  
  139. // Get cached data 
  140. return ( 'options-page' === $this->object_type ) 
  141. ? cmb2_options( $this->object_id )->get( $this->embed_args['cache_key'] ) 
  142. : get_metadata( $this->object_type, $this->object_id, $meta_key, true ); 
  143.  
  144.  
  145. /** 
  146. * Hijacks saving of cached oEmbed. 
  147. * Saves cached data to relevant object metadata (vs postmeta) 
  148. * @since 0.9.5 
  149. * @param boolean $check Whether to continue setting postmeta 
  150. * @param int $object_id Object ID to get postmeta from 
  151. * @param string $meta_key Postmeta's key 
  152. * @param mixed $meta_value Value of the postmeta to be saved 
  153. * @return boolean Whether to continue setting 
  154. */ 
  155. public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { 
  156.  
  157. if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { 
  158. return $check; 
  159.  
  160. $this->oembed_cache_set( $meta_key, $meta_value ); 
  161.  
  162. // Anything other than `null` to cancel saving to postmeta 
  163. return true; 
  164.  
  165. /** 
  166. * Saves the cached oEmbed value to relevant object metadata (vs postmeta) 
  167. * @since 1.3.0 
  168. * @param string $meta_key Postmeta's key 
  169. * @param mixed $meta_value Value of the postmeta to be saved 
  170. */ 
  171. public function oembed_cache_set( $meta_key, $meta_value ) { 
  172.  
  173. // Cache the result to our metadata 
  174. return ( 'options-page' !== $this->object_type ) 
  175. ? update_metadata( $this->object_type, $this->object_id, $meta_key, $meta_value ) 
  176. : cmb2_options( $this->object_id )->update( $this->embed_args['cache_key'], $meta_value, true ); 
  177.  
/vendor/wordimpress/maps-builder-core/includes/libraries/metabox/includes/CMB2_Ajax.php  
  1. class CMB2_Ajax { 
  2.  
  3. // Whether to hijack the oembed cache system 
  4. protected $hijack = false; 
  5. protected $object_id = 0; 
  6. protected $embed_args = array(); 
  7. protected $object_type = 'post'; 
  8. protected $ajax_update = false; 
  9.  
  10. /** 
  11. * Instance of this class 
  12. * @since 2.2.2 
  13. * @var object 
  14. */ 
  15. protected static $instance; 
  16.  
  17. /** 
  18. * Get the singleton instance of this class 
  19. * @since 2.2.2 
  20. * @return object 
  21. */ 
  22. public static function get_instance() { 
  23. if ( ! ( self::$instance instanceof self ) ) { 
  24. self::$instance = new self(); 
  25.  
  26. return self::$instance; 
  27.  
  28. /** 
  29. * Constructor 
  30. * @since 2.2.0 
  31. */ 
  32. protected function __construct() { 
  33. add_action( 'wp_ajax_cmb2_oembed_handler', array( $this, 'oembed_handler' ) ); 
  34. add_action( 'wp_ajax_nopriv_cmb2_oembed_handler', array( $this, 'oembed_handler' ) ); 
  35. // Need to occasionally clean stale oembed cache data from the option value. 
  36. add_action( 'cmb2_save_options-page_fields', array( __CLASS__, 'clean_stale_options_page_oembeds' ) ); 
  37.  
  38. /** 
  39. * Handles our oEmbed ajax request 
  40. * @since 0.9.5 
  41. * @return object oEmbed embed code | fallback | error message 
  42. */ 
  43. public function oembed_handler() { 
  44.  
  45. // Verify our nonce 
  46. if ( ! ( isset( $_REQUEST['cmb2_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb2_ajax_nonce'], 'ajax_nonce' ) ) ) { 
  47. die(); 
  48.  
  49. // Sanitize our search string 
  50. $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] ); 
  51.  
  52. // Send back error if empty 
  53. if ( empty( $oembed_string ) ) { 
  54. wp_send_json_error( '<p class="ui-state-error-text">' . __( 'Please Try Again', 'cmb2' ) . '</p>' ); 
  55.  
  56. // Set width of embed 
  57. $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640'; 
  58.  
  59. // Set url 
  60. $oembed_url = esc_url( $oembed_string ); 
  61.  
  62. // Set args 
  63. $embed_args = array( 'width' => $embed_width ); 
  64.  
  65. $this->ajax_update = true; 
  66.  
  67. // Get embed code (or fallback link) 
  68. $html = $this->get_oembed( array( 
  69. 'url' => $oembed_url,  
  70. 'object_id' => $_REQUEST['object_id'],  
  71. 'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post',  
  72. 'oembed_args' => $embed_args,  
  73. 'field_id' => $_REQUEST['field_id'],  
  74. ) ); 
  75.  
  76. wp_send_json_success( $html ); 
  77.  
  78. /** 
  79. * Retrieves oEmbed from url/object ID 
  80. * @since 0.9.5 
  81. * @param array $args Arguments for method 
  82. * @return string html markup with embed or fallback 
  83. */ 
  84. public function get_oembed_no_edit( $args ) { 
  85. global $wp_embed; 
  86.  
  87. $oembed_url = esc_url( $args['url'] ); 
  88.  
  89. // Sanitize object_id 
  90. $this->object_id = is_numeric( $args['object_id'] ) ? absint( $args['object_id'] ) : sanitize_text_field( $args['object_id'] ); 
  91.  
  92. $args = wp_parse_args( $args, array( 
  93. 'object_type' => 'post',  
  94. 'oembed_args' => $this->embed_args,  
  95. 'field_id' => false,  
  96. 'wp_error' => false,  
  97. ) ); 
  98.  
  99. $this->embed_args =& $args; 
  100.  
  101. /** 
  102. * Set the post_ID so oEmbed won't fail 
  103. * wp-includes/class-wp-embed.php, WP_Embed::shortcode() 
  104. */ 
  105. $wp_embed->post_ID = $this->object_id; 
  106.  
  107. // Special scenario if NOT a post object 
  108. if ( isset( $args['object_type'] ) && 'post' != $args['object_type'] ) { 
  109.  
  110. if ( 'options-page' == $args['object_type'] ) { 
  111.  
  112. // Bogus id to pass some numeric checks. Issue with a VERY large WP install? 
  113. $wp_embed->post_ID = 1987645321; 
  114.  
  115. // Ok, we need to hijack the oembed cache system 
  116. $this->hijack = true; 
  117. $this->object_type = $args['object_type']; 
  118.  
  119. // Gets ombed cache from our object's meta (vs postmeta) 
  120. add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 ); 
  121.  
  122. // Sets ombed cache in our object's meta (vs postmeta) 
  123. add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 ); 
  124.  
  125.  
  126. $embed_args = ''; 
  127.  
  128. foreach ( $args['oembed_args'] as $key => $val ) { 
  129. $embed_args .= " $key=\"$val\""; 
  130.  
  131. // Ping WordPress for an embed 
  132. $embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' ); 
  133.  
  134. // Fallback that WordPress creates when no oEmbed was found 
  135. $fallback = $wp_embed->maybe_make_link( $oembed_url ); 
  136.  
  137. return compact( 'embed', 'fallback', 'args' ); 
  138.  
  139. /** 
  140. * Retrieves oEmbed from url/object ID 
  141. * @since 0.9.5 
  142. * @param array $args Arguments for method 
  143. * @return string html markup with embed or fallback 
  144. */ 
  145. public function get_oembed( $args ) { 
  146. $oembed = $this->get_oembed_no_edit( $args ); 
  147.  
  148. // Send back our embed 
  149. if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) { 
  150. 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'] . '">' . __( 'Remove Embed', 'cmb2' ) . '</a></p></div>'; 
  151.  
  152. // Otherwise, send back error info that no oEmbeds were found 
  153. return '<p class="ui-state-error-text">' . sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb2' ), $oembed['fallback'] ) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>'; 
  154.  
  155. /** 
  156. * Hijacks retrieving of cached oEmbed. 
  157. * Returns cached data from relevant object metadata (vs postmeta) 
  158. * @since 0.9.5 
  159. * @param boolean $check Whether to retrieve postmeta or override 
  160. * @param int $object_id Object ID 
  161. * @param string $meta_key Object metakey 
  162. * @return mixed Object's oEmbed cached data 
  163. */ 
  164. public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { 
  165. if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { 
  166. return $check; 
  167.  
  168. if ( $this->ajax_update ) { 
  169. return false; 
  170.  
  171. return $this->cache_action( $meta_key ); 
  172.  
  173. /** 
  174. * Hijacks saving of cached oEmbed. 
  175. * Saves cached data to relevant object metadata (vs postmeta) 
  176. * @since 0.9.5 
  177. * @param boolean $check Whether to continue setting postmeta 
  178. * @param int $object_id Object ID to get postmeta from 
  179. * @param string $meta_key Postmeta's key 
  180. * @param mixed $meta_value Value of the postmeta to be saved 
  181. * @return boolean Whether to continue setting 
  182. */ 
  183. public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { 
  184.  
  185. if ( 
  186. ! $this->hijack 
  187. || ( $this->object_id != $object_id && 1987645321 !== $object_id ) 
  188. // only want to hijack oembed meta values 
  189. || 0 !== strpos( $meta_key, '_oembed_' ) 
  190. ) { 
  191. return $check; 
  192.  
  193. $this->cache_action( $meta_key, $meta_value ); 
  194.  
  195. // Anything other than `null` to cancel saving to postmeta 
  196. return true; 
  197.  
  198. /** 
  199. * Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta) 
  200. * @since 1.3.0 
  201. * @param string $meta_key Postmeta's key 
  202. * @param mixed $meta_value (Optional) value of the postmeta to be saved 
  203. */ 
  204. protected function cache_action( $meta_key ) { 
  205. $func_args = func_get_args(); 
  206. $action = isset( $func_args[1] ) ? 'update' : 'get'; 
  207.  
  208. if ( 'options-page' === $this->object_type ) { 
  209.  
  210. $args = array( $meta_key ); 
  211.  
  212. if ( 'update' === $action ) { 
  213. $args[] = $func_args[1]; 
  214. $args[] = true; 
  215.  
  216. // Cache the result to our options 
  217. $status = call_user_func_array( array( cmb2_options( $this->object_id ), $action ), $args ); 
  218. } else { 
  219.  
  220. $args = array( $this->object_type, $this->object_id, $meta_key ); 
  221. $args[] = 'update' === $action ? $func_args : true; 
  222.  
  223. // Cache the result to our metadata 
  224. $status = call_user_func_array( $action . '_metadata', $args ); 
  225.  
  226. return $status; 
  227.  
  228. /** 
  229. * Hooks in when options-page data is saved to clean stale 
  230. * oembed cache data from the option value. 
  231. * @since 2.2.0 
  232. * @param string $option_key The options-page option key 
  233. * @return void 
  234. */ 
  235. public static function clean_stale_options_page_oembeds( $option_key ) { 
  236. $options = cmb2_options( $option_key )->get_options(); 
  237. if ( is_array( $options ) ) { 
  238.  
  239. $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, '', array(), 0 ); 
  240. $now = time(); 
  241. $modified = false; 
  242.  
  243. foreach ( $options as $key => $value ) { 
  244. // Check for cached oembed data 
  245. if ( 0 === strpos( $key, '_oembed_time_' ) ) { 
  246. $cached_recently = ( $now - $value ) < $ttl; 
  247.  
  248. if ( ! $cached_recently ) { 
  249. $modified = true; 
  250. // Remove the the cached ttl expiration, and the cached oembed value. 
  251. unset( $options[ $key ] ); 
  252. unset( $options[ str_replace( '_oembed_time_', '_oembed_', $key ) ] ); 
  253. // Remove the cached unknown values 
  254. elseif ( '{{unknown}}' === $value ) { 
  255. $modified = true; 
  256. unset( $options[ $key ] ); 
  257. // Update the option and remove stale cache data 
  258. if ( $modified ) { 
  259. $updated = cmb2_options( $option_key )->set( $options ); 
  260.