| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Modules; |
| 4 |
|
| 5 |
use Depicter\Modules\Elementor\SliderWidget; |
| 6 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Register widgets. |
| 10 |
*/ |
| 11 |
class ModulesServiceProvider implements ServiceProviderInterface { |
| 12 |
/** |
| 13 |
* {@inheritDoc} |
| 14 |
*/ |
| 15 |
public function register( $container ) { |
| 16 |
// Nothing to register. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* {@inheritDoc} |
| 21 |
*/ |
| 22 |
public function bootstrap( $container ) { |
| 23 |
|
| 24 |
if ( defined( 'ELEMENTOR_VERSION' ) && version_compare( ELEMENTOR_VERSION, '3.5.0', '>=' ) ) { |
| 25 |
add_action( 'elementor/widgets/register', [$this, 'registerElementorWidgets'] ); |
| 26 |
} else { |
| 27 |
add_action( 'elementor/widgets/widgets_registered', [$this, 'registerElementorWidgets'] ); |
| 28 |
} |
| 29 |
|
| 30 |
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueueEditorScripts'] ); |
| 31 |
add_action( 'wp_enqueue_scripts', [ $this, 'loadElementorWidgetScript'] ); |
| 32 |
|
| 33 |
add_action( 'init', [ $this, 'initGutenbergBlock'] ); |
| 34 |
add_action( 'admin_enqueue_scripts', [ $this, 'loadGutenbergWidgetScripts'] ); |
| 35 |
|
| 36 |
add_action( 'init', [ $this, 'initBeaverModule' ] ); |
| 37 |
add_action( 'wp_enqueue_scripts', [ $this, 'load_beaver_builder_widget_script'] ); |
| 38 |
|
| 39 |
add_action( 'vc_before_init', [ $this, 'initWPBakeryModule' ] ); |
| 40 |
add_action( 'init', [ $this, 'initDiviModule' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register Elementor widgets. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function registerElementorWidgets() { |
| 49 |
if ( defined( 'ELEMENTOR_VERSION' ) && version_compare( ELEMENTOR_VERSION, '3.5.0', '>=' ) ) { |
| 50 |
\Elementor\Plugin::instance()->widgets_manager->register( new SliderWidget() ); |
| 51 |
} else { |
| 52 |
\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new SliderWidget() ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* load required script for elementor widget in elementor editor env |
| 58 |
*/ |
| 59 |
public function loadElementorWidgetScript() { |
| 60 |
if ( class_exists( '\Elementor\Plugin' ) && \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 61 |
\Depicter::front()->assets()->enqueueScripts('widget'); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function load_beaver_builder_widget_script() { |
| 66 |
if ( class_exists( '\FLBuilderModel' ) && \FLBuilderModel::is_builder_active() ) { |
| 67 |
$this->enqueueEditorScripts(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function enqueueEditorScripts() { |
| 72 |
|
| 73 |
\Depicter::core()->assets()->enqueueStyle( |
| 74 |
'depicter-admin', |
| 75 |
\Depicter::core()->assets()->getUrl() . '/resources/styles/admin/admin.css' |
| 76 |
); |
| 77 |
|
| 78 |
\Depicter::core()->assets()->enqueueScript( |
| 79 |
'depicter-admin', |
| 80 |
\Depicter::core()->assets()->getUrl() . '/resources/scripts/admin/index.js', |
| 81 |
['jquery'], |
| 82 |
true |
| 83 |
); |
| 84 |
|
| 85 |
wp_localize_script( 'depicter-admin', 'depicterParams',[ |
| 86 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 87 |
'editorUrl' => \Depicter::editor()->getEditUrl('1'), |
| 88 |
'token' => \Depicter::csrf()->getToken( \Depicter\Security\CSRF::EDITOR_ACTION ), |
| 89 |
'publishedText' => esc_html__( 'Published', 'depicter' ) |
| 90 |
]); |
| 91 |
} |
| 92 |
|
| 93 |
public function loadGutenbergWidgetScripts() { |
| 94 |
|
| 95 |
$current_screen = get_current_screen(); |
| 96 |
if ( !$current_screen->is_block_editor() ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
$list = [ |
| 101 |
[ |
| 102 |
'id' => "0", |
| 103 |
'name' => __( 'Select Slider', 'depicter' ) |
| 104 |
] |
| 105 |
]; |
| 106 |
$list = array_merge( $list, \Depicter::app()->documentRepository()->getList(['name','id']) ); |
| 107 |
if ( !empty( $list ) ) { |
| 108 |
foreach ( $list as $key => $item ) { |
| 109 |
$list[ $key ]['label'] = $item['name']; |
| 110 |
unset( $list[ $key ]['name'] ); |
| 111 |
|
| 112 |
$list[ $key ]['value'] = $item['id']; |
| 113 |
unset( $list[ $key ]['id'] ); |
| 114 |
} |
| 115 |
} |
| 116 |
wp_localize_script( 'wp-block-editor', 'depicterSliders',[ |
| 117 |
'list' => $list |
| 118 |
]); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
public function initGutenbergBlock() { |
| 123 |
register_block_type( __DIR__ . '/Gutenberg/build', [ |
| 124 |
'render_callback' => [ $this, 'renderGutenbergBlock' ] |
| 125 |
] ); |
| 126 |
} |
| 127 |
|
| 128 |
public function renderGutenbergBlock( $blockAttributes ) { |
| 129 |
|
| 130 |
if ( !empty( $blockAttributes['id'] ) ) { |
| 131 |
$id = (int) $blockAttributes['id']; |
| 132 |
return depicter( $id, ['echo' => false ] ); |
| 133 |
} else { |
| 134 |
echo esc_html__( 'Slider ID required', 'depicter' ); |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Load beaver builder module |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function initBeaverModule() { |
| 146 |
if ( class_exists( '\FLBuilder' ) ) { |
| 147 |
require_once 'Beaver/module.php'; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Load WPBakery module |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function initWPBakeryModule() { |
| 157 |
require_once 'WPBakery/module.php'; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Load divi module |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function initDiviModule() { |
| 166 |
if ( class_exists( 'ET_Builder_Plugin' ) ) { |
| 167 |
require_once 'Divi/depicter-divi.php'; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|