get_option( "{$size}_size_w" ), 'height' => get_option( "{$size}_size_h" ), 'crop' => (bool) get_option( "{$size}_crop" ), ); } elseif ( isset( $wais[ $size ] ) ) { $sizes[ $size ] = array( 'width' => $wais[ $size ]['width'], 'height' => $wais[ $size ]['height'], 'crop' => $wais[ $size ]['crop'], ); } // Size registered, but has 0 width and height. if ( 0 === (int) $sizes[ $size ]['width'] && 0 === (int) $sizes[ $size ]['height'] ) { unset( $sizes[ $size ] ); } } } return $sizes; } } if ( ! function_exists( 'abr_get_image_size' ) ) { /** * Gets the data of a specific image size. * * @param string $size Name of the size. */ function abr_get_image_size( $size ) { if ( ! is_string( $size ) ) { return; } $sizes = abr_get_available_image_sizes(); return isset( $sizes[ $size ] ) ? $sizes[ $size ] : false; } } if ( ! function_exists( 'abr_get_list_available_image_sizes' ) ) { /** * Get the list available image sizes */ function abr_get_list_available_image_sizes() { $intermediate_image_sizes = get_intermediate_image_sizes(); $image_sizes = array(); foreach ( $intermediate_image_sizes as $size ) { $image_sizes[ $size ] = $size; $data = abr_get_image_size( $size ); if ( isset( $data['width'] ) || isset( $data['height'] ) ) { $width = '~'; $height = '~'; if ( isset( $data['width'] ) && $data['width'] ) { $width = $data['width'] . 'px'; } if ( isset( $data['height'] ) && $data['height'] ) { $height = $data['height'] . 'px'; } $image_sizes[ $size ] .= sprintf( ' [%s, %s]', $width, $height ); } } $image_sizes = apply_filters( 'abr_list_available_image_sizes', $image_sizes ); return $image_sizes; } } if ( ! function_exists( 'abr_get_post_metadata' ) ) { /** * Retrieves a post meta field for the given post ID. * * @param int $post_id Post ID. * @param string $key Optional. The meta key to retrieve. By default, returns * data for all keys. Default empty. * @param bool $single Optional. If true, returns only the first value for the specified meta key. * This parameter has no effect if $key is not specified. Default false. * @param mixed $default Default value. * @return mixed Will be an array if $single is false. Will be value of the meta * field if $single is true. */ function abr_get_post_metadata( $post_id, $key = '', $single = false, $default = null ) { if ( ! metadata_exists( 'post', $post_id, $key ) && $default ) { return $default; } return get_metadata( 'post', $post_id, $key, $single ); } } if ( ! function_exists( 'abr_default_post_types' ) ) { /** * Return default post types. */ function abr_default_post_types() { $types = array( 'post' => 'post', ); return apply_filters( 'abr_default_post_types', $types ); } } if ( ! function_exists( 'abr_default_indicators' ) ) { /** * Return default indicators. */ function abr_default_indicators() { $indicators = array( 0 => array( 'name' => 'None', ), 1 => array( 'name' => 'Awfully', ), 2 => array( 'name' => 'Very bad', ), 3 => array( 'name' => 'Bad', ), 4 => array( 'name' => 'Passably', ), 5 => array( 'name' => 'Neutral', ), 6 => array( 'name' => 'Normal', ), 7 => array( 'name' => 'Good', ), 8 => array( 'name' => 'Very good', ), 9 => array( 'name' => 'Amazing', ), 10 => array( 'name' => 'The best', ), ); return apply_filters( 'abr_default_indicators', $indicators ); } } if ( ! function_exists( 'abr_list_indicators' ) ) { /** * Return list indicators. */ function abr_list_indicators() { $indicators = abr_default_indicators(); // Disable all indicators. if ( get_option( 'abr_review_disable_indicators', false ) ) { $indicators = array(); } // Set custom name for indicators. foreach ( $indicators as $index => $value ) { $indicators[ $index ]['name'] = get_option( "abr_review_indicator_label_{$index}", $indicators[ $index ]['name'] ); } return $indicators; } } if ( ! function_exists( 'abr_the_review' ) ) { /** * Post Review * * @param int $post_id Post ID. */ function abr_the_review( $post_id = null ) { if ( ! $post_id ) { $post_id = get_the_ID(); } if ( ! $post_id ) { return; } $post_type = get_post_type( $post_id ); // Get types of option. $option_types = get_option( 'abr_review_post_types', abr_default_post_types() ); if ( ! in_array( $post_type, $option_types, true ) ) { return; } // Check display. if ( ! abr_get_post_metadata( $post_id, '_abr_review_settings', true ) ) { return; } // Params. $params = array( 'type' => abr_get_post_metadata( $post_id, '_abr_review_type', true, 'percentage' ), 'items' => abr_get_post_metadata( $post_id, '_abr_review_items', true, array() ), 'heading' => abr_get_post_metadata( $post_id, '_abr_review_heading', true ), 'desc' => abr_get_post_metadata( $post_id, '_abr_review_desc', true ), 'main_scale' => abr_get_post_metadata( $post_id, '_abr_review_main_scale', true, true ), 'total_score_number' => abr_get_post_metadata( $post_id, '_abr_review_total_score_number', true ), 'pros_heading' => abr_get_post_metadata( $post_id, '_abr_review_pros_heading', true ), 'pros_items' => abr_get_post_metadata( $post_id, '_abr_review_pros_items', true, array() ), 'cons_heading' => abr_get_post_metadata( $post_id, '_abr_review_cons_heading', true ), 'cons_items' => abr_get_post_metadata( $post_id, '_abr_review_cons_items', true, array() ), 'legend' => abr_get_post_metadata( $post_id, '_abr_review_legend', true ), 'schema_heading' => abr_get_post_metadata( $post_id, '_abr_review_schema_heading', true ), 'schema_desc' => abr_get_post_metadata( $post_id, '_abr_review_schema_desc', true ), 'schema_author' => abr_get_post_metadata( $post_id, '_abr_review_schema_author', true ), 'schema_author_custom' => abr_get_post_metadata( $post_id, '_abr_review_schema_author_custom', true ), ); abr_review_display_block( $params ); } } if ( ! function_exists( 'abr_get_review' ) ) { /** * Get review of post * * @param bool $format Do you format the result?. * @param int $post_id Post ID. */ function abr_get_review( $format = false, $post_id = null ) { if ( ! $post_id ) { $post_id = get_the_ID(); } if ( ! $post_id ) { return 0; } if ( ! abr_get_post_metadata( $post_id, '_abr_review_settings', true ) ) { return 0; } $review_type = abr_get_post_metadata( $post_id, '_abr_review_type', true, 'percentage' ); $total_score = (float) abr_get_post_metadata( $post_id, '_abr_review_total_score_number', true ); // Vars. switch ( $review_type ) { case 'percentage': $max = 100; break; case 'point-5': $max = 5; break; case 'point-10': $max = 10; break; case 'star': $max = 5; break; } // Formating value. if ( $total_score && $format ) { if ( 'percentage' === $review_type ) { $total_score = sprintf( '%s%%', $total_score ); } else { $total_score = sprintf( '%s/%s', $total_score, $max ); } } return $total_score; } } if ( ! function_exists( 'abr_review_get_val_index' ) ) { /** * Get value index for post review * * @param string $type The type of review. * @param float $value The value. */ function abr_review_get_val_index( $type, $value ) { $val_index = $value; if ( 'star' === $type ) { $val_index = round( $value * 2 - 1 ); } if ( 'point-5' === $type ) { $val_index = round( $value * 2 - 1 ); } if ( 'percentage' === $type ) { $val_index = round( $value / 10 ); } return $val_index; } } if ( ! function_exists( 'abr_review_get_type' ) ) { /** * Get type review of post * * @param int $post_id Post ID. * @param mixed $default Return default. */ function abr_review_get_type( $post_id = null, $default = false ) { if ( ! $post_id ) { $post_id = get_the_ID(); } if ( ! $post_id ) { return $default; } return abr_get_post_metadata( $post_id, '_abr_review_type', true, $default ); } } if ( ! function_exists( 'abr_review_star_rating' ) ) { /** * Output a HTML element with a star rating for a given rating. * * @param array $args { * Optional. Array of star ratings arguments. * * @type int|float $rating The rating to display, expressed in either a 0.5 rating increment, * or percentage. Default 0. * @type string $type Format that the $rating is in. Valid values are 'rating' (default), * or, 'percent'. Default 'rating'. * @type int $number The number of ratings that makes up this rating. Default 0. * @type bool $echo Whether to echo the generated markup. False to return the markup instead * of echoing it. Default true. * } */ function abr_review_star_rating( $args = array() ) { $defaults = array( 'rating' => 0, 'type' => 'rating', 'number' => 0, 'echo' => true, ); $r = wp_parse_args( $args, $defaults ); // Non-English decimal places when the $rating is coming from a string. $rating = (float) str_replace( ',', '.', $r['rating'] ); // Convert Percentage to star rating, 0..5 in .5 increments. if ( 'percent' === $r['type'] ) { $rating = round( $rating / 10, 0 ) / 2; } // Calculate the number of each type of star needed. $full_stars = floor( $rating ); $half_stars = ceil( $rating - $full_stars ); $empty_stars = 5 - $full_stars - $half_stars; if ( $r['number'] ) { /* translators: 1: the rating, 2: the number of ratings */ $format = _n( '%1$s rating based on %2$s rating', '%1$s rating based on %2$s ratings', $r['number'] ); $title = sprintf( $format, number_format_i18n( $rating, 1 ), number_format_i18n( $r['number'] ) ); } else { /* translators: %s: the rating */ $title = sprintf( __( '%s rating' ), number_format_i18n( $rating, 1 ) ); } $output = '
'; if ( $r['echo'] ) { echo (string) $output; // XSS. } return $output; } } if ( ! function_exists( 'abr_review_display_rating' ) ) { /** * Display review rating info * * @param string $type The type of review. * @param float $max The max value. * @param float $value The value. * @param string $name The name. * @param bool $label Display label. */ function abr_review_display_rating( $type, $max, $value, $name = null, $label = true ) { switch ( $type ) { case 'star': $value = ( $value <= $max ) ? round( $value, 1 ) : $max; break; case 'point-5': case 'point-10': case 'percentage': $value = ( $value <= $max ) ? round( $value ) : $max; break; } // Get indicators. $indicators = abr_list_indicators(); // Set value index. $val_index = abr_review_get_val_index( $type, $value ); ?>