123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- if (!class_exists('MultiPostThumbnails')) {
- class MultiPostThumbnails {
- public function __construct($args = array()) {
- $this->register($args);
- }
-
- public function register($args = array()) {
- $defaults = array(
- 'label' => null,
- 'id' => null,
- 'post_type' => 'post',
- 'priority' => 'low',
- );
- $args = wp_parse_args($args, $defaults);
-
- foreach($args as $k => $v) {
- $this->$k = $v;
- }
-
- if (null === $this->label || null === $this->id) {
- if (WP_DEBUG) {
- trigger_error(sprintf("The 'label' and 'id' values of the 'args' parameter of '%s::%s()' are required", __CLASS__, __FUNCTION__));
- }
- return;
- }
-
- if (!current_theme_supports('post-thumbnails')) {
- add_theme_support( 'post-thumbnails' );
- }
- add_action('add_meta_boxes', array($this, 'add_metabox'));
- add_filter('attachment_fields_to_edit', array($this, 'add_attachment_field'), 20, 2);
- add_action('admin_init', array($this, 'enqueue_admin_scripts'));
- add_action("wp_ajax_set-{$this->post_type}-{$this->id}-thumbnail", array($this, 'set_thumbnail'));
- add_action('delete_attachment', array($this, 'action_delete_attachment'));
- }
-
- public function add_metabox() {
- add_meta_box("{$this->post_type}-{$this->id}", __($this->label), array($this, 'thumbnail_meta_box'), $this->post_type, 'side', $this->priority);
- }
-
- public function thumbnail_meta_box() {
- global $post;
- $thumbnail_id = get_post_meta($post->ID, "{$this->post_type}_{$this->id}_thumbnail_id", true);
- echo $this->post_thumbnail_html($thumbnail_id);
- }
-
- public function add_attachment_field($form_fields, $post) {
- $calling_post_id = 0;
- if (isset($_GET['post_id']))
- $calling_post_id = absint($_GET['post_id']);
- elseif (isset($_POST) && count($_POST))
- $calling_post_id = $post->post_parent;
-
- $calling_post = get_post($calling_post_id);
- if (is_null($calling_post) || $calling_post->post_type != $this->post_type) {
- return $form_fields;
- }
- $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
- $link = sprintf('<a id="%4$s-%1$s-thumbnail-%2$s" class="%1$s-thumbnail" href="#" onclick="MultiPostThumbnailsSetAsThumbnail(\'%2$s\', \'%1$s\', \'%4$s\', \'%5$s\');return false;">Set as %3$s</a>', $this->id, $post->ID, $this->label, $this->post_type, $ajax_nonce);
- $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
- 'label' => $this->label,
- 'input' => 'html',
- 'html' => $link);
- return $form_fields;
- }
-
- public function enqueue_admin_scripts() {
-
- wp_enqueue_script( 'multi-thumbs', get_template_directory_uri() . '/js/multi-thumbs.js', 'jquery', '20120206', true );
- }
-
- public function action_delete_attachment($post_id) {
- global $wpdb;
- $meta_key = "{$this->post_type}_{$this->id}_thumbnail_id";
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '%s' AND meta_value = %d", $meta_key, $post_id ));
- }
- private function plugins_url($relative_path, $plugin_path) {
- $template_dir = get_template_directory();
- foreach ( array('template_dir', 'plugin_path') as $var ) {
- $$var = str_replace('\\' ,'/', $$var);
- $$var = preg_replace('|/+|', '/', $$var);
- }
- if(0 === strpos($plugin_path, $template_dir)) {
- $url = get_template_directory_uri();
- $folder = str_replace($template_dir, '', dirname($plugin_path));
- if ( '.' != $folder ) {
- $url .= '/' . ltrim($folder, '/');
- }
- if ( !empty($relative_path) && is_string($relative_path) && strpos($relative_path, '..') === false ) {
- $url .= '/' . ltrim($relative_path, '/');
- }
- return $url;
- } else {
- return plugins_url($relative_path, $plugin_path);
- }
- }
-
- public static function has_post_thumbnail($post_type, $id, $post_id = null) {
- if (null === $post_id) {
- $post_id = get_the_ID();
- }
- if (!$post_id) {
- return false;
- }
- return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
- }
-
- public static function the_post_thumbnail($post_type, $thumb_id, $post_id = null, $size = 'cover', $attr = '', $link_to_original = false) {
- echo self::get_the_post_thumbnail($post_type, $thumb_id, $post_id, $size, $attr, $link_to_original);
- }
- public static function the_post_thumbnail_thumb($post_type, $thumb_id, $post_id = null, $size = 'small', $attr = '', $link_to_original = false) {
- echo self::get_the_post_thumbnail($post_type, $thumb_id, $post_id, $size, $attr, $link_to_original);
- }
-
-
- public static function get_the_post_thumbnail($post_type, $thumb_id, $post_id = NULL, $size = 'cover', $attr = '' , $link_to_original = false) {
- global $id;
- $post_id = (NULL === $post_id) ? get_the_ID() : $post_id;
- $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $thumb_id, $post_id);
- $size = apply_filters("{$post_type}_{$post_id}_thumbnail_size", $size);
- if ($post_thumbnail_id) {
- do_action("begin_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size);
- $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
- do_action("end_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size);
- } else {
- $html = '';
- }
- if ($link_to_original) {
- $html = sprintf('<a href="%s">%s</a>', wp_get_attachment_url($post_thumbnail_id), $html);
- }
- return apply_filters("{$post_type}_{$thumb_id}_thumbnail_html", $html, $post_id, $post_thumbnail_id, $size, $attr);
- }
-
- public static function get_post_thumbnail_id($post_type, $id, $post_id) {
- return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
- }
-
- public static function get_post_thumbnail_url($post_type, $id, $post_id = 0) {
- if (!$post_id) {
- $post_id = get_the_ID();
- }
- $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $id, $post_id);
- return wp_get_attachment_url($post_thumbnail_id);
- }
-
- private function post_thumbnail_html($thumbnail_id = NULL) {
- global $content_width, $_wp_additional_image_sizes, $post_ID;
- $set_thumbnail_link = sprintf('<p class="hide-if-no-js"><a title="%1$s" href="%2$s" id="set-%3$s-%4$s-thumbnail" class="thickbox">%%s</a></p>', esc_attr__( "Set {$this->label}" ), get_upload_iframe_src('image'), $this->post_type, $this->id);
- $content = sprintf($set_thumbnail_link, esc_html__( "Set {$this->label}" ));
- if ($thumbnail_id && get_post($thumbnail_id)) {
- $old_content_width = $content_width;
- $content_width = 266;
- if ( !isset($_wp_additional_image_sizes["{$this->post_type}-{$this->id}-thumbnail"]))
- $thumbnail_html = wp_get_attachment_image($thumbnail_id, array($content_width, $content_width));
- else
- $thumbnail_html = wp_get_attachment_image($thumbnail_id, "{$this->post_type}-{$this->id}-thumbnail");
- if (!empty($thumbnail_html)) {
- $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
- $content = sprintf($set_thumbnail_link, $thumbnail_html);
- $content .= sprintf('<p class="hide-if-no-js"><a href="#" id="remove-%1$s-%2$s-thumbnail" onclick="MultiPostThumbnailsRemoveThumbnail(\'%2$s\', \'%1$s\', \'%4$s\');return false;">%3$s</a></p>', $this->post_type, $this->id, esc_html__( "Remove {$this->label}" ), $ajax_nonce);
- }
- $content_width = $old_content_width;
- }
- return $content;
- }
-
- public function set_thumbnail() {
- global $post_ID;
- $post_ID = intval($_POST['post_id']);
- if ( !current_user_can('edit_post', $post_ID))
- die('-1');
- $thumbnail_id = intval($_POST['thumbnail_id']);
- check_ajax_referer("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
- if ($thumbnail_id == '-1') {
- delete_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id");
- die($this->post_thumbnail_html(NULL));
- }
- if ($thumbnail_id && get_post($thumbnail_id)) {
- $thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail');
- if (!empty($thumbnail_html)) {
- update_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id", $thumbnail_id);
- die($this->post_thumbnail_html($thumbnail_id));
- }
- }
- die('0');
- }
- }
- }
|