| 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 |
|