Last active
December 30, 2015 19:39
-
-
Save stevenkword/7876057 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Post Bling | |
*/ | |
class Post_Bling { | |
/* | |
* Class variables | |
*/ | |
var $meta_key = 'post_bling'; | |
var $metabox_title = 'Post Bling'; | |
/* | |
* Register actions and filters | |
* @uses add_action, add_filter | |
* @return null | |
*/ | |
function __construct() { | |
//Actions | |
add_action( 'pre_get_posts', array( $this, 'action_pre_get_posts' ) ); | |
add_action( 'after_setup_theme', array( $this, 'add_image_sizes' ) ); | |
//Admin actions | |
add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) ); | |
add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ) ); | |
add_action( 'save_post', array( $this, 'action_save_post' ) ); | |
} | |
/** | |
* Exclude posts from the feed | |
* @action pre_get_posts | |
* @return null | |
*/ | |
function action_pre_get_posts( $query ) { | |
} | |
/** | |
* Add image sizes specific to the Yahoo feed | |
* @action after_setup_theme | |
* @return null | |
*/ | |
function add_image_sizes() { | |
add_image_size( '_s-980', 980, 550, true ); | |
} | |
/** | |
* Output admin page stylesheet | |
* @uses wp_enqueue_style | |
* @action admin_enqueue_scripts | |
* @return null | |
*/ | |
function action_admin_enqueue_scripts() { | |
wp_enqueue_style( 'wp-color-picker' ); | |
wp_enqueue_script( 'wp-color-picker' ); | |
//wp_enqueue_script( 'iris' ); | |
wp_enqueue_script( 'adminjs', get_template_directory_uri() . '/js/admin.js', false, true ); | |
} | |
/** | |
* Add Yahoo Links meta box | |
* @uses add_meta_box | |
* @action add_meta_boxes | |
* @return null | |
*/ | |
function action_add_meta_boxes() { | |
add_meta_box( $this->meta_key . '_customizer', $this->metabox_title, array( $this, 'render_links_meta_box' ), 'post' , 'advanced' ); | |
add_meta_box( $this->meta_key . '_customizer', $this->metabox_title, array( $this, 'render_links_meta_box' ), 'page' , 'advanced' ); | |
} | |
/** | |
* Render Yahoo Feed Links meta box | |
* @param object $post | |
* @uses get_post_meta, esc_attr, wp_nonce_field | |
* @return null | |
*/ | |
function render_links_meta_box( $post ) { | |
?> | |
<!--Select your favorite color: <input type="color" name="favcolor">--> | |
<div id="post-bling-metabox"> | |
<table class="form-table"> | |
<tr> | |
<td>Background Color</td> | |
<td>Text Color</td> | |
<td> </td> | |
</tr> | |
<tr> | |
<td> | |
<?php $background = get_post_meta( $post->ID, $this->meta_key . '_background_color', true );?> | |
<input type="text" name="<?php echo esc_attr( $this->meta_key ) . '_background_color'; ?>" value="<?php echo $background; ?>" class="my-color-field" /> | |
</td> | |
<td> | |
<?php $foreground = get_post_meta( $post->ID, $this->meta_key . '_foreground_color', true );?> | |
<input type="text" name="<?php echo esc_attr( $this->meta_key ) . '_foreground_color'; ?>" value="<?php echo $foreground; ?>" class="my-color-field" /> | |
</td> | |
</tr> | |
</table> | |
<p class="description">Colorify the Demons!</p> | |
</div> | |
<?php | |
wp_nonce_field( $this->meta_key , $this->meta_key . '_wpnonce' ); | |
//var_dump( $_REQUEST ); | |
} | |
/* | |
* Save Yahoo Feed links metabox input | |
* @param int $post_id | |
* @uses DOING_AUTOSAVE, delete_post_meta, wp_verify_nonce, esc_url_raw, add_post_meta | |
* @action save_post | |
* @return null | |
*/ | |
function action_save_post( $post_id ) { | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
//var_dump( $_POST ); | |
// Checks user capabilities | |
if ( !current_user_can( 'edit_post', $post_id ) ) | |
return; | |
$background = sanitize_text_field( $_POST[ $this->meta_key . '_background_color' ] ); | |
$foreground = sanitize_text_field( $_POST[ $this->meta_key . '_foreground_color' ] ); | |
update_post_meta( $post_id, $this->meta_key . '_background_color', $background ); | |
update_post_meta( $post_id, $this->meta_key . '_foreground_color', $foreground ); | |
/* | |
// Links | |
if ( isset( $_POST[ $this->meta_key . '_links_wpnonce' ] ) && wp_verify_nonce( $_POST[ $this->meta_key . '_links_wpnonce' ], $this->meta_key . '_background_color' ) ) { | |
//First, clear all existing yahoo URLs | |
delete_post_meta( $post_id, $this->meta_key . '_background_color' ); | |
//Now, process input URLs | |
if ( is_array( $_POST[ $this->meta_key . '_links_url' ] ) && ! empty( $_POST[ $this->meta_key . '_links_url' ] ) ) { | |
$sanitized_urls = array(); | |
foreach( $_POST[ $this->meta_key . '_links_url' ] as $key => $url ) { | |
//Validate URL | |
$yahoo_feed_url = $this->sanitize_url( $url ); | |
//Ensure that only unique values are stored for this post | |
if ( in_array( $yahoo_feed_url, $sanitized_urls ) ) | |
continue; | |
else | |
$sanitized_urls[] = $yahoo_feed_url; | |
//Add the link URL | |
if ( ! empty( $yahoo_feed_url ) && 'http://' != $yahoo_feed_url && '/' != $yahoo_feed_url ) { | |
$data['url'] = $yahoo_feed_url; | |
if( ! empty( $_POST[ $this->meta_key . '_links_title' ][$key] ) ) | |
$data['title'] = sanitize_text_field( $_POST[ $this->meta_key . '_links_title' ][$key] ); | |
else | |
$data['title'] = $yahoo_feed_url; | |
add_post_meta( $post_id, $this->meta_key . '_links', $data, false ); | |
} | |
} | |
unset( $sanitized_urls ); | |
} | |
}// | |
*/ | |
} | |
}//class | |
global $post_bling; | |
if( !is_a( $post_bling, 'Post_Bling' ) ) | |
$post_bling = new Post_Bling; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment