Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
4 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
6 months ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
4 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
7 months ago
StackedImages.php
4 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
Testimonials.php
257 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Testimonials module for FrontBlocks. |
| 4 | * |
| 5 | * @package FrontBlocks |
| 6 | * @author David Perez <david@close.technology> |
| 7 | * @copyright 2025 Closemarketing |
| 8 | * @version 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace FrontBlocks\Frontend; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Testimonials class. |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | class Testimonials { |
| 21 | /** |
| 22 | * Option key for testimonials feature. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private $option_enable_testimonials = 'enable_testimonials'; |
| 27 | |
| 28 | /** |
| 29 | * Page slug. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | private $page_slug = 'frontblocks-settings'; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | if ( ! $this->is_testimonials_enabled() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Frontend hooks. |
| 44 | add_action( 'init', array( $this, 'register_cpt_testimonials' ) ); |
| 45 | add_action( 'add_meta_boxes', array( $this, 'add_metabox_stars' ) ); |
| 46 | add_action( 'save_post', array( $this, 'save_metabox_stars' ) ); |
| 47 | add_shortcode( 'frontblocks_testimonials_carousel', array( $this, 'testimonials_shortcode' ) ); |
| 48 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Enqueue scripts. |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public function enqueue_scripts() { |
| 57 | wp_register_style( |
| 58 | 'frontblocks-testimonials-style', |
| 59 | FRBL_PLUGIN_URL . 'assets/testimonials/frontblocks-testimonials.css', |
| 60 | array(), |
| 61 | FRBL_VERSION |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check if testimonials are enabled. |
| 67 | * |
| 68 | * @return bool |
| 69 | */ |
| 70 | private function is_testimonials_enabled() { |
| 71 | $options = get_option( 'frontblocks_settings', array() ); |
| 72 | return (bool) ( $options[ $this->option_enable_testimonials ] ?? false ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Render checkbox field for enable testimonials. |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function field_enable_testimonials() { |
| 81 | $options = get_option( 'frontblocks_settings', array() ); |
| 82 | $enabled = (bool) ( $options[ $this->option_enable_testimonials ] ?? false ); |
| 83 | echo '<label for="' . esc_attr( $this->option_enable_testimonials ) . '">'; |
| 84 | echo '<input type="checkbox" id="' . esc_attr( $this->option_enable_testimonials ) . '" name="frontblocks_settings[' . esc_attr( $this->option_enable_testimonials ) . ']" value="1" ' . checked( true, $enabled, false ) . ' /> '; |
| 85 | echo esc_html__( 'Enable testimonials', 'frontblocks' ); |
| 86 | echo '</label>'; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Register testimonials custom post type. |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
| 94 | public function register_cpt_testimonials() { |
| 95 | $labels = array( |
| 96 | 'name' => _x( 'Testimonials', 'Post Type General Name', 'frontblocks' ), |
| 97 | 'singular_name' => _x( 'Testimonial', 'Post Type Singular Name', 'frontblocks' ), |
| 98 | 'menu_name' => __( 'Testimonials', 'frontblocks' ), |
| 99 | 'all_items' => __( 'All Testimonials', 'frontblocks' ), |
| 100 | 'add_new_item' => __( 'Add New Testimonial', 'frontblocks' ), |
| 101 | 'add_new' => __( 'Add New', 'frontblocks' ), |
| 102 | 'new_item' => __( 'New Testimonial', 'frontblocks' ), |
| 103 | 'edit_item' => __( 'Edit Testimonial', 'frontblocks' ), |
| 104 | 'update_item' => __( 'Update Testimonial', 'frontblocks' ), |
| 105 | 'view_item' => __( 'View Testimonial', 'frontblocks' ), |
| 106 | 'view_items' => __( 'View Testimonials', 'frontblocks' ), |
| 107 | 'search_items' => __( 'Search Testimonial', 'frontblocks' ), |
| 108 | ); |
| 109 | |
| 110 | $args = array( |
| 111 | 'label' => __( 'Testimonials', 'frontblocks' ), |
| 112 | 'labels' => $labels, |
| 113 | 'supports' => array( 'title', 'editor', 'thumbnail' ), |
| 114 | 'public' => true, |
| 115 | 'show_ui' => true, |
| 116 | 'show_in_menu' => true, |
| 117 | 'menu_position' => 5, |
| 118 | 'menu_icon' => 'dashicons-testimonial', |
| 119 | 'can_export' => true, |
| 120 | ); |
| 121 | |
| 122 | register_post_type( 'fbrl_testimonial', $args ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Add metabox for star rating. |
| 127 | * |
| 128 | * @return void |
| 129 | */ |
| 130 | public function add_metabox_stars() { |
| 131 | add_meta_box( |
| 132 | 'testimonial_rating_metabox', |
| 133 | __( 'Rating (Stars)', 'frontblocks' ), |
| 134 | array( $this, 'render_metabox_stars' ), |
| 135 | 'fbrl_testimonial', |
| 136 | 'side', |
| 137 | 'high' |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Render metabox content. |
| 143 | * |
| 144 | * @param WP_Post $post Post object. |
| 145 | * @return void |
| 146 | */ |
| 147 | public function render_metabox_stars( $post ) { |
| 148 | wp_nonce_field( basename( __FILE__ ), 'frontblocks_testimonials_nonce' ); |
| 149 | $stars = get_post_meta( $post->ID, 'frontblocks_stars', true ); |
| 150 | ?> |
| 151 | <p> |
| 152 | <label for="frontblocks_stars"><?php esc_html_e( 'Select rating:', 'frontblocks' ); ?></label> |
| 153 | <select name="frontblocks_stars" id="frontblocks_stars"> |
| 154 | <option value="0" <?php selected( $stars, 0 ); ?>><?php esc_html_e( '0 Stars', 'frontblocks' ); ?></option> |
| 155 | <option value="1" <?php selected( $stars, 1 ); ?>><?php esc_html_e( '1 Star', 'frontblocks' ); ?></option> |
| 156 | <option value="2" <?php selected( $stars, 2 ); ?>><?php esc_html_e( '2 Stars', 'frontblocks' ); ?></option> |
| 157 | <option value="3" <?php selected( $stars, 3 ); ?>><?php esc_html_e( '3 Stars', 'frontblocks' ); ?></option> |
| 158 | <option value="4" <?php selected( $stars, 4 ); ?>><?php esc_html_e( '4 Stars', 'frontblocks' ); ?></option> |
| 159 | <option value="5" <?php selected( $stars, 5 ); ?>><?php esc_html_e( '5 Stars', 'frontblocks' ); ?></option> |
| 160 | </select> |
| 161 | </p> |
| 162 | <?php |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Save metabox data. |
| 167 | * |
| 168 | * @param int $post_id Post ID. |
| 169 | * @return void |
| 170 | */ |
| 171 | public function save_metabox_stars( $post_id ) { |
| 172 | if ( ! isset( $_POST['frontblocks_testimonials_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['frontblocks_testimonials_nonce'] ) ), basename( __FILE__ ) ) ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if ( isset( $_POST['frontblocks_stars'] ) ) { |
| 185 | update_post_meta( $post_id, 'frontblocks_stars', (int) $_POST['frontblocks_stars'] ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Testimonials carousel shortcode. |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | public function testimonials_shortcode() { |
| 195 | wp_enqueue_style( 'frontblocks-testimonials-style' ); |
| 196 | $args = array( |
| 197 | 'post_type' => 'fbrl_testimonial', |
| 198 | 'posts_per_page' => -1, |
| 199 | 'orderby' => 'date', |
| 200 | 'order' => 'DESC', |
| 201 | ); |
| 202 | |
| 203 | $query_testimonios = new \WP_Query( $args ); |
| 204 | ob_start(); |
| 205 | |
| 206 | if ( ! $query_testimonios->have_posts() ) { |
| 207 | return ''; |
| 208 | } |
| 209 | ?> |
| 210 | <div class="frontblocks-carousel frontblocks-testimonials-carousel" data-type="carousel" data-view="4" data-res-view="1" data-buttons="bullets" data-autoplay="6000"> |
| 211 | <?php |
| 212 | while ( $query_testimonios->have_posts() ) : |
| 213 | $query_testimonios->the_post(); |
| 214 | $nombre = get_the_title(); |
| 215 | $texto_resena = get_the_content(); |
| 216 | $imagen_url = get_the_post_thumbnail_url( get_the_ID(), 'full' ); |
| 217 | |
| 218 | $stars = get_post_meta( get_the_ID(), 'frontblocks_stars', true ); |
| 219 | |
| 220 | $stars = $stars ? intval( $stars ) : 0; |
| 221 | $stars_html = ''; |
| 222 | |
| 223 | for ( $i = 1; $i <= 5; $i++ ) { |
| 224 | if ( $i <= $stars ) { |
| 225 | $stars_html .= '<span class="star filled">� |
| 226 | </span>'; |
| 227 | } else { |
| 228 | $stars_html .= '<span class="star">� |
| 229 | </span>'; |
| 230 | } |
| 231 | } |
| 232 | ?> |
| 233 | <div class="testimonial-card"> |
| 234 | <div class="testimonial-header"> |
| 235 | <?php if ( $imagen_url ) : ?> |
| 236 | <div class="testimonial-image-container"> |
| 237 | <img src="<?php echo esc_url( $imagen_url ); ?>" alt="<?php echo esc_attr( $nombre ); ?>" class="testimonial-image" /> |
| 238 | </div> |
| 239 | <?php endif; ?> |
| 240 | </div> |
| 241 | <div class="testimonial-content"> |
| 242 | <h3 class="testimonial-name"><?php echo esc_html( $nombre ); ?></h3> |
| 243 | <p class="testimonial-text">"<?php echo esc_html( $texto_resena ); ?>"</p> |
| 244 | <div class="stars-container"> |
| 245 | <?php echo $stars_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 246 | </div> |
| 247 | </div> |
| 248 | </div> |
| 249 | <?php |
| 250 | endwhile; |
| 251 | ?> |
| 252 | </div> |
| 253 | <?php |
| 254 | wp_reset_postdata(); |
| 255 | return ob_get_clean(); |
| 256 | } |
| 257 | } |