1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
add_action( 'cmb2_admin_init', 'create_review' ); /** * Hook in and register a metabox for the admin comment edit page. */ function create_review() { $review = new_cmb2_box( array( 'id' => 'review_box', 'title' => __( 'Recenzja', 'cmb2' ), 'object_types' => array( 'post', ), // Post type 'context' => 'normal', 'priority' => 'high', 'show_names' => true, // Show field names on the left 'cmb_styles' => true, // false to disable the CMB stylesheet ) ); $review->add_field( array( 'name' => 'Pozycja recenzji', 'id' => 'review_position', 'desc' => 'Wybierz pozycję wyświetlania recenzji', 'type' => 'select', 'default' => 'after', 'options_cb' => 'display_review_position_options', 'render_row_cb' => 'display_review_position_fields', ) ); $review->add_field( array( 'name' => 'Tytuł recenzji', 'desc' => 'Wyświetlane nad recenzją', 'id' => 'review_title', 'type' => 'text', ) ); $review->add_field( array( 'name' => 'Typ oceny', 'id' => 'review_type', 'desc' => 'Wybierz w jaki sposób będzie pokazywana ocena produktu', 'type' => 'select', 'default' => 'hundred_format', 'options_cb' => 'review_type_options', ) ); $review->add_field( array( 'name' => 'Tytuł podsumowania', 'desc' => 'Wyświetlane na końcu recenzji', 'id' => 'review_summary_title', 'type' => 'text', ) ); $rate_group = $review->add_field( array( 'id' => 'rate_group', 'type' => 'group', 'repeatable' => true, // use false if you want non-repeatable group 'options' => array( 'group_title' => __( 'Kryterium {#}', 'cmb2' ), // since version 1.1.4, {#} gets replaced by row number 'add_button' => __( 'Dodaj kolejne kryteriu', 'cmb2' ), 'remove_button' => __( 'Usuń kryterium', 'cmb2' ), 'sortable' => false, 'closed' => true, // true to have the groups closed by default ), ) ); $review->add_group_field($rate_group, array( 'name' => 'Kryterium', 'desc' => 'Wpisz tutaj kryterium oceny', 'id' => 'rate_label', 'type' => 'text', 'repeatable' => true, 'render_row_cb' => 'display_rate_fields', ) ); $review->add_group_field($rate_group, array( 'name' => 'Ocena', 'desc' => 'Wpisz tutaj ocenę', 'id' => 'rate_val', 'type' => 'text', 'render_row_cb' => 'display_rate_fields', 'attributes' => array( 'type' => 'number', ), ) ); } function review_type_options() { return array( 'percent' => __( '1-100%', 'cmb2' ), 'ten' => __( '1-10', 'cmb2' ), 'hundred' => __( '1-100', 'cmb2' ), ); } function display_review_position_options() { return array( 'none' => __( 'Nie wyświetlaj', 'cmb2' ), 'before' => __( 'Przed treścią wpisu', 'cmb2' ), 'after' => __( 'Po treści wpisu', 'cmb2' ), 'custom' => __( 'Własna', 'cmb2' ), ); } function display_rate_fields( $field_args, $field ) { $id = $field->args( 'id' ); $label = $field->args( 'name' ); $name = $field->args( '_name' ); $value = $field->escaped_value(); $description = $field->args( 'description' ); $type = $field->args( 'attributes', 'type') ? $field->args( 'attributes', 'type') : 'text'; ?> <div style="display: inline-block; margin-right: 20px;"> <label for="<?php echo $id; ?>"><?php echo $label; ?> <input id="<?php echo $id; ?>" type="<? echo $type ?>" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/> </label><br> <em>(<?php echo $description; ?>)</em> </div> <? } function display_review_position_fields( $f_args, $f ) { $name = $f_args['name']; $desc = $f_args['desc']; $id = $f_args['id']; $options = display_review_position_options(); $value = $f->escaped_value(); $post_id = $f->object_id; ?> <div class="cmb-row cmb-type-select cmb2-id-review-type" data-fieldtype="select"> <div class="cmb-th"> <label for="<?=$id?>"><?=$name?></label> </div> <div class="cmb-td"> <select class="cmb2_select" name="<?=$id?>" id="<?=$id?>" data-hash="1gnotga895ig"> <?php foreach( $options as $k => $v ) : ?> <option value="<?=$k?>"<?=$k==$value ? ' selected="selected"' : ''?>><?=$v?></option> <?php endforeach; ?> </select> <p class="cmb2-metabox-description"><?=$desc?></p> <p class="review_shortcode"<?=$value != 'custom' ? 'style="display: none;"' : ''?>> <b>Wklej ten shortcode w dowolne miejsce w treści wpisu:</b><br> <input type="text" value='<?='[review id="'.$post_id.'"]'?>' disabled> </p> <script> jQuery(function ($) { $('#<?=$id?>').change(function(){ if ( $(this).val() == 'custom' ) { $('.review_shortcode').fadeIn(200); } else { $('.review_shortcode').fadeOut(200); } }); }); </script> </div> </div> <?php } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
function check_rate_val( $type, $val ) { switch ($type) { case 'percent': $min = 1; $max = 100; break; case 'ten': $min = 1; $max = 10; break; case 'hundred': $min = 1; $max = 100; break; } $rate_val = $val < $min ? $min : $val; $rate_val = $rate_val > $max ? $max : $rate_val; return $rate_val; } /* Tutaj zmieniacie strukturę HTML; zmienna $output */ function render_review( $id ) { $rating = get_post_meta( $id, 'rate_group', true); $tytul = get_post_meta( $id, 'review_title', true); $type = get_post_meta( $id, 'review_type', true); $summary = get_post_meta( $id, 'review_summary_title', true); $rate_sum = 0; $output = '<section class="ocena mt-3 '; $output .= $tytul==null ? 'd-none">' : '">' . "\n"; $output .= '<h3 class="ocena">' . $tytul . '</h3>' . "\n"; foreach ($rating as $rate) { $label = $rate['rate_label']; $val = check_rate_val( $type, $rate['rate_val'] ); $val = $val['val']; $rate_sum += $val; $val .= $type == 'percent' ? '%' : ''; $val .= $type == 'ten' ? '/10' : ''; $val .= $type == 'hundred' ? '/100' : ''; $output .= "<p>$label: <b>$val</b></p\n>"; } $review_average = $rate_sum / count($rating); $review_average .= $type == 'percent' ? '%' : ''; $review_average .= $type == 'ten' ? '/10' : ''; $review_average .= $type == 'hundred' ? '/100' : ''; $output .= $summary ? "<p>$summary: <b>$review_average</b></p>\n</section>" : "</section>"; return $output; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Create Shortcode review // Shortcode: [review id=""] function create_review_shortcode($atts) { $atts = shortcode_atts( array( 'id' => '', ), $atts, 'review' ); $id = $atts['id']; return get_post_meta( $id, 'review_position', true) == 'custom' ? render_review($id) : null; } add_shortcode( 'review', 'create_review_shortcode' ); |
1 |
<?php echo get_post_meta( $id, 'review_position', true) == 'before' ? render_review(get_the_ID()) : null; ?> |
1 |
<?php echo get_post_meta( $id, 'review_position', true) == 'after' ? render_review(get_the_ID()) : null; ?> |