| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\WordPress; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Averta\WordPress\Utility\JSON; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
class DocumentDetectorService |
| 10 |
{ |
| 11 |
|
| 12 |
protected array $documentIDs = []; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->init(); |
| 16 |
} |
| 17 |
|
| 18 |
public function init() { |
| 19 |
|
| 20 |
$conditionalDocumentIDs = \Depicter::document()->getConditionalDocumentIDs(); |
| 21 |
if ( ! empty( $conditionalDocumentIDs ) ) { |
| 22 |
$this->documentIDs = Arr::merge( $this->documentIDs, $conditionalDocumentIDs ); |
| 23 |
} |
| 24 |
|
| 25 |
$this->checkForElementorWidget(); |
| 26 |
$this->checkForGutenbergWidget(); |
| 27 |
$this->checkForDiviWidget(); |
| 28 |
$this->checkForBeaverWidget(); |
| 29 |
|
| 30 |
$this->documentIDs = array_unique( $this->documentIDs ); |
| 31 |
} |
| 32 |
|
| 33 |
public function hasAnyDocument() { |
| 34 |
return !empty( $this->documentIDs ); |
| 35 |
} |
| 36 |
|
| 37 |
public function getDocumentsList() { |
| 38 |
return $this->documentIDs; |
| 39 |
} |
| 40 |
|
| 41 |
public function checkForElementorWidget(){ |
| 42 |
if ( ! class_exists('\Elementor\Plugin') ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if (!is_page() && !is_singular()) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$document = Plugin::$instance->documents->get( get_the_ID() ); |
| 51 |
if ( ! $document || ! $document->is_built_with_elementor() ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$elementor_data = get_post_meta( get_the_ID(), '_elementor_data', true ); |
| 56 |
$elementor_data = JSON::isJson( $elementor_data ) ? $elementor_data : JSON::encode( $elementor_data ); |
| 57 |
preg_match_all( '/"slider_id":"#(\d+)"/', $elementor_data, $sliderIDs, PREG_SET_ORDER ); |
| 58 |
if ( ! empty( $sliderIDs ) ) { |
| 59 |
foreach( $sliderIDs as $sliderID ) { |
| 60 |
if ( !empty( $sliderID[1] ) ) { |
| 61 |
$this->documentIDs[] = $sliderID[1]; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
preg_match_all( '/\[depciter id="(\d+)"/', $elementor_data, $sliders, PREG_SET_ORDER ); |
| 67 |
if ( ! empty( $sliders ) ) { |
| 68 |
foreach ( $sliders as $slider ) { |
| 69 |
if ( ! empty( $slider[1] ) ) { |
| 70 |
$this->documentIDs[] = $slider[1]; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function checkForGutenbergWidget(){ |
| 77 |
if (!is_page() && !is_singular()) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$post_content = get_post( get_the_ID() )->post_content; |
| 82 |
if (strpos($post_content, 'wp:depicter/slider') === false && strpos($post_content, '[depicter id="') === false) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
preg_match_all( '/wp:depicter\/slider \{"id":(\d+)\}/', $post_content, $sliders, PREG_SET_ORDER ); |
| 87 |
if ( ! empty( $sliders ) ) { |
| 88 |
foreach ( $sliders as $slider ) { |
| 89 |
if ( ! empty( $slider[1] ) ) { |
| 90 |
$this->documentIDs[] = $slider[1]; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
preg_match_all( '/\[depciter id="(\d+)"/', $post_content, $sliders, PREG_SET_ORDER ); |
| 96 |
if ( ! empty( $sliders ) ) { |
| 97 |
foreach ( $sliders as $slider ) { |
| 98 |
if ( ! empty( $slider[1] ) ) { |
| 99 |
$this->documentIDs[] = $slider[1]; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function checkForBeaverWidget() { |
| 106 |
if (!is_page() && !is_singular()) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$flbuilder_data = get_post_meta( get_the_ID(), '_fl_builder_data', true); |
| 111 |
$flbuilder_data = is_array( $flbuilder_data ) ? maybe_serialize( $flbuilder_data ) : $flbuilder_data; |
| 112 |
preg_match_all( '/document_id";s:\d+:"(\d+)"/', $flbuilder_data, $sliderIDs, PREG_SET_ORDER ); |
| 113 |
foreach( $sliderIDs as $key => $sliderID ) { |
| 114 |
if ( !empty( $sliderID[1] ) ) { |
| 115 |
$this->documentIDs[] = $sliderID[1]; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public function checkForDiviWidget(){ |
| 121 |
|
| 122 |
if ( ! function_exists( 'et_core_is_builder_used_on_current_request' ) ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if (!is_page() && !is_singular() && ! et_core_is_builder_used_on_current_request()) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$post_content = get_post( get_the_ID() )->post_content; |
| 131 |
preg_match_all( '/document_id="(\d+)"/', $post_content, $sliderIDs, PREG_SET_ORDER ); |
| 132 |
foreach( $sliderIDs as $key => $sliderID ) { |
| 133 |
if ( !empty( $sliderID[1] ) ) { |
| 134 |
$this->documentIDs[] = $sliderID[1]; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|