<?php
/**
 * Writer Ancora Framework: Testimonial support
 *
 * @package	writer_ancora
 * @since	writer_ancora 1.0
 */

// Theme init
if (!function_exists('writer_ancora_testimonial_theme_setup')) {
	add_action( 'writer_ancora_action_before_init_theme', 'writer_ancora_testimonial_theme_setup', 1 );
	function writer_ancora_testimonial_theme_setup() {

		// Add item in the admin menu
		add_filter('trx_utils_filter_override_options',		'writer_ancora_testimonial_add_override_options');

		// Save data from override options
		add_action('save_post',				'writer_ancora_testimonial_save_data');

		// Override options fields
		writer_ancora_storage_set('testimonial_override_options', array(
			'id' => 'testimonial-override-options',
			'title' => esc_html__('Testimonial Details', 'writer-ancora'),
			'page' => 'testimonial',
			'context' => 'normal',
			'priority' => 'high',
			'fields' => array(
				"testimonial_author" => array(
					"title" => esc_html__('Testimonial author',  'writer-ancora'),
					"desc" => wp_kses( __("Name of the testimonial's author", 'writer-ancora'), writer_ancora_storage_get('allowed_tags') ),
					"class" => "testimonial_author",
					"std" => "",
					"type" => "text"),
				"testimonial_position" => array(
					"title" => esc_html__("Author's position",  'writer-ancora'),
					"desc" => wp_kses( __("Position of the testimonial's author", 'writer-ancora'), writer_ancora_storage_get('allowed_tags') ),
					"class" => "testimonial_author",
					"std" => "",
					"type" => "text"),
				"testimonial_email" => array(
					"title" => esc_html__("Author's e-mail",  'writer-ancora'),
					"desc" => wp_kses( __("E-mail of the testimonial's author - need to take Gravatar (if registered)", 'writer-ancora'), writer_ancora_storage_get('allowed_tags') ),
					"class" => "testimonial_email",
					"std" => "",
					"type" => "text"),
				"testimonial_link" => array(
					"title" => esc_html__('Testimonial link',  'writer-ancora'),
					"desc" => wp_kses( __("URL of the testimonial source or author profile page", 'writer-ancora'), writer_ancora_storage_get('allowed_tags') ),
					"class" => "testimonial_link",
					"std" => "",
					"type" => "text")
				)
			)
		);

		// Add supported data types
		writer_ancora_theme_support_pt('testimonial');
		writer_ancora_theme_support_tx('testimonial_group');

	}
}


// Add override options
if (!function_exists('writer_ancora_testimonial_add_override_options')) {
	function writer_ancora_testimonial_add_override_options($boxes = array()) {
        $boxes[] = array_merge(writer_ancora_storage_get('testimonial_override_options'), array('callback' => 'writer_ancora_testimonial_show_override_options'));
        return $boxes;
	}
}

// Callback function to show fields in override options
if (!function_exists('writer_ancora_testimonial_show_override_options')) {
	function writer_ancora_testimonial_show_override_options() {
		global $post;

		// Use nonce for verification
		echo '<input type="hidden" name="override_options_testimonial_nonce" value="'.esc_attr(wp_create_nonce(admin_url())).'" />';

		$data = get_post_meta($post->ID, 'writer_ancora_testimonial_data', true);

		$fields = writer_ancora_storage_get_array('testimonial_override_options', 'fields');
		?>
		<table class="testimonial_area">
		<?php
		if (is_array($fields) && count($fields) > 0) {
			foreach ($fields as $id=>$field) {
				$meta = isset($data[$id]) ? $data[$id] : '';
				?>
				<tr class="testimonial_field <?php echo esc_attr($field['class']); ?>" valign="top">
					<td><label for="<?php echo esc_attr($id); ?>"><?php echo esc_html($field['title']); ?></label></td>
					<td><input type="text" name="<?php echo esc_attr($id); ?>" id="<?php echo esc_attr($id); ?>" value="<?php echo esc_attr($meta); ?>" size="30" />
						<br><small><?php echo esc_html($field['desc']); ?></small></td>
				</tr>
				<?php
			}
		}
		?>
		</table>
		<?php
	}
}


// Save data from override options
if (!function_exists('writer_ancora_testimonial_save_data')) {
	function writer_ancora_testimonial_save_data($post_id) {
		// verify nonce
		if ( !wp_verify_nonce( writer_ancora_get_value_gp('override_options_testimonial_nonce'), admin_url() ) )
			return $post_id;

		// check autosave
		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
			return $post_id;
		}

		// check permissions
		if ($_POST['post_type']!='testimonial' || !current_user_can('edit_post', $post_id)) {
			return $post_id;
		}

		$data = array();

		$fields = writer_ancora_storage_get_array('testimonial_override_options', 'fields');

		// Post type specific data handling
		if (is_array($fields) && count($fields) > 0) {
			foreach ($fields as $id=>$field) {
				if (isset($_POST[$id]))
					$data[$id] = stripslashes($_POST[$id]);
			}
		}

		update_post_meta($post_id, 'writer_ancora_testimonial_data', $data);
	}
}
?>