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