<?php

	add_action( 'add_meta_boxes', 'wp_superlike_add_metabox' );
	function wp_superlike_add_metabox() {
		$post_types = get_post_types();
		foreach ( $post_types as $post_type )
			add_meta_box( 'wp_superlike-metabox', __('SuperLike Options','wp_superlike'), 'wp_superlike_render_metabox', $post_type, 'normal', 'low' );
	}

	/* render the meta box */
	function wp_superlike_render_metabox($post) {
	
	$style = get_post_meta($post->ID, 'style', true);
	$showbutton = get_post_meta($post->ID, 'showbutton', true);
	
	wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>

		<div class="wrap">
			<table class="form-table">
				<tr valign="top">
					<th scope="row"><label><?php _e('Show the Button','wp_superlike'); ?></label></th>
					<td>
						<select name="wpsuperlike-showbutton" id="wpsuperlike-showbutton">
							<option value="0" <?php selected(0, $showbutton); ?>><?php _e('Use global default','ocart'); ?></option>
							<option value="-1" <?php selected(-1, $showbutton); ?>><?php _e('Do not show','ocart'); ?></option>
							<option value="1" <?php selected(1, $showbutton); ?>><?php _e('Show','ocart'); ?></option>
						</select><p style="color:#666666"><small><?php _e('Note that for this option to be applicable, you must disable the Like Counter from Theme Option Panel - Options-Post - Share Post Box - Like Button and enable Automatically show the like button in every post on WP-SuperLike Settings','wp_superlike'); ?></small></p>

					</td>
				</tr>
				<tr valign="top">
					<th scope="row"><label><?php _e('Override Button Theme','wp_superlike'); ?></label></th>
					<td>
						<select name="wpsuperlike-style" id="wpsuperlike-style">
							<option value="" <?php selected('', $style); ?>><?php _e('Use active theme','ocart'); ?></option>
						<?php
							$_builtin_styles = apply_filters('wp_superlike_builtin_styles', wp_superlike_builtin_styles() );
							foreach($_builtin_styles as $_builtin_style) {
						?>
							<option value="<?php echo $_builtin_style['slug']; ?>" <?php selected($_builtin_style['slug'], $style); ?>><?php echo $_builtin_style['name']; ?></option>
						<?php
							}
						?>
						</select>
					</td>
				</tr>
			</table>
		</div>
	
<?php
}

/* save metabox */
add_action( 'save_post', 'wp_superlike_save_metabox' );  
function wp_superlike_save_metabox( $post_id ) {

	global $wp_superlike;

	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
	if( !current_user_can( 'edit_post', $post_id ) ) return;
	
	if (isset($_POST['wpsuperlike-style']))
		update_post_meta($post_id, 'style', $_POST['wpsuperlike-style']);
		
	if (isset($_POST['wpsuperlike-showbutton']))
		update_post_meta($post_id, 'showbutton', $_POST['wpsuperlike-showbutton']);

}