*
*/
class AffiGet_Review_Element_Star_Ratings extends AffiGet_Abstract_Element
{
const DEFAULT_RATING = 4;
function __construct( AffiGet_Abstract_Meta $meta, $name, array $params ){
parent::__construct( $meta, $name, $params ); //calls resolve_settings(), which calls get_settings_config()
if( ! $this->is_status( AffiGet_Abstract_Element::STATUS_ENABLED ) ) return;
$element_name = $this->name;
add_action("afg_front__html_{$element_name}", array(&$this, 'front_html'), 10, 1);
//metavalue
$meta_key = AFG_META_PREFIX . $this->name;
add_filter( "sanitize_post_meta_{$meta_key}", array(&$this, 'sanitize_value' ), 10, 3);
//metabox
add_action( 'afg_review_renderer__register_metabox_fields', array(&$this, 'register_cmb2_fields'));
add_action( "cmb2_render_{$this->control_id}", array(&$this, 'render_cmb2_field'), 10, 5 );
add_filter( "cmb2_types_esc_{$this->control_id}", array(&$this, 'escape_cmb2_field'), 10, 4 );
//add_filter( "cmb2_sanitize_{$this->control_id}", array(&$this, 'sanitize_cmb2_field'), 10, 5 );
//assets
add_action( 'admin_enqueue_scripts', array(&$this, 'enqueue_scripts_and_styles'));
add_action( 'wp_enqueue_scripts', array(&$this, 'enqueue_scripts_and_styles'));
}
protected function get_settings_config(){
$new_fields = array(
'aspects' => array(
'name' => 'aspects',
'atts' => 'size="55"',
'type' => 'text',
'default' => _x('Rating', 'a comma-separated list of supported rating aspects', 'afg'),
'label' => __('Rating aspects', 'afg'),
'hint' => __('A comma-separated list, e.g. Design, Quality, Service, Price.', 'afg'),
'help' => __('Help'),
),
'ranks' => array(
'name' => 'ranks',
'atts' => 'size="55"',
'type' => 'text',
'default' => __('Bad, Poor, Normal, Good, Perfect', 'afg'),
'label' => __('Rating scores', 'afg'),
'hint' => __('A comma-separated list, e.g. Bad, Poor, Normal, Good, Perfect.', 'afg'),
'help' => __('Help'),
),
//images_path must contain files: star-on.png, star-off.png, star-half.png, star-rating.png
'images_path' => array(
'name' => 'images_path',
'atts' => 'size="70"',
'type' => 'text',
'default' => plugin_dir_url(__FILE__) . 'libs/raty/images/',
'label' => __('Path to images', 'afg'),
'hint' => sprintf(__('Must contain images: %s', 'afg'), 'star-on.png, star-off.png, star-half.png, star-rating.png.'),
'help' => __('Help'),
),
'display_format' => array(
'name' => 'display_format',
'atts' => '',
'type' => 'dropdown',
'options' => array(
'table' => __('Table - labels in first column (default)', 'afg'), /* labels in the first column */
'list' => __('List - labels in front of values', 'afg'), /* labels before values */
'form' => __('Form - small labels below values', 'afg'), /* small labels under values */
'divs' => __('Generic - divs and spans', 'afg'), /* divs and spans */
),
'default' => 'table',
'label' => __('', 'afg'),
'hint' => __('', 'afg'),
'help' => __('Help'),
),
);
return array_merge( parent::get_settings_config(), $new_fields );
}
function register_cmb2_fields( &$fields ){
/*$fields[] = array(
'name' => $this->settings['title'],
//'desc' => __('Title to show on the front end.', 'afg'),
'id' => AFG_META_PREFIX . $this->name .'_heading',
'type' => 'title',
'position'=> $this->settings[ 'metabox_position' ],
//'default' => $this->settings['title'],
);*/
$post_type_name = $this->meta->post_type_name;
$fields[] = array(
'name' => 'value',
'desc' => $this->settings['description'],
'id' => '_' . AFG_META_PREFIX . $this->name, //prefixed with underscore to avoid showing a new entry in Custom fields
'type' => 'afg_star_ratings',
//'options' => array( 'textarea_rows' => $this->settings[ 'textarea_rows' ] ),
'position'=> $this->settings[ 'metabox_position' ],
'metabox' => array(
'id' => $this->control_id. '_metabox',
'title' => $this->settings['label'],
'object_types' => array( $post_type_name ), // Post type
'context' => 'side',
'priority' => 'low', //high/core/default/low
'show_names' => false, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // true to keep the metabox closed by default
)
);
}
function front_html( array $review_data ){
$post_id = $review_data['post_fields']['ID'];
$nonce = '';
if ( current_user_can('edit_post', $post_id )){
$nonce = wp_create_nonce("afg-update-{$post_id}");
}
ob_start();
$this->render_html( $post_id, null, $this->name, AFG_META_PREFIX . $this->name, $nonce );
$content = ob_get_clean();
if( $content ){
return $this->front_title() . $content;
}
return;
}
function render_cmb2_field( $field_args, $value, $post_id, $object_type, $field_type_object ){
if( $field_args->args['id'] === '_'.AFG_META_PREFIX . $this->name ){
$nonce = '';
if ( current_user_can('edit_post', $post_id )){
$nonce = wp_create_nonce("afg-update-{$post_id}");
}
$this->render_html( $post_id, null, $this->name, AFG_META_PREFIX . $this->name, $nonce );
}
}
function escape_cmb2_field( $null, $meta_value, $args, $field ){
//if any non-null value is returned, dumb default escaping is avoided
//afg_log(__METHOD__, compact('null', 'meta_value', 'args', 'field'));
//short-circuit default escaping, as it results in PHP notices
//(because passed values cannot be easily converted to string)
return true;
}
function sanitize_cmb2_field( $override_value, $value, $object_id, $field_args, $sanitizer_object ){
$value = $this->sanitize_value( $value, AFG_META_PREFIX . $this->name, 'post' );
return $value;
}
protected function _prepare_items( &$items, $post_id, $fieldname ){
$encoded = false;
if( is_null( $items )){
if( $post_id && $fieldname == $this->name ){
$review_data = $this->meta->load_post( $post_id );
$items = $this->meta->pick_value( $review_data, $fieldname );
}
if( empty( $items )){
$items = $this->get_default_value( compact( 'post_id', 'fieldname' ) );
}
$encoded = json_encode( $items );
} elseif( is_string( $items )){
$encoded = $items;
$items = json_decode( $items, $assoc = true );
} else {
$encoded = json_encode( $items );
}
return $encoded;
}
function render_html( $post_id, $items, $fieldname, $input_id, $nonce, $context = 'not-widget', $params = null ){
$format = $this->settings['display_format'];
printf('
| _render_stars( $aspect, $ranks, $path ); ?> |