| 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( \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 |
/** |
| 83 |
* Shortcode callback |
| 84 |
* |
| 85 |
* @param array $attrs |
| 86 |
* @param null $content |
| 87 |
* |
| 88 |
* @return string |
| 89 |
* @throws \Exception |
| 90 |
*/ |
| 91 |
function process_shortcode( $attrs, $content = null ) { |
| 92 |
|
| 93 |
extract( shortcode_atts( |
| 94 |
array_fill_keys( self::SHORTCODE_ATTRS, '' ), |
| 95 |
$attrs, |
| 96 |
'depicter' |
| 97 |
)); |
| 98 |
|
| 99 |
$documentId = $id; |
| 100 |
|
| 101 |
if ( empty( $documentId ) ) { |
| 102 |
if( empty( $slug ) && ! empty( $alias ) ){ |
| 103 |
$slug = $alias; |
| 104 |
} |
| 105 |
$documentId = $slug; |
| 106 |
} |
| 107 |
|
| 108 |
return \Depicter::front()->render()->document( $documentId, [ 'echo' => false ] ); |
| 109 |
} |
| 110 |
} |
| 111 |
|