Changeset 2186230
- Timestamp:
- 11/05/2019 02:40:33 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
simple-multiple-featured-images/trunk/includes/class-smfi-shortcodes.php
r1971107 r2186230 8 8 */ 9 9 if ( ! class_exists( 'SMFI_Shortcodes' ) ) { 10 10 11 11 /** 12 * Adds shortcodes support to the simple multiple featured images plugin. 12 * Adds shortcodes support to the simple multiple featured images plugin. 13 13 * 14 14 * Allows to visualize the featured images via shortcodes. … … 17 17 */ 18 18 class SMFI_Shortcodes { 19 19 20 20 /** 21 21 * The default gallery shortcode. 22 22 * 23 23 * @since 1.0.0 24 * @var string 24 * @var string 25 25 */ 26 26 private $shortcode_insert_default_gallery = 'smfi-insert-default-gallery'; 27 27 28 28 /** 29 29 * The default slider shortcode. 30 30 * 31 31 * @since 1.0.0 32 * @var string 32 * @var string 33 33 */ 34 34 private $shortcode_insert_default_slider = 'smfi-insert-default-slider'; 35 35 36 36 /** 37 37 * The smfi plugin. 38 38 * 39 39 * @since 1.0.0 40 * @var string 40 * @var string 41 41 */ 42 42 private $smfi_plugin; 43 43 44 44 /** 45 45 * Constructor. 46 46 * 47 * @since 1.0.0 47 * @since 1.0.0 48 48 * 49 49 */ … … 51 51 $this -> smfi_plugin = $smfi_plugin; 52 52 } 53 53 54 54 /** 55 55 * Initialize the shortcodes. … … 58 58 */ 59 59 public function init() { 60 60 61 61 // Enqueue all necessary CSS and JS into the admin screen. 62 62 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); 63 63 64 64 // Enqueue all necessary CSS and JS into the front end. 65 65 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); 66 66 67 67 // Register shortcodes. 68 68 add_action('init', array( $this, 'register_shortcodes' ) ); 69 69 70 70 /* 71 71 * Attach new buttons to the TinyMCE editor. … … 74 74 add_action( 'init', array( $this, 'init_shortcode_buttons') ); 75 75 } 76 76 77 77 /** 78 78 * Enqueue admin scripts. … … 80 80 * Enqueue all necessary CSS and javascript files. If the current post type is not supported by this plugin then no files will be enqueued. 81 81 * 82 * @since 1.0.0 82 * @since 1.0.0 83 83 */ 84 84 public function enqueue_admin_scripts() { 85 85 86 86 if( ! is_null( $this -> smfi_plugin ) ) { 87 87 if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) { 88 88 89 89 // Add plugin specific js with translations. 90 90 $translation_object_name = 'smfi_shortcode_translation_object'; … … 93 93 'insert_default_slider_btn_tooltip' => esc_html__( 'Insert SMFI slider', 'simple-multiple-featured-images'), 94 94 ); 95 95 96 96 // Add shortcodes JS. 97 97 SMFI_JS_Importer::import_js( 98 'smfi_shortcodes_js', 99 plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes.js', 98 'smfi_shortcodes_js', 99 plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes.js', 100 100 false /*use minified version*/, 101 $translation_object_name, 101 $translation_object_name, 102 102 $translation_array 103 103 ); … … 105 105 } 106 106 } 107 107 108 108 /** 109 109 * Enqueue frontend scripts. … … 111 111 * Enqueue all necessary CSS and javascript files into the frontend. If the current post type is not supported by this plugin then no files will be enqueued. 112 112 * 113 * @since 1.0.0 113 * @since 1.0.0 114 114 */ 115 115 public function enqueue_frontend_scripts() { 116 116 117 117 if( ! is_null( $this -> smfi_plugin ) ) { 118 118 if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) { 119 119 120 120 // Add shortcode and CSS 121 121 SMFI_CSS_Importer::import_css( 122 'smfi_shortcodes_frontend_css', 123 plugin_dir_url( dirname( __FILE__ ) ) . 'public/css/smfi-shortcodes-frontend-style.css', 124 false /*use minified version*/ 122 'smfi_shortcodes_frontend_css', 123 plugin_dir_url( dirname( __FILE__ ) ) . 'public/css/smfi-shortcodes-frontend-style.css', 124 false /*use minified version*/ 125 125 ); 126 126 127 127 // Add shortcode JS. 128 128 SMFI_JS_Importer::import_js( 129 'smfi_shortcodes_frontend_js', 130 plugin_dir_url( dirname( __FILE__ ) ) . 'public/js/jquery.smfi-shortcodes-frontend.js', 129 'smfi_shortcodes_frontend_js', 130 plugin_dir_url( dirname( __FILE__ ) ) . 'public/js/jquery.smfi-shortcodes-frontend.js', 131 131 false /*use minified version*/ 132 132 ); … … 134 134 } 135 135 } 136 136 137 137 /** 138 138 * Registers the shortcodes in wordpress. … … 147 147 add_shortcode( $this -> shortcode_insert_default_slider, array( $this, 'get_frontend_html_for_shortcode' ) ); 148 148 } 149 149 150 150 /** 151 151 * Attachs new shortcode buttons into the TinyMCE editor. For each shortcode a seperate button will be provided. … … 153 153 * Pressing the button will add the corresponding shortcode automatically into the current selected area inside the editor. 154 154 * 155 * @since 1.0.0 155 * @since 1.0.0 156 156 */ 157 157 function init_shortcode_buttons() { 158 158 // Register the shortcode buttons. 159 159 add_filter( 'mce_buttons', array( $this, 'register_shortcode_buttons') ); 160 160 161 161 // Register the necessary JS plugin for the shortcode buttons. 162 162 add_filter( 'mce_external_plugins', array( $this, 'register_shortcode_buttons_js') ); 163 163 } 164 164 165 165 /** 166 166 * Register the shortcode buttons. 167 167 * 168 * @since 1.0.0 168 * @since 1.0.0 169 169 * 170 170 * @param array $buttonIDs All IDs (as string) of all already registered buttons. … … 172 172 */ 173 173 function register_shortcode_buttons( $buttonIDs ) { 174 $buttonIDs[] = 'smfiDefaultGalleryBtn'; 175 $buttonIDs[] = 'smfiDefaultSliderBtn'; 174 if( ! is_null( $this -> smfi_plugin ) ) { 175 if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) { 176 $buttonIDs[] = 'smfiDefaultGalleryBtn'; 177 $buttonIDs[] = 'smfiDefaultSliderBtn'; 178 } 179 } 176 180 return $buttonIDs; 177 181 } 178 182 179 183 /** 180 184 * Register the necessary JS for the registered shortcode buttons. 181 185 * 182 * @since 1.0.0 186 * @since 1.0.0 183 187 * 184 188 * @param array $paths All absolute paths of all JS scripts of all already registered buttons. … … 186 190 */ 187 191 function register_shortcode_buttons_js( $paths ) { 188 $paths[ 'smfiShortcodesButtonsPlugin' ] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes-buttons-plugin.js'; 192 if( ! is_null( $this -> smfi_plugin ) ) { 193 if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) { 194 $paths[ 'smfiShortcodesButtonsPlugin' ] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/jquery.smfi-shortcodes-buttons-plugin.js'; 195 } 196 } 189 197 return $paths; 190 198 } … … 193 201 * Returns the HTML for the provided shortcode. 194 202 * 195 * @since 1.0.0 203 * @since 1.0.0 196 204 * 197 205 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 201 209 */ 202 210 public function get_frontend_html_for_shortcode( $shortcode_attributes = [], $content = null, $shortcode_name = '' ) { 203 211 204 212 $output = ''; 205 213 206 214 // Search the SMFI plugin. 207 215 if( ! is_null( $this -> smfi_plugin ) ) { 208 216 209 217 // Check if the current post uses the featured images of the SMFI plugin. 210 218 if( $this -> smfi_plugin -> get_public_api() -> is_smfi_showed() ) { 211 219 212 220 // Sanitizes the raw shortcode attributes for further using. 213 221 $sanitize_shortcode_attributes = $this -> sanitize_shortcode_attributes( $shortcode_attributes, $shortcode_name ); 214 222 215 223 // Get the appropriate image size for the current shortcode. 216 224 $image_size = $this -> get_image_size_by_shortcode( $sanitize_shortcode_attributes, $shortcode_name ); 217 225 218 226 // Create and collect custom image tag attributes. 219 227 $imgAttributes = array(); 220 228 $imgAttributes[ 'class' ] = $this -> get_image_tag_css_classes( $shortcode_name ); 221 229 222 230 // Get the HTML for each featured image individually. 223 231 $smfi_api = $this -> smfi_plugin -> get_public_api(); 224 232 $img_tags = $smfi_api -> get_all_featured_images_tags( get_the_id(), $image_size, $imgAttributes ); 225 233 226 234 // Generate the frontend HTML by using the generated image tags. 227 235 $output .= '<div class="' . $this -> get_image_container_css_class( $shortcode_name ) . '"'; … … 234 242 } 235 243 $output .= '>'; 236 244 237 245 // Adjust the image tags as necessary for the current shortcode. 238 246 foreach( $img_tags as $index => $img_tag ) { 239 247 $output .= $this -> adjust_image_tag_by_shortcode( $img_tag, $shortcode_name ); 240 248 } 241 249 242 250 // Check if the current shortcode has some HTML which should be inserted after the image tags. 243 251 $number_of_image_tags = count( $img_tags ); … … 246 254 } 247 255 } 248 256 249 257 return $output; 250 258 } 251 259 252 260 /** 253 261 * Sanitizes the raw shortcode attributes for further using. … … 255 263 * Removes unused attributes and creates and initialize missing attributes with default values. 256 264 * 257 * @since 1.0.0 265 * @since 1.0.0 258 266 * 259 267 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 264 272 // Change all received shortcode attributes to lowercase. 265 273 $shortcode_attributes = array_change_key_case( ( array ) $shortcode_attributes , CASE_LOWER ); 266 274 267 275 // Set default attribute values and override them with attribute values defined by the user. 268 276 return shortcode_atts( [ … … 275 283 ], $shortcode_attributes, $shortcode_name ); 276 284 } 277 285 278 286 /** 279 287 * Returns the user defined image size via shortcode. 280 288 * 281 289 * If no image size can be found then a default image size will be returned. 282 * 283 * @since 1.0.0 290 * 291 * @since 1.0.0 284 292 * 285 293 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 288 296 */ 289 297 private function get_image_size_by_shortcode( $shortcode_attributes, $shortcode_name ) { 290 298 291 299 // Get all available intermediate image sizes. 292 300 $all_available_image_sizes = get_intermediate_image_sizes(); 293 301 294 302 // Attach additionally the full image size. 295 303 $all_available_image_sizes[] = 'full'; 296 304 297 305 if( in_array( $shortcode_attributes[ 'image-size' ], $all_available_image_sizes ) ) { 298 306 // User defined a valid image size via shortcode -> return this image size. … … 303 311 } 304 312 } 305 313 306 314 /** 307 315 * Returns the default image size for the given shortcode. 308 316 * 309 * @since 1.0.0 317 * @since 1.0.0 310 318 * 311 319 * @param string $shortcode_name The shortcode name. … … 324 332 } 325 333 } 326 334 327 335 /** 328 336 * Returns the value for the class attribute of the image tags of the given shortcode. 329 337 * 330 * @since 1.0.0 338 * @since 1.0.0 331 339 * 332 340 * @param string $shortcode_name The shortcode name. … … 343 351 } 344 352 } 345 353 346 354 /** 347 355 * Returns the class of the container tag which will contains all image tags. 348 356 * 349 * @since 1.0.0 357 * @since 1.0.0 350 358 * 351 359 * @param string $shortcode_name The shortcode name. … … 362 370 } 363 371 } 364 365 372 373 366 374 /** 367 375 * Checks if the given shortcode should create a slideshow. 368 * 369 * @since 1.0.0 376 * 377 * @since 1.0.0 370 378 * 371 379 * @param string $shortcode_name The shortcode name. … … 378 386 return false; 379 387 } 380 388 381 389 /** 382 390 * Returns the slideshow speed as a HTML5 data attribute. 383 * 384 * @since 1.0.0 391 * 392 * @since 1.0.0 385 393 * 386 394 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 396 404 return ''; 397 405 } 398 406 399 407 /** 400 408 * Returns the slideshow dot color as a HTML5 data attribute. 401 * 402 * @since 1.0.0 409 * 410 * @since 1.0.0 403 411 * 404 412 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 414 422 return ''; 415 423 } 416 424 417 425 /** 418 426 * Returns the slideshow active dot color as a HTML5 data attribute. 419 * 420 * @since 1.0.0 427 * 428 * @since 1.0.0 421 429 * 422 430 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 432 440 return ''; 433 441 } 434 442 435 443 /** 436 444 * Returns the slideshow arrow color as a HTML5 data attribute. 437 * 438 * @since 1.0.0 445 * 446 * @since 1.0.0 439 447 * 440 448 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 450 458 return ''; 451 459 } 452 460 453 461 /** 454 462 * Returns the slideshow active arrow color as a HTML5 data attribute. 455 * 456 * @since 1.0.0 463 * 464 * @since 1.0.0 457 465 * 458 466 * @param array $shortcode_attributes The shortcode attributes entered by the user. … … 468 476 return ''; 469 477 } 470 478 471 479 /** 472 480 * Returns the user defined time after which the slider automatically changes images. 473 * 474 * @since 1.0.0 475 * 476 * @param array $shortcode_attributes The shortcode attributes entered by the user. 477 * @param string $shortcode_name The shortcode name. 478 * @return int Duration in millisonds. Returns -1 if the given shortcode is invalid, 481 * 482 * @since 1.0.0 483 * 484 * @param array $shortcode_attributes The shortcode attributes entered by the user. 485 * @param string $shortcode_name The shortcode name. 486 * @return int Duration in millisonds. Returns -1 if the given shortcode is invalid, 479 487 * shortcode does not support slideshow speed or 480 488 * user entered an invalid shortcode attribute value (less than 1). … … 483 491 // Search time entered by user and convert it into int. 484 492 $time = intval( $shortcode_attributes[ 'slideshow-speed' ] ); 485 493 486 494 if( $time > 0 ) { 487 495 // User defined a valid time via shortcode -> return this time. … … 492 500 } 493 501 } 494 502 495 503 /** 496 504 * Returns the default time after which the slider automatically changes images. 497 505 * 498 * @since 1.0.0 506 * @since 1.0.0 499 507 * 500 508 * @param string $shortcode_name The shortcode name. … … 508 516 return -1; 509 517 } 510 518 511 519 /** 512 520 * Returns the user defined color for the dots of a slideshow. 513 * 514 * @since 1.0.0 515 * 516 * @param array $shortcode_attributes The shortcode attributes entered by the user. 517 * @param string $shortcode_name The shortcode name. 518 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 521 * 522 * @since 1.0.0 523 * 524 * @param array $shortcode_attributes The shortcode attributes entered by the user. 525 * @param string $shortcode_name The shortcode name. 526 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 519 527 * shortcode does not support dots or 520 528 * user entered an invalid color as shortcode attribute value. 521 529 */ 522 530 private function get_dot_color( $shortcode_attributes, $shortcode_name ) { 523 531 524 532 // Search color entered by user. 525 533 $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-dot-color' ] ); 526 534 527 535 if( isset( $color ) || ! empty( $color ) ) { 528 536 // User defined a valid color via shortcode -> return this color. … … 533 541 } 534 542 } 535 543 536 544 /** 537 545 * Returns the default color for the dots of a slideshow. 538 546 * 539 * @since 1.0.0 547 * @since 1.0.0 540 548 * 541 549 * @param string $shortcode_name The shortcode name. … … 549 557 return ''; 550 558 } 551 559 552 560 /** 553 561 * Returns the user defined color for active dots of a slideshow. 554 * 555 * @since 1.0.0 556 * 557 * @param array $shortcode_attributes The shortcode attributes entered by the user. 558 * @param string $shortcode_name The shortcode name. 559 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 562 * 563 * @since 1.0.0 564 * 565 * @param array $shortcode_attributes The shortcode attributes entered by the user. 566 * @param string $shortcode_name The shortcode name. 567 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 560 568 * shortcode does not support dots or 561 569 * user entered an invalid color as shortcode attribute value. 562 570 */ 563 571 private function get_active_dot_color( $shortcode_attributes, $shortcode_name ) { 564 572 565 573 // Search color entered by user. 566 574 $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-active-dot-color' ] ); 567 575 568 576 if( isset( $color ) || ! empty( $color ) ) { 569 577 // User defined a valid color via shortcode -> return this color. … … 574 582 } 575 583 } 576 584 577 585 /** 578 586 * Returns the default color for active dots of a slideshow. 579 587 * 580 * @since 1.0.0 588 * @since 1.0.0 581 589 * 582 590 * @param string $shortcode_name The shortcode name. … … 590 598 return ''; 591 599 } 592 600 593 601 /** 594 602 * Returns the user defined color for the arrows of a slideshow. 595 * 596 * @since 1.0.0 597 * 598 * @param array $shortcode_attributes The shortcode attributes entered by the user. 599 * @param string $shortcode_name The shortcode name. 600 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 603 * 604 * @since 1.0.0 605 * 606 * @param array $shortcode_attributes The shortcode attributes entered by the user. 607 * @param string $shortcode_name The shortcode name. 608 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 601 609 * shortcode does not support arrows or 602 610 * user entered an invalid color as shortcode attribute value. 603 611 */ 604 612 private function get_arrow_color( $shortcode_attributes, $shortcode_name ) { 605 613 606 614 // Search color entered by user. 607 615 $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-arrow-color' ] ); 608 616 609 617 if( isset( $color ) || ! empty( $color ) ) { 610 618 // User defined a valid color via shortcode -> return this color. … … 615 623 } 616 624 } 617 625 618 626 /** 619 627 * Returns the default color for the arrows of a slideshow. 620 628 * 621 * @since 1.0.0 629 * @since 1.0.0 622 630 * 623 631 * @param string $shortcode_name The shortcode name. … … 631 639 return ''; 632 640 } 633 641 634 642 /** 635 643 * Returns the user defined color for active arrows of a slideshow. 636 * 637 * @since 1.0.0 638 * 639 * @param array $shortcode_attributes The shortcode attributes entered by the user. 640 * @param string $shortcode_name The shortcode name. 641 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 644 * 645 * @since 1.0.0 646 * 647 * @param array $shortcode_attributes The shortcode attributes entered by the user. 648 * @param string $shortcode_name The shortcode name. 649 * @return string Color as HEX. Returns empty string if the given shortcode is invalid, 642 650 * shortcode does not support arrows or 643 651 * user entered an invalid color as shortcode attribute value. 644 652 */ 645 653 private function get_active_arrow_color( $shortcode_attributes, $shortcode_name ) { 646 654 647 655 // Search color entered by user. 648 656 $color = sanitize_hex_color( $shortcode_attributes[ 'slideshow-active-arrow-color' ] ); 649 657 650 658 if( isset( $color ) || ! empty( $color ) ) { 651 659 // User defined a valid color via shortcode -> return this color. … … 656 664 } 657 665 } 658 666 659 667 /** 660 668 * Returns the default color for active arrows of a slideshow. 661 669 * 662 * @since 1.0.0 670 * @since 1.0.0 663 671 * 664 672 * @param string $shortcode_name The shortcode name. … … 672 680 return ''; 673 681 } 674 682 675 683 /** 676 684 * Adjusts the given image tag for the provided shortcode. 677 685 * 678 * @since 1.0.0 686 * @since 1.0.0 679 687 * 680 688 * @param array $img_tag The image tag. … … 683 691 */ 684 692 private function adjust_image_tag_by_shortcode( $img_tag, $shortcode_name ) { 685 693 686 694 if( $shortcode_name === $this -> shortcode_insert_default_gallery ) { 687 695 // Currently the default gallery do not need any adjustments -> just return the image tag. … … 702 710 } 703 711 } 704 712 705 713 /** 706 714 * Returns the HTML which should be appended after all image tags. 707 715 * 708 * @since 1.0.0 716 * @since 1.0.0 709 717 * 710 718 * @param string $number_of_image_tags The number of all image tags. … … 726 734 $output .= '<button class="smfi-default-slider-next">❯</button>'; 727 735 $output .= '<div class="smfi-default-slider-dot-container">'; 728 for ( $i = 0 ; $i < $number_of_image_tags; $i++ ){ 729 $output .= '<div class="smfi-default-slider-dot" tabindex=0></div>'; 736 for ( $i = 0 ; $i < $number_of_image_tags; $i++ ){ 737 $output .= '<div class="smfi-default-slider-dot" tabindex=0></div>'; 730 738 } 731 739 $output .= '</div>';
Note: See TracChangeset
for help on using the changeset viewer.