prefix assing prefix for meta fields] * [$this->fields meta fields] * @var [type] */ $this->prefix = $prefix; $this->fields = $fields; $this->scripts = $scripts; $this->styles = $styles; $this->nonce = $nonce; /** * Adding actions , this will check if $this->fields is array */ if(is_array($this->fields)) { // register the fields add_action('admin_menu', array(&$this , 'register_meta_settings')); // save settings action add_action('save_post',array(&$this , 'save_meta_fields')); // Register Post Meta Scripts and Styles add_action('admin_enqueue_scripts', array(&$this , 'register_meta_scripts')); // enqueue scripts add_action('admin_enqueue_scripts' , array(&$this , 'print_fe_scripts')); } } public function print_fe_scripts() { // enqueue media gallery script wp_enqueue_script('media-upload'); // register custom script wp_register_script('theme_media_gallery' , get_template_directory_uri() . '/inc/theme-addons/post-meta/post-meta-script.js' , 'jquery'); wp_enqueue_script('theme_media_gallery'); } /** * Register Settings */ public function register_meta_settings() { if(is_array($this->fields['page'])) { foreach ($this->fields['page'] as $page) { // print the fields add_meta_box($this->fields['id'], $this->fields['title'], array(&$this , 'print_meta_fields'), $page, $this->fields['context'], $this->fields['priority']); } } else { // print the fields add_meta_box($this->fields['id'], $this->fields['title'], array(&$this , 'print_meta_fields'), $this->fields['page'], $this->fields['context'], $this->fields['priority']); } } /** * Print Fields */ public function print_meta_fields(){ global $post; echo ''; echo '
'; foreach($this->fields['fields'] as $field){ $meta = get_post_meta($post->ID, $field['id'], true); switch($field['type']){ case 'text' : $out = '

'.$field['desc'].'

'; echo $out; break; case 'textarea' : $out = '

'.$field['desc'].'

'; echo $out; break; case 'select' : $out = '

'.$field['desc'].'

'; echo $out; break; case 'checkbox' : $out = '
'; echo $out; break; case 'media_gallery' : // Media Gallery Code $out = '

'.$field['desc'].'

'; echo $out; break; }// end switch }// end foreach echo '
'; } /** * Save Meta Fields */ public function save_meta_fields($post_id) { global $post_id; // verify nonce if(isset($_POST[$this->nonce])) { if (!wp_verify_nonce($_POST[$this->nonce], basename(__FILE__))) { return $post_id; } } // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // check permissions if(!current_user_can('edit_post', $post_id)) { return $post_id; } foreach ($this->fields['fields'] as $single_field) { $id = $single_field['id']; $old = get_post_meta($post_id, $id, true); if(isset($_POST[$id])) { $new = $_POST[$id]; if ($new && $new != $old) { update_post_meta($post_id, $id, $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $id, $old); } } else{ delete_post_meta($post_id , $id); } } } /** * Register Meta Scripts And Styles */ public function register_meta_scripts() { /** * this will extract styles array and register styles */ // check if this is post editing page if(strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php')) : if(is_array($this->styles) && !empty($this->styles)) { foreach ($this->styles as $key => $value) { wp_register_style($key , $value); wp_enqueue_style($key); } } endif; } } } /** * [$meta_styles the required styles for meta fields] * @var $new [instantiate post meta class [prefix , meta fields array , js , css , nonce id] ] * @var array */ $meta_styles = array( 'blogget-meta-css' => get_template_directory_uri() . '/inc/theme-addons/post-meta/post-meta-styles.css' ); // post meta fields /** * [$new call the class , blogget is the theme main prefix] * [$post_fields , post meta fields] * @class POST_META */ $post_fields = array( 'id' => 'post-meta-items', 'title' => __('Post Format Setting',''), 'page' => array('post'), 'context' => 'normal', 'priority' => 'high', 'fields' => array( array( 'id' => 'video', 'desc' => __('Paste your video code here for video post format .',''), 'name' => __('Post Format Video',''), 'type' => 'textarea' ), array( 'id' => 'buzz_media_gallery' , 'desc' => __('Create gallery and upload images by clicking on upload button , For multiple images selection , hold "CTRL (windows) / Command (mac) " button on your keyboard when clicking .' , ''), 'name' => __('post Format Gallery' , '') , 'type' => 'media_gallery' ), array( 'id' => 'quote', 'desc' => __('Add a quote post , select quote post format .',''), 'name' => __('Post Format Quote',''), 'type' => 'textarea' ), array( 'id' => 'quote-author', 'desc' => __('put quote author name.',''), 'name' => __('Quote Author',''), 'type' => 'text' ), array( 'id' => 'quote-link', 'desc' => __('check this box if you wish to link quote to the post.',''), 'name' => __('Quote Link',''), 'type' => 'checkbox' ), array( 'id' => 'link', 'desc' => __('Paste your link here with http://yourlink.com and make sure link post format is selected.',''), 'name' => __('Post Format Link',''), 'type' => 'text' ), array( 'id' => 'status', 'desc' => __('Embed Status Here, Embed your facebook status or tweet here.',''), 'name' => __('Post Format Status',''), 'type' => 'textarea' ), ) ); $new = new POST_META('blogget' , $post_fields , false , $meta_styles , 'blogget_postmeta_nonce'); ?>