Last active
September 30, 2015 18:49
-
-
Save remcotolsma/d932f4d10a0194a2b342 to your computer and use it in GitHub Desktop.
WordPress sponsors plugin with custom featured image meta box for banners.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PronamicSponsorsPlugin { | |
/** | |
* Constructs and initalizes an sponsor plugin | |
* | |
* @param string $file | |
*/ | |
public function __construct( $file ) { | |
$this->file = $file; | |
// Actions | |
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); | |
add_action( 'init', array( $this, 'init' ) ); | |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | |
add_action( 'wp_ajax_pronamic_set_image', array( $this, 'wp_ajax_set_image' ) ); | |
// Sponsor | |
$post_type = 'sponsor'; | |
add_action( 'add_meta_boxes_' . $post_type, array( $this, 'add_meta_boxes_sponsor' ) ); | |
} | |
////////////////////////////////////////////////// | |
/** | |
* Plugins loaded | |
*/ | |
public function plugins_loaded() { | |
load_plugin_textdomain( 'text_domain', false, dirname( plugin_basename( $this->file ) ) . '/languages' ); | |
} | |
////////////////////////////////////////////////// | |
/** | |
* Init | |
*/ | |
public function init() { | |
register_post_type( 'sponsor', array( | |
'labels' => array( | |
'name' => _x( 'Sponsors', 'post type general name', 'text_domain' ), | |
'singular_name' => _x( 'Sponsor', 'post type singular name', 'text_domain' ), | |
'menu_name' => _x( 'Sponsors', 'admin menu', 'text_domain' ), | |
'name_admin_bar' => _x( 'Sponsor', 'add new on admin bar', 'text_domain' ), | |
'add_new' => _x( 'Add New', 'sponsor', 'text_domain' ), | |
'add_new_item' => __( 'Add New Sponsor', 'text_domain' ), | |
'new_item' => __( 'New Sponsor', 'text_domain' ), | |
'edit_item' => __( 'Edit Sponsor', 'text_domain' ), | |
'view_item' => __( 'View Sponsor', 'text_domain' ), | |
'all_items' => __( 'All Sponsors', 'text_domain' ), | |
'search_items' => __( 'Search Sponsors', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Sponsors:', 'text_domain' ), | |
'not_found' => __( 'No sponsors found.', 'text_domain' ), | |
'not_found_in_trash' => __( 'No sponsors found in Trash.', 'text_domain' ) | |
), | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( | |
'slug' => _x( 'sponsors', 'slug', 'text_domain' ), | |
), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'menu_icon' => 'dashicons-megaphone', | |
'supports' => array( | |
'title', | |
'editor', | |
'author', | |
'thumbnail', | |
'excerpt', | |
'comments', | |
), | |
) ); | |
} | |
/** | |
* Admin enqueue scripts | |
*/ | |
function admin_enqueue_scripts() { | |
wp_enqueue_script( | |
'sponsors', | |
plugins_url( 'admin.js', $this->file ), | |
array(), | |
'1.0.0', | |
true | |
); | |
} | |
////////////////////////////////////////////////// | |
/** | |
* Add meta boxes sponsor | |
*/ | |
public function add_meta_boxes_sponsor( $post ) { | |
add_meta_box( | |
'sponsor_medium_rectangle_banner', | |
__( 'Medium Rectangle Banner', 'text_domain' ), | |
array( $this, 'meta_box_sponsor_medium_rectangle_banner' ), | |
'sponsor', | |
'side', | |
'default' | |
); | |
add_meta_box( | |
'sponsor_rectangle_banner', | |
__( 'Rectangle Banner', 'text_domain' ), | |
array( $this, 'meta_box_sponsor_rectangle_banner' ), | |
'sponsor', | |
'side', | |
'default' | |
); | |
} | |
/** | |
* Meta box sponsor details | |
*/ | |
public function meta_box_sponsor_medium_rectangle_banner( $post ) { | |
echo '<p>'; | |
echo wp_kses( sprintf( | |
__( 'Medium Rectangle Banner is %s × %s.', 'text_domain' ), | |
'<code>' . 300 . '</code>', | |
'<code>' . 250 . '</code>' | |
), array( 'code' => array() ) ); | |
echo '</p>'; | |
echo $this->post_image_html( array( | |
'post_id' => $post->ID, | |
'meta_key' => '_sponsor_medium_rectangle_banner_id', | |
'labels' => array( | |
'select' => __( 'Select Medium Rectangle Banner', 'text_domain' ), | |
'remove' => __( 'Remove Medium Rectangle Banner', 'text_domain' ), | |
), | |
) ); | |
} | |
/** | |
* Meta box sponsor medium rectangle banner | |
*/ | |
public function meta_box_sponsor_rectangle_banner( $post ) { | |
echo '<p>'; | |
echo wp_kses( sprintf( | |
__( 'Rectangle Banner is %s × %s.', 'text_domain' ), | |
'<code>' . 180 . '</code>', | |
'<code>' . 150 . '</code>' | |
), array( 'code' => array() ) ); | |
echo '</p>'; | |
echo $this->post_image_html( array( | |
'post_id' => $post->ID, | |
'meta_key' => '_sponsor_rectangle_banner_id', | |
'labels' => array( | |
'select' => __( 'Select Rectangle Banner', 'text_domain' ), | |
'remove' => __( 'Remove Rectangle Banner', 'text_domain' ), | |
), | |
) ); | |
} | |
/** | |
* WordPress AJAX set image. | |
* | |
* @see https://github.com/WordPress/WordPress/blob/4.1/wp-includes/js/media-editor.js#L638-L660 | |
* @see https://github.com/WordPress/WordPress/blob/4.1/wp-admin/includes/ajax-actions.php#L1923-L1957 | |
*/ | |
function wp_ajax_set_image() { | |
$post_id = filter_input( INPUT_POST, 'post_id', FILTER_VALIDATE_INT ); | |
$meta_key = filter_input( INPUT_POST, 'meta_key', FILTER_SANITIZE_STRING ); | |
$image_id = filter_input( INPUT_POST, 'image_id', FILTER_VALIDATE_INT ); | |
$labels = filter_input( INPUT_POST, 'labels', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY ); | |
if ( empty( $image_id ) ) { | |
delete_post_meta( $post_id, $meta_key ); | |
} else { | |
update_post_meta( $post_id, $meta_key, $image_id ); | |
} | |
$return = $this->post_image_html( array( | |
'post_id' => $post_id, | |
'meta_key' => $meta_key, | |
'image_id' => $image_id, | |
'labels' => $labels, | |
) ); | |
wp_send_json_success( $return ); | |
} | |
/** | |
* Post image HTML. | |
* | |
* @see https://github.com/WordPress/WordPress/blob/4.1/wp-admin/includes/post.php#L1267-L1309 | |
*/ | |
private function post_image_html( $args ) { | |
$args = wp_parse_args( $args, array( | |
'post_id' => null, | |
'meta_key' => '_custom_thumbnail_id', | |
'mime_type' => 'image', | |
'labels' => array( | |
'select' => __( 'Select Image', 'text_domain' ), | |
'remove' => __( 'Remove Image', 'text_domain' ), | |
), | |
) ); | |
$post_id = $args['post_id']; | |
$meta_key = $args['meta_key']; | |
$image_id = get_post_meta( $post_id, $meta_key, true ); | |
$labels = $args['labels']; | |
$post = get_post( $post_id ); | |
$set_thumbnail_link = sprintf( | |
'<a class="%s" href="%s">%s</a>', | |
esc_attr( 'set-post-image' ), | |
esc_attr( '#' ), | |
'%s' | |
); | |
$content = ''; | |
$content .= sprintf( | |
'<div class="pronamic-image-control" data-image-control="%s">', | |
esc_attr( wp_json_encode( $args ) ) | |
); | |
if ( $image_id && get_post( $image_id ) ) { | |
$thumbnail_html = wp_get_attachment_image( $image_id, array( 266, 266 ) ); | |
$content .= '<p>'; | |
$content .= sprintf( $set_thumbnail_link, $thumbnail_html ); | |
$content .= '</p>'; | |
$content .= '<p class="hide-if-no-js">'; | |
$content .= sprintf( | |
'<a class="%s" href="%s">%s</a>', | |
esc_attr( 'remove-post-image' ), | |
esc_attr( '#' ), | |
esc_html( $labels['remove'] ) | |
); | |
$content .= '</p>'; | |
} else { | |
$content .= '<p class="hide-if-no-js">'; | |
$content .= sprintf( $set_thumbnail_link, esc_html( $labels['select'] ) ); | |
$content .= '</p>'; | |
} | |
$content .= '</div>'; | |
return $content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment