PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / WordPress / ShortcodesServiceProvider.php

ShortcodesServiceProvider.php in Depicter — Popup & Slider Builder 1.3.3, at app/src/WordPress/ShortcodesServiceProvider.php

103 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\WordPress;
3
4 use Averta\WordPress\Utility\Extract;
5 use WPEmerge\ServiceProviders\ServiceProviderInterface;
6
7 /**
8 * Register shortcodes.
9 */
10 class ShortcodesServiceProvider implements ServiceProviderInterface {
11
12 const SHORTCODE_NAME = 'depicter';
13 const SHORTCODE_ATTRS = [ 'id', 'alias', 'slug' ];
14
15 /**
16 * {@inheritDoc}
17 */
18 public function register( $container ) {
19 // Nothing to register.
20 }
21
22 /**
23 * {@inheritDoc}
24 */
25 public function bootstrap( $container ) {
26 add_shortcode(self::SHORTCODE_NAME , [ $this, 'process_shortcode' ]);
27 add_action( 'wp_enqueue_scripts', [ $this, 'loadShortcodeAssets'] );
28 }
29
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( $post->post_content ) ){
39 return;
40 }
41 $content = $post->post_content;
42
43 $shortcodeInfo = Extract::shortcodeAttributes( $content, self::SHORTCODE_NAME, self::SHORTCODE_ATTRS );
44
45 // the shortcode exist in the content
46 if( $shortcodeInfo !== false ){
47 // load common assets
48 \Depicter::front()->assets()->enqueueStyles();
49 \Depicter::front()->assets()->enqueueScripts();
50
51 // Load custom css files
52 foreach( self::SHORTCODE_ATTRS as $documentAttribute ){
53 if( ! empty( $shortcodeInfo[ $documentAttribute ][0] ) ){
54 \Depicter::front()->assets()->enqueueCustomAssets( $shortcodeInfo[ $documentAttribute ][0] );
55 }
56 }
57 }
58
59 if ( false != strpos( $post->post_content, 'wp:depicter/slider' ) ) {
60 // load common assets
61 \Depicter::front()->assets()->enqueueStyles();
62 \Depicter::front()->assets()->enqueueScripts();
63
64 preg_match_all( '/wp:depicter\/slider\s+{"id":(\d+)/', $post->post_content, $sliderIDs, PREG_SET_ORDER );
65 if ( !empty( $sliderIDs ) ) {
66 foreach( $sliderIDs as $sliderID ) {
67 \Depicter::front()->assets()->enqueueCustomAssets( $sliderID[1] );
68 }
69 }
70 }
71 }
72
73
74 /**
75 * Shortcode callback
76 *
77 * @param array $attrs
78 * @param null $content
79 *
80 * @return string
81 * @throws \Exception
82 */
83 function process_shortcode( $attrs, $content = null ) {
84
85 extract( shortcode_atts(
86 array_fill_keys( self::SHORTCODE_ATTRS, '' ),
87 $attrs,
88 'depicter'
89 ));
90
91 $documentId = $id;
92
93 if ( empty( $documentId ) ) {
94 if( empty( $slug ) && ! empty( $alias ) ){
95 $slug = $alias;
96 }
97 $documentId = $slug;
98 }
99
100 return \Depicter::front()->render()->document( $documentId, [ 'echo' => false ] );
101 }
102 }
103