| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Depicter\WordPress; |
| 3 | 3 | |
| 4 | +use Averta\WordPress\Utility\Extract; | |
| 4 | 5 | use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 5 | 6 | |
| 6 | 7 | /** |
| 7 | 8 | * Register shortcodes. |
| @@ -6,8 +7,12 @@ | ||
| 6 | 7 | /** |
| 7 | 8 | * Register shortcodes. |
| 8 | 9 | */ |
| 9 | 10 | class ShortcodesServiceProvider implements ServiceProviderInterface { |
| 11 | + | |
| 12 | + const SHORTCODE_NAME = 'depicter'; | |
| 13 | + const SHORTCODE_ATTRS = [ 'id', 'alias', 'slug' ]; | |
| 14 | + | |
| 10 | 15 | /** |
| 11 | 16 | * {@inheritDoc} |
| 12 | 17 | */ |
| 13 | 18 | public function register( $container ) { |
| @@ -17,12 +22,65 @@ | ||
| 17 | 22 | /** |
| 18 | 23 | * {@inheritDoc} |
| 19 | 24 | */ |
| 20 | 25 | public function bootstrap( $container ) { |
| 21 | - add_shortcode('depicter' , [ $this, 'process_shortcode' ]); | |
| 26 | + add_shortcode(self::SHORTCODE_NAME , [ $this, 'process_shortcode' ]); | |
| 27 | + add_action( 'wp_enqueue_scripts', [ $this, 'loadShortcodeAssets'] ); | |
| 22 | 28 | } |
| 23 | 29 | |
| 24 | 30 | /** |
| 31 | + * Load assets if the shortcode exists in the page content | |
| 32 | + * | |
| 33 | + * @return void | |
| 34 | + */ | |
| 35 | + public function loadShortcodeAssets() { | |
| 36 | + global $post; | |
| 37 | + | |
| 38 | + if ( !empty( \Depicter::options()->get( 'always_load_assets', 1 ) ) ) { | |
| 39 | + \Depicter::front()->assets()->enqueueStyles(); | |
| 40 | + \Depicter::front()->assets()->enqueueScripts(); | |
| 41 | + } | |
| 42 | + | |
| 43 | + if( empty( $post->post_content ) ){ | |
| 44 | + return; | |
| 45 | + } | |
| 46 | + | |
| 47 | + $content = $post->post_content; | |
| 48 | + | |
| 49 | + $shortcodeInfo = Extract::shortcodeAttributes( $content, self::SHORTCODE_NAME, self::SHORTCODE_ATTRS ); | |
| 50 | + | |
| 51 | + // the shortcode exist in the content | |
| 52 | + if( $shortcodeInfo !== false ){ | |
| 53 | + // load common assets | |
| 54 | + \Depicter::front()->assets()->enqueueStyles(); | |
| 55 | + \Depicter::front()->assets()->enqueueScripts(); | |
| 56 | + | |
| 57 | + // Load custom css files | |
| 58 | + foreach( self::SHORTCODE_ATTRS as $documentAttribute ){ | |
| 59 | + if( ! empty( $shortcodeInfo[ $documentAttribute ][0] ) ){ | |
| 60 | + \Depicter::front()->assets()->enqueueCustomAssets( $shortcodeInfo[ $documentAttribute ][0] ); | |
| 61 | + \Depicter::front()->assets()->enqueuePreloadTags( $shortcodeInfo[ $documentAttribute ][0] ); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + } | |
| 65 | + | |
| 66 | + if ( false != strpos( $post->post_content, 'wp:depicter/slider' ) ) { | |
| 67 | + // load common assets | |
| 68 | + \Depicter::front()->assets()->enqueueStyles(); | |
| 69 | + \Depicter::front()->assets()->enqueueScripts(); | |
| 70 | + | |
| 71 | + preg_match_all( '/wp:depicter\/slider\s+{"id":(\d+)/', $post->post_content, $sliderIDs, PREG_SET_ORDER ); | |
| 72 | + if ( !empty( $sliderIDs ) ) { | |
| 73 | + foreach( $sliderIDs as $sliderID ) { | |
| 74 | + \Depicter::front()->assets()->enqueueCustomAssets( $sliderID[1] ); | |
| 75 | + \Depicter::front()->assets()->enqueuePreloadTags( $sliderID[1] ); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + } | |
| 79 | + } | |
| 80 | + | |
| 81 | + | |
| 82 | + /** | |
| 25 | 83 | * Shortcode callback |
| 26 | 84 | * |
| 27 | 85 | * @param array $attrs |
| 28 | 86 | * @param null $content |
| @@ -31,13 +89,10 @@ | ||
| 31 | 89 | * @throws \Exception |
| 32 | 90 | */ |
| 33 | 91 | function process_shortcode( $attrs, $content = null ) { |
| 34 | 92 | |
| 35 | - extract( shortcode_atts([ | |
| 36 | - 'id' => '', | |
| 37 | - 'alias' => '', | |
| 38 | - 'slug' => '' | |
| 39 | - ], | |
| 93 | + extract( shortcode_atts( | |
| 94 | + array_fill_keys( self::SHORTCODE_ATTRS, '' ), | |
| 40 | 95 | $attrs, |
| 41 | 96 | 'depicter' |
| 42 | 97 | )); |
| 43 | 98 | |