| 1 |
<?php |
| 2 |
namespace Depicter\Modules\Bricks; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class Widget extends \Bricks\Element { |
| 9 |
|
| 10 |
// Element properties |
| 11 |
public $category = 'general'; |
| 12 |
|
| 13 |
public $name = 'depicter'; |
| 14 |
|
| 15 |
public $icon = 'ti-layout-slider-alt'; |
| 16 |
|
| 17 |
public $css_selector = '.depicter'; |
| 18 |
|
| 19 |
public $scripts = []; // Script(s) run when element is rendered on frontend or updated in builder |
| 20 |
|
| 21 |
// Return localised element label |
| 22 |
public function get_label() { |
| 23 |
return esc_html__( 'Depicter', 'depicter' ); |
| 24 |
} |
| 25 |
|
| 26 |
// Set builder control groups |
| 27 |
public function set_control_groups() { |
| 28 |
|
| 29 |
$this->control_groups['sliders_list'] = [ |
| 30 |
'title' => esc_html__( 'Sliders List', 'depicter' ), |
| 31 |
'tab' => 'content', |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
// Set builder controls |
| 36 |
public function set_controls() { |
| 37 |
|
| 38 |
$this->controls['slider_id'] = [ |
| 39 |
'tab' => 'content', |
| 40 |
'group' => 'sliders_list', |
| 41 |
'label' => esc_html__( 'Select Slider', 'depicter' ), |
| 42 |
'type' => 'select', |
| 43 |
'options' => $this->getSlidersList(), |
| 44 |
'inline' => true, |
| 45 |
'clearable' => false, |
| 46 |
'pasteStyles' => false, |
| 47 |
'default' => '0', |
| 48 |
'rerender' => true, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
// Enqueue element styles and scripts |
| 53 |
public function enqueue_scripts() { |
| 54 |
|
| 55 |
$styles = \Depicter::front()->assets()->enqueueStyles(); |
| 56 |
foreach ( $styles as $key => $style ) { |
| 57 |
wp_enqueue_style( $key ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( ( defined('REST_REQUEST') && REST_REQUEST ) || ( isset( $_GET['bricks'] ) && $_GET['bricks'] == 'run' ) ) { |
| 61 |
$scripts = \Depicter::front()->assets()->enqueueScripts(['player', 'iframe-resizer']); |
| 62 |
} else { |
| 63 |
$scripts = \Depicter::front()->assets()->enqueueScripts(); |
| 64 |
} |
| 65 |
|
| 66 |
foreach ( $scripts as $key => $script ) { |
| 67 |
wp_enqueue_script( $key ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function getSlidersList() { |
| 72 |
$list = [ |
| 73 |
'0' => __( 'Select Slider', 'depicter' ) |
| 74 |
]; |
| 75 |
$documents = \Depicter::documentRepository()->select( ['id', 'name'] )->orderBy('modified_at', 'DESC')->findAll()->get(); |
| 76 |
$documents = $documents ? $documents->toArray() : []; |
| 77 |
foreach( $documents as $document ) { |
| 78 |
$list[ "#" . $document['id'] ] = "[#{$document['id']}]: " . $document['name']; |
| 79 |
} |
| 80 |
return $list; |
| 81 |
} |
| 82 |
|
| 83 |
// Render element HTML |
| 84 |
public function render() { |
| 85 |
if ( $this->settings['slider_id'] ) { |
| 86 |
$sliderID = ltrim( $this->settings['slider_id'], '#' ); |
| 87 |
if ( ( defined('REST_REQUEST') && REST_REQUEST ) || ( isset( $_GET['bricks'] ) && $_GET['bricks'] == 'run' ) ) { |
| 88 |
$iframeID = "sliderIframe-" . $sliderID; |
| 89 |
$iframeURL = admin_url('admin-ajax.php') . '?action=depicter-document-preview&depicter-csrf=' . \Depicter::csrf()->getToken( \Depicter\Security\CSRF::EDITOR_ACTION ) . '&ID=' . $sliderID . '&status=draft|publish&gutenberg=true'; |
| 90 |
echo '<iframe id="' . esc_attr( $iframeID ) . '" style="width: 1px;min-width: 100%;" src="' . esc_url( $iframeURL ) . '"></iframe>'; |
| 91 |
echo "<script>iFrameResize({}, '#sliderIframe-" . esc_js( $sliderID ) . "')</script>"; |
| 92 |
} else { |
| 93 |
echo \Depicter::front()->render()->document( $sliderID ); |
| 94 |
} |
| 95 |
} else { |
| 96 |
echo esc_html__('Please select a Depicter slider','depicter' ); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|