cmb_Meta_Box_ajax

CMB ajax methods (i.e.

Defined (1)

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

/admin/includes/CMBF/helpers/cmb_Meta_Box_ajax.php  
  1. class cmb_Meta_Box_ajax { 
  2.  
  3. // A single instance of this class. 
  4. public static $instance = null; 
  5. // Whether to hijack the oembed cache system 
  6. public static $hijack = false; 
  7. public static $object_id = 0; 
  8. public static $embed_args = array(); 
  9. public static $object_type = 'post'; 
  10.  
  11. /** 
  12. * Creates or returns an instance of this class. 
  13. * @since 0.1.0 
  14. * @return cmb_Meta_Box_ajax A single instance of this class. 
  15. */ 
  16. public static function get() { 
  17. if ( self::$instance === null ) 
  18. self::$instance = new self(); 
  19.  
  20. return self::$instance; 
  21.  
  22. /** 
  23. * Handles our oEmbed ajax request 
  24. * @since 0.9.5 
  25. * @return object oEmbed embed code | fallback | error message 
  26. */ 
  27. public function oembed_handler() { 
  28.  
  29. // verify our nonce 
  30. if ( ! ( isset( $_REQUEST['cmb_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb_ajax_nonce'], 'ajax_nonce' ) ) ) 
  31. die(); 
  32.  
  33. // sanitize our search string 
  34. $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] ); 
  35.  
  36. // send back error if empty 
  37. if ( empty( $oembed_string ) ) 
  38. self::send_result( '<p class="ui-state-error-text">'. __( 'Please Try Again', 'cmb' ) .'</p>', false ); 
  39.  
  40. // Set width of embed 
  41. $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640'; 
  42.  
  43. // set url 
  44. $oembed_url = esc_url( $oembed_string ); 
  45. // set args 
  46. $embed_args = array( 'width' => $embed_width ); 
  47.  
  48. // Get embed code (or fallback link) 
  49. $html = self::get_oembed( $oembed_url, $_REQUEST['object_id'], array( 
  50. 'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post',  
  51. 'oembed_args' => $embed_args,  
  52. 'field_id' => $_REQUEST['field_id'],  
  53. ) ); 
  54.  
  55. self::send_result( $html ); 
  56.  
  57.  
  58. /** 
  59. * Retrieves oEmbed from url/object ID 
  60. * @since 0.9.5 
  61. * @param string $url URL to retrieve oEmbed 
  62. * @param int $object_id Object ID 
  63. * @param array $args Arguments for method 
  64. * @return string html markup with embed or fallback 
  65. */ 
  66. public static function get_oembed( $url, $object_id, $args = array() ) { 
  67. global $wp_embed; 
  68.  
  69. $oembed_url = esc_url( $url ); 
  70.  
  71. // Sanitize object_id 
  72. self::$object_id = is_numeric( $object_id ) ? absint( $object_id ) : sanitize_text_field( $object_id ); 
  73.  
  74. $args = wp_parse_args( $args, array( 
  75. 'object_type' => 'post',  
  76. 'oembed_args' => self::$embed_args,  
  77. 'field_id' => false,  
  78. 'cache_key' => false,  
  79. ) ); 
  80.  
  81. self::$embed_args =& $args; 
  82.  
  83. // set the post_ID so oEmbed won't fail 
  84. // wp-includes/class-wp-embed.php, WP_Embed::shortcode(), line 162 
  85. $wp_embed->post_ID = self::$object_id; 
  86.  
  87. // Special scenario if NOT a post object 
  88. if ( isset( $args['object_type'] ) && $args['object_type'] != 'post' ) { 
  89.  
  90. if ( 'options-page' == $args['object_type'] ) { 
  91. // bogus id to pass some numeric checks 
  92. // Issue with a VERY large WP install? 
  93. $wp_embed->post_ID = 1987645321; 
  94. // Use our own cache key to correspond to this field (vs one cache key per url) 
  95. $args['cache_key'] = $args['field_id'] .'_cache'; 
  96. // Ok, we need to hijack the oembed cache system 
  97. self::$hijack = true; 
  98. self::$object_type = $args['object_type']; 
  99.  
  100. // Gets ombed cache from our object's meta (vs postmeta) 
  101. add_filter( 'get_post_metadata', array( 'cmb_Meta_Box_ajax', 'hijack_oembed_cache_get' ), 10, 3 ); 
  102. // Sets ombed cache in our object's meta (vs postmeta) 
  103. add_filter( 'update_post_metadata', array( 'cmb_Meta_Box_ajax', 'hijack_oembed_cache_set' ), 10, 4 ); 
  104.  
  105.  
  106. $embed_args = ''; 
  107. foreach ( $args['oembed_args'] as $key => $val ) { 
  108. $embed_args .= " $key=\"$val\""; 
  109.  
  110. // ping WordPress for an embed 
  111. $check_embed = $wp_embed->run_shortcode( '[embed'. $embed_args .']'. $oembed_url .'[/embed]' ); 
  112.  
  113. // fallback that WordPress creates when no oEmbed was found 
  114. $fallback = $wp_embed->maybe_make_link( $oembed_url ); 
  115.  
  116. // Send back our embed 
  117. if ( $check_embed && $check_embed != $fallback ) 
  118. return '<div class="embed_status">'. $check_embed .'<p class="cmb_remove_wrapper"><a href="#" class="cmb_remove_file_button" rel="'. $args['field_id'] .'">'. __( 'Remove Embed', 'cmb' ) .'</a></p></div>'; 
  119.  
  120. // Otherwise, send back error info that no oEmbeds were found 
  121. return '<p class="ui-state-error-text">'. sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb' ), $fallback ) .' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>'; 
  122.  
  123.  
  124. /** 
  125. * Hijacks retrieving of cached oEmbed. 
  126. * Returns cached data from relevant object metadata (vs postmeta) 
  127. * @since 0.9.5 
  128. * @param boolean $check Whether to retrieve postmeta or override 
  129. * @param int $object_id Object ID 
  130. * @param string $meta_key Object metakey 
  131. * @return mixed Object's oEmbed cached data 
  132. */ 
  133. public static function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { 
  134.  
  135. if ( ! self::$hijack || ( self::$object_id != $object_id && 1987645321 !== $object_id ) ) 
  136. return $check; 
  137.  
  138. // get cached data 
  139. $data = 'options-page' === self::$object_type 
  140. ? cmb_Meta_Box::get_option( self::$object_id, self::$embed_args['cache_key'] ) 
  141. : get_metadata( self::$object_type, self::$object_id, $meta_key, true ); 
  142.  
  143. return $data; 
  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 static function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { 
  156. if ( ! self::$hijack || ( self::$object_id != $object_id && 1987645321 !== $object_id ) ) 
  157. return $check; 
  158.  
  159. // Cache the result to our metadata 
  160. if ( 'options-page' === self::$object_type ) { 
  161. // Set the option 
  162. cmb_Meta_Box::update_option( self::$object_id, self::$embed_args['cache_key'], $meta_value, array( 'type' => 'oembed' ) ); 
  163. // Save the option 
  164. cmb_Meta_Box::save_option( self::$object_id ); 
  165. } else { 
  166. update_metadata( self::$object_type, self::$object_id, $meta_key, $meta_value ); 
  167.  
  168. // Anything other than `null` to cancel saving to postmeta 
  169. return true; 
  170.  
  171. /** 
  172. * Helper to send json encoded response to ajax 
  173. * @since 0.9.5 
  174. * @param string $data Data to be shown via ajax 
  175. * @param boolean $success Success or fail 
  176. */ 
  177. public static function send_result( $data, $success = true ) { 
  178. $found = $success ? 'found' : 'not found'; 
  179. // send back our encoded data 
  180. echo json_encode( array( 'result' => $data, 'id' => $found ) ); 
  181. die(); 
  182.