PluginProbe
Virtue/Ascend/Pinnacle Toolkit / 2.2
Virtue/Ascend/Pinnacle Toolkit v2.2
4.9.12.2 trunk 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.7 All 48 releases
virtue-toolkit / cmb / helpers / cmb_Meta_Box_ajax.php

cmb_Meta_Box_ajax.php in Virtue/Ascend/Pinnacle Toolkit 2.2, at cmb/helpers/cmb_Meta_Box_ajax.php

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