| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
use Averta\Core\Utility\JSON; |
| 5 |
use Averta\WordPress\Utility\Extract; |
| 6 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Register shortcodes. |
| 10 |
*/ |
| 11 |
class ShortcodesServiceProvider implements ServiceProviderInterface { |
| 12 |
|
| 13 |
const SHORTCODE_NAME = 'depicter'; |
| 14 |
const SHORTCODE_ATTRS = [ 'id', 'alias', 'slug' ]; |
| 15 |
|
| 16 |
/** |
| 17 |
* {@inheritDoc} |
| 18 |
*/ |
| 19 |
public function register( $container ) { |
| 20 |
// Nothing to register. |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* {@inheritDoc} |
| 25 |
*/ |
| 26 |
public function bootstrap( $container ) { |
| 27 |
add_shortcode(self::SHORTCODE_NAME , [ $this, 'process_shortcode' ]); |
| 28 |
add_action( 'wp_enqueue_scripts', [ $this, 'loadShortcodeAssets'] ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Load assets if the shortcode exists in the page content |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function loadShortcodeAssets() { |
| 37 |
global $post; |
| 38 |
|
| 39 |
if( empty( $post->ID ) ){ |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if ( !empty( \Depicter::options()->get( 'always_load_assets', false ) ) ) { |
| 44 |
\Depicter::front()->assets()->enqueueStyles(); |
| 45 |
\Depicter::front()->assets()->enqueueScripts(); |
| 46 |
} |
| 47 |
|
| 48 |
$content = ''; |
| 49 |
$builtWithElementor = false; |
| 50 |
// check if page built by elementor and user used shortcode widget instead of depicter widget |
| 51 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 52 |
|
| 53 |
$document = \Elementor\Plugin::$instance->documents->get($post->ID); |
| 54 |
if ( !empty( $document ) && $document->is_built_with_elementor() ) { |
| 55 |
$builtWithElementor = true; |
| 56 |
$elementorData = get_post_meta( $post->ID, '_elementor_data', true ); |
| 57 |
$elementorData = JSON::isJson( $elementorData ) ? $elementorData : JSON::encode( $elementorData ); |
| 58 |
preg_match_all( '/\[depicter.*?(?=\])\]/', $elementorData, $matches, PREG_SET_ORDER ); |
| 59 |
if ( empty( $matches ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
foreach ( $matches as $key => $shortcode ) { |
| 63 |
$content .= stripslashes( $shortcode[0] ); |
| 64 |
} |
| 65 |
} else { |
| 66 |
if( empty( $post->post_content ) ){ |
| 67 |
return; |
| 68 |
} |
| 69 |
$content = $post->post_content; |
| 70 |
} |
| 71 |
} else { |
| 72 |
if( empty( $post->post_content ) ){ |
| 73 |
return; |
| 74 |
} |
| 75 |
$content = $post->post_content; |
| 76 |
} |
| 77 |
|
| 78 |
$shortcodeInfo = Extract::shortcodeAttributes( $content, self::SHORTCODE_NAME, self::SHORTCODE_ATTRS ); |
| 79 |
|
| 80 |
// the shortcode exist in the content |
| 81 |
if( $shortcodeInfo !== false ){ |
| 82 |
// load common assets |
| 83 |
\Depicter::front()->assets()->enqueueStyles(); |
| 84 |
\Depicter::front()->assets()->enqueueScripts(); |
| 85 |
|
| 86 |
// Load custom css files |
| 87 |
foreach( self::SHORTCODE_ATTRS as $documentAttribute ){ |
| 88 |
if( ! empty( $shortcodeInfo[ $documentAttribute ][0] ) ){ |
| 89 |
$documentId = ! is_numeric( $shortcodeInfo[ $documentAttribute ][0] ) ? \Depicter::document()->getID( $shortcodeInfo[ $documentAttribute ][0] ) : $shortcodeInfo[ $documentAttribute ][0]; |
| 90 |
if ( ! $documentId ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
\Depicter::document()->cacheCustomStyles( $documentId ); |
| 95 |
\Depicter::front()->assets()->enqueueCustomAssets( $documentId ); |
| 96 |
\Depicter::front()->assets()->enqueuePreloadTags( $documentId ); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if ( false != strpos( $post->post_content, 'wp:depicter/slider' ) ) { |
| 102 |
// load common assets |
| 103 |
\Depicter::front()->assets()->enqueueStyles(); |
| 104 |
\Depicter::front()->assets()->enqueueScripts(); |
| 105 |
|
| 106 |
preg_match_all( '/wp:depicter\/slider\s+{"id":(\d+)/', $post->post_content, $sliderIDs, PREG_SET_ORDER ); |
| 107 |
if ( !empty( $sliderIDs ) ) { |
| 108 |
foreach( $sliderIDs as $sliderID ) { |
| 109 |
\Depicter::document()->cacheCustomStyles( $sliderID[1] ); |
| 110 |
\Depicter::front()->assets()->enqueueCustomAssets( $sliderID[1] ); |
| 111 |
\Depicter::front()->assets()->enqueuePreloadTags( $sliderID[1] ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Shortcode callback |
| 120 |
* |
| 121 |
* @param array $attrs |
| 122 |
* @param null $content |
| 123 |
* |
| 124 |
* @return string |
| 125 |
* @throws \Exception |
| 126 |
*/ |
| 127 |
function process_shortcode( $attrs, $content = null ) { |
| 128 |
|
| 129 |
extract( shortcode_atts( |
| 130 |
array_fill_keys( self::SHORTCODE_ATTRS, '' ), |
| 131 |
$attrs, |
| 132 |
'depicter' |
| 133 |
)); |
| 134 |
|
| 135 |
$documentId = $id; |
| 136 |
|
| 137 |
if ( empty( $documentId ) ) { |
| 138 |
if( empty( $slug ) && ! empty( $alias ) ){ |
| 139 |
$slug = $alias; |
| 140 |
} |
| 141 |
$documentId = $slug; |
| 142 |
} |
| 143 |
|
| 144 |
return \Depicter::front()->render()->document( $documentId, [ 'echo' => false ] ); |
| 145 |
} |
| 146 |
} |
| 147 |
|