FormTemplates.php
449 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPForms\Admin\Traits; |
| 4 | |
| 5 | /** |
| 6 | * Form Templates trait. |
| 7 | * |
| 8 | * @since 1.7.7 |
| 9 | */ |
| 10 | trait FormTemplates { |
| 11 | |
| 12 | // phpcs:disable WPForms.PHP.BackSlash.UseShortSyntax |
| 13 | /** |
| 14 | * Addons data handler class instance. |
| 15 | * |
| 16 | * @since 1.7.7 |
| 17 | * |
| 18 | * @var \WPForms\Admin\Addons\Addons |
| 19 | */ |
| 20 | private $addons_obj; |
| 21 | // phpcs:enable WPForms.PHP.BackSlash.UseShortSyntax |
| 22 | |
| 23 | /** |
| 24 | * Is addon templates available? |
| 25 | * |
| 26 | * @since 1.7.7 |
| 27 | * |
| 28 | * @var bool |
| 29 | */ |
| 30 | private $is_addon_templates_available = false; |
| 31 | |
| 32 | /** |
| 33 | * Is custom templates available? |
| 34 | * |
| 35 | * @since 1.7.7 |
| 36 | * |
| 37 | * @var bool |
| 38 | */ |
| 39 | private $is_custom_templates_available = false; |
| 40 | |
| 41 | /** |
| 42 | * Prepared templates list. |
| 43 | * |
| 44 | * @since 1.7.7 |
| 45 | * |
| 46 | * @var array |
| 47 | */ |
| 48 | private $prepared_templates = []; |
| 49 | |
| 50 | /** |
| 51 | * Output templates content section. |
| 52 | * |
| 53 | * @since 1.7.7 |
| 54 | */ |
| 55 | private function output_templates_content() { |
| 56 | |
| 57 | $this->prepare_templates_data(); |
| 58 | ?> |
| 59 | |
| 60 | <div class="wpforms-setup-templates"> |
| 61 | <div class="wpforms-setup-templates-sidebar"> |
| 62 | |
| 63 | <div class="wpforms-setup-templates-search-wrap"> |
| 64 | <i class="fa fa-search"></i> |
| 65 | <label> |
| 66 | <input type="text" id="wpforms-setup-template-search" value="" placeholder="<?php esc_attr_e( 'Search Templates', 'wpforms-lite' ); ?>"> |
| 67 | </label> |
| 68 | </div> |
| 69 | |
| 70 | <ul class="wpforms-setup-templates-categories"> |
| 71 | <?php $this->template_categories(); ?> |
| 72 | </ul> |
| 73 | |
| 74 | </div> |
| 75 | |
| 76 | <div id="wpforms-setup-templates-list"> |
| 77 | <div class="list"> |
| 78 | <?php $this->template_select_options(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 79 | </div> |
| 80 | <div class="wpforms-templates-no-results"> |
| 81 | <p> |
| 82 | <?php esc_html_e( 'Sorry, we didn\'t find any templates that match your criteria.', 'wpforms-lite' ); ?> |
| 83 | </p> |
| 84 | </div> |
| 85 | </div> |
| 86 | </div> |
| 87 | <?php |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Prepare templates data for output. |
| 92 | * |
| 93 | * @since 1.7.7 |
| 94 | */ |
| 95 | private function prepare_templates_data() { |
| 96 | |
| 97 | $templates = wpforms()->get( 'builder_templates' )->get_templates(); |
| 98 | |
| 99 | if ( empty( $templates ) ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Loop through each available template. |
| 104 | foreach ( $templates as $id => $template ) { |
| 105 | |
| 106 | $this->prepared_templates[ $id ] = $this->prepare_template_render_arguments( $template ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Generate and display categories menu. |
| 112 | * |
| 113 | * @since 1.7.7 |
| 114 | */ |
| 115 | private function template_categories() { |
| 116 | |
| 117 | $templates_count = $this->get_count_in_categories(); |
| 118 | |
| 119 | $generic_categories = [ |
| 120 | 'all' => esc_html__( 'All Templates', 'wpforms-lite' ), |
| 121 | ]; |
| 122 | |
| 123 | if ( isset( $templates_count['all'], $templates_count['available'] ) && $templates_count['all'] !== $templates_count['available'] ) { |
| 124 | $generic_categories['available'] = esc_html__( 'Available Templates', 'wpforms-lite' ); |
| 125 | } |
| 126 | |
| 127 | $generic_categories['favorites'] = esc_html__( 'Favorite Templates', 'wpforms-lite' ); |
| 128 | $generic_categories['new'] = esc_html__( 'New Templates', 'wpforms-lite' ); |
| 129 | |
| 130 | $this->output_categories( $generic_categories, $templates_count ); |
| 131 | |
| 132 | printf( '<li class="divider"></li>' ); |
| 133 | |
| 134 | $common_categories = []; |
| 135 | |
| 136 | if ( $this->is_custom_templates_available ) { |
| 137 | $common_categories['custom'] = esc_html__( 'Custom Templates', 'wpforms-lite' ); |
| 138 | } |
| 139 | |
| 140 | if ( $this->is_addon_templates_available ) { |
| 141 | $common_categories['addons'] = esc_html__( 'Addon Templates', 'wpforms-lite' ); |
| 142 | } |
| 143 | |
| 144 | $categories = array_merge( |
| 145 | $common_categories, |
| 146 | wpforms()->get( 'builder_templates' )->get_categories() |
| 147 | ); |
| 148 | |
| 149 | $this->output_categories( $categories, $templates_count ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Output categories list. |
| 154 | * |
| 155 | * @since 1.7.7 |
| 156 | * |
| 157 | * @param array $categories Categories list. |
| 158 | * @param array $templates_count Templates count by categories. |
| 159 | */ |
| 160 | private function output_categories( $categories, $templates_count ) { |
| 161 | |
| 162 | foreach ( $categories as $slug => $name ) { |
| 163 | |
| 164 | $class = ''; |
| 165 | |
| 166 | if ( $slug === 'all' ) { |
| 167 | $class = ' class="active"'; |
| 168 | } elseif ( empty( $templates_count[ $slug ] ) ) { |
| 169 | $class = ' class="wpforms-hidden"'; |
| 170 | } |
| 171 | |
| 172 | $count = isset( $templates_count[ $slug ] ) ? $templates_count[ $slug ] : '0'; |
| 173 | |
| 174 | printf( |
| 175 | '<li data-category="%1$s"%2$s>%3$s<span>%4$s</span></li>', |
| 176 | esc_attr( $slug ), |
| 177 | $class, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 178 | esc_html( $name ), |
| 179 | esc_html( $count ) |
| 180 | ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Generate a block of templates to choose from. |
| 186 | * |
| 187 | * @since 1.7.7 |
| 188 | * |
| 189 | * @param array $templates Deprecated. |
| 190 | * @param string $slug Deprecated. |
| 191 | */ |
| 192 | public function template_select_options( $templates = [], $slug = '' ) { |
| 193 | |
| 194 | foreach ( $this->prepared_templates as $template ) { |
| 195 | |
| 196 | echo wpforms_render( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 197 | 'builder/templates-item', |
| 198 | $template, |
| 199 | true |
| 200 | ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Prepare arguments for rendering template item. |
| 206 | * |
| 207 | * @since 1.7.7 |
| 208 | * |
| 209 | * @param array $template Template data. |
| 210 | * |
| 211 | * @return array Arguments. |
| 212 | */ |
| 213 | private function prepare_template_render_arguments( $template ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 214 | |
| 215 | $template['plugin_dir'] = isset( $template['plugin_dir'] ) ? $template['plugin_dir'] : ''; |
| 216 | $template['source'] = $this->get_template_source( $template ); |
| 217 | $template['url'] = ! empty( $template['url'] ) ? $template['url'] : ''; |
| 218 | $template['has_access'] = ! empty( $template['license'] ) ? $template['has_access'] : true; |
| 219 | $template['favorite'] = isset( $template['favorite'] ) ? $template['favorite'] : wpforms()->get( 'builder_templates' )->is_favorite( $template['slug'] ); |
| 220 | |
| 221 | $args = []; |
| 222 | |
| 223 | $args['template_id'] = ! empty( $template['id'] ) ? $template['id'] : $template['slug']; |
| 224 | $args['categories'] = $this->get_template_categories( $template ); |
| 225 | $args['demo_url'] = ''; |
| 226 | |
| 227 | if ( ! empty( $template['url'] ) ) { |
| 228 | $medium = wpforms_is_admin_page( 'templates' ) ? 'Form Templates Subpage' : 'builder-templates'; |
| 229 | $args['demo_url'] = wpforms_utm_link( $template['url'], $medium, $template['name'] ); |
| 230 | } |
| 231 | |
| 232 | $template_license = ! empty( $template['license'] ) ? $template['license'] : ''; |
| 233 | $template_name = sprintf( /* translators: %s - Form template name. */ |
| 234 | esc_html__( '%s template', 'wpforms-lite' ), |
| 235 | esc_html( $template['name'] ) |
| 236 | ); |
| 237 | |
| 238 | $args['badge_text'] = ''; |
| 239 | $args['license_class'] = ''; |
| 240 | $args['education_class'] = ''; |
| 241 | $args['education_attributes'] = ''; |
| 242 | |
| 243 | if ( $template['source'] === 'wpforms-addon' ) { |
| 244 | $args['badge_text'] = esc_html__( 'Addon', 'wpforms-lite' ); |
| 245 | |
| 246 | // At least one addon template available. |
| 247 | $this->is_addon_templates_available = true; |
| 248 | } |
| 249 | |
| 250 | if ( $template['source'] === 'wpforms-custom' ) { |
| 251 | $args['badge_text'] = esc_html__( 'Custom', 'wpforms-lite' ); |
| 252 | |
| 253 | // At least one custom template available. |
| 254 | $this->is_custom_templates_available = true; |
| 255 | } |
| 256 | |
| 257 | $args['action_text'] = $this->get_action_button_text( $template ); |
| 258 | |
| 259 | if ( empty( $template['has_access'] ) ) { |
| 260 | $args['license_class'] = ' pro'; |
| 261 | $args['badge_text'] = $template_license; |
| 262 | $args['education_class'] = ' education-modal'; |
| 263 | $args['education_attributes'] = sprintf( |
| 264 | ' data-name="%1$s" data-license="%2$s" data-action="upgrade"', |
| 265 | esc_attr( $template_name ), |
| 266 | esc_attr( $template_license ) |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | $args['addons_attributes'] = $this->prepare_addons_attributes( $template ); |
| 271 | |
| 272 | $args['selected'] = ! empty( $this->form_data['meta']['template'] ) && $this->form_data['meta']['template'] === $args['template_id']; |
| 273 | $args['selected_class'] = $args['selected'] ? ' selected' : ''; |
| 274 | $args['badge_text'] = $args['selected'] ? esc_html__( 'Selected', 'wpforms-lite' ) : $args['badge_text']; |
| 275 | $args['badge_class'] = ! empty( $args['badge_text'] ) ? ' badge' : ''; |
| 276 | $args['template'] = $template; |
| 277 | |
| 278 | return $args; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get action button text. |
| 283 | * |
| 284 | * @since 1.7.7 |
| 285 | * |
| 286 | * @param array $template Template data. |
| 287 | * |
| 288 | * @return string |
| 289 | */ |
| 290 | private function get_action_button_text( $template ) { |
| 291 | |
| 292 | if ( $template['slug'] === 'blank' ) { |
| 293 | return __( 'Create Blank Form', 'wpforms-lite' ); |
| 294 | } |
| 295 | |
| 296 | if ( wpforms_is_admin_page( 'templates' ) ) { |
| 297 | return __( 'Create Form', 'wpforms-lite' ); |
| 298 | } |
| 299 | |
| 300 | return __( 'Use Template', 'wpforms-lite' ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Generate addon attributes. |
| 305 | * |
| 306 | * @since 1.7.7 |
| 307 | * |
| 308 | * @param array $template Template data. |
| 309 | * |
| 310 | * @return string Addon attributes. |
| 311 | */ |
| 312 | private function prepare_addons_attributes( $template ) { |
| 313 | |
| 314 | $addons_attributes = ''; |
| 315 | $required_addons = false; |
| 316 | |
| 317 | if ( ! empty( $template['addons'] ) && is_array( $template['addons'] ) ) { |
| 318 | $required_addons = $this->addons_obj->get_by_slugs( $template['addons'] ); |
| 319 | |
| 320 | foreach ( $required_addons as $i => $addon ) { |
| 321 | if ( |
| 322 | ! isset( $addon['action'], $addon['title'], $addon['slug'] ) || |
| 323 | ! in_array( $addon['action'], [ 'install', 'activate' ], true ) |
| 324 | ) { |
| 325 | unset( $required_addons[ $i ] ); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if ( ! empty( $required_addons ) ) { |
| 331 | $addons_names = implode( ', ', wp_list_pluck( $required_addons, 'title' ) ); |
| 332 | $addons_slugs = implode( ',', wp_list_pluck( $required_addons, 'slug' ) ); |
| 333 | |
| 334 | $addons_attributes = sprintf( |
| 335 | ' data-addons-names="%1$s" data-addons="%2$s"', |
| 336 | esc_attr( $addons_names ), |
| 337 | esc_attr( $addons_slugs ) |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | return $addons_attributes; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Determine a template source. |
| 346 | * |
| 347 | * @since 1.7.7 |
| 348 | * |
| 349 | * @param array $template Template data. |
| 350 | * |
| 351 | * @return string Template source. |
| 352 | */ |
| 353 | private function get_template_source( $template ) { |
| 354 | |
| 355 | if ( ! empty( $template['source'] ) ) { |
| 356 | return $template['source']; |
| 357 | } |
| 358 | |
| 359 | $source = 'wpforms-addon'; |
| 360 | |
| 361 | static $addons = null; |
| 362 | |
| 363 | if ( $addons === null ) { |
| 364 | $addons = array_keys( $this->addons_obj->get_all() ); |
| 365 | } |
| 366 | |
| 367 | if ( $template['plugin_dir'] === 'wpforms' || $template['plugin_dir'] === 'wpforms-lite' ) { |
| 368 | $source = 'wpforms-core'; |
| 369 | } |
| 370 | |
| 371 | if ( $source !== 'wpforms-core' && ! in_array( $template['plugin_dir'], $addons, true ) ) { |
| 372 | $source = 'wpforms-custom'; |
| 373 | } |
| 374 | |
| 375 | return $source; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Determine template categories. |
| 380 | * |
| 381 | * @since 1.7.7 |
| 382 | * |
| 383 | * @param array $template Template data. |
| 384 | * |
| 385 | * @return string Template categories coma separated. |
| 386 | */ |
| 387 | private function get_template_categories( $template ) { |
| 388 | |
| 389 | $categories = ! empty( $template['categories'] ) ? (array) $template['categories'] : []; |
| 390 | $source = $this->get_template_source( $template ); |
| 391 | |
| 392 | if ( $source === 'wpforms-addon' ) { |
| 393 | $categories[] = 'addons'; |
| 394 | } |
| 395 | |
| 396 | if ( $source === 'wpforms-custom' ) { |
| 397 | $categories[] = 'custom'; |
| 398 | } |
| 399 | |
| 400 | if ( isset( $template['created_at'] ) && strtotime( $template['created_at'] ) > strtotime( '-3 Months' ) ) { |
| 401 | $categories[] = 'new'; |
| 402 | } |
| 403 | |
| 404 | return implode( ',', $categories ); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Get categories templates count. |
| 409 | * |
| 410 | * @since 1.7.7 |
| 411 | * |
| 412 | * @return array |
| 413 | */ |
| 414 | private function get_count_in_categories() { |
| 415 | |
| 416 | $all_categories = []; |
| 417 | $available_templates_count = 0; |
| 418 | $favorites_templates_count = 0; |
| 419 | |
| 420 | foreach ( $this->prepared_templates as $template_data ) { |
| 421 | |
| 422 | $template = $template_data['template']; |
| 423 | $categories = explode( ',', $template_data['categories'] ); |
| 424 | |
| 425 | if ( $template['has_access'] ) { |
| 426 | $available_templates_count ++; |
| 427 | } |
| 428 | |
| 429 | if ( $template['favorite'] ) { |
| 430 | $favorites_templates_count++; |
| 431 | } |
| 432 | |
| 433 | if ( is_array( $categories ) ) { |
| 434 | array_push( $all_categories, ...$categories ); |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | $all_categories[] = $categories; |
| 439 | } |
| 440 | |
| 441 | $categories_count = array_count_values( $all_categories ); |
| 442 | $categories_count['all'] = count( $this->prepared_templates ); |
| 443 | $categories_count['available'] = $available_templates_count; |
| 444 | $categories_count['favorites'] = $favorites_templates_count; |
| 445 | |
| 446 | return $categories_count; |
| 447 | } |
| 448 | } |
| 449 |