| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Modules\WPBakery; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Module { |
| 10 |
|
| 11 |
/** |
| 12 |
* Class constructor |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
if ( function_exists( 'vc_map' ) ) { |
| 16 |
$this->map_vc_shortcodes(); |
| 17 |
|
| 18 |
vc_add_shortcode_param( 'depicter_slider_buttons', [ $this, 'slider_buttons_markup' ] ); |
| 19 |
add_action( 'vc_edit_form_fields_after_render', [ $this, 'add_scripts' ] ); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Add depicter shortcode to wpbakery |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function map_vc_shortcodes() { |
| 29 |
|
| 30 |
vc_map([ |
| 31 |
'name' => __( 'Depicter', 'depicter' ), |
| 32 |
'base' => 'depicter', |
| 33 |
'category' => __( 'Content', 'depicter' ), |
| 34 |
'description' => __( 'Make animated and interactive slider', 'depicter' ), |
| 35 |
'icon' => \Depicter::core()->assets()->getUrl() . '/resources/images/svg/logo-without-text.svg', |
| 36 |
'params' => $this->get_fields() |
| 37 |
]); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* get fields |
| 42 |
* |
| 43 |
* @return array |
| 44 |
* @throws \Exception |
| 45 |
*/ |
| 46 |
public function get_fields() { |
| 47 |
|
| 48 |
$list = [ |
| 49 |
__( 'Select Slider', 'depicter' ) => 0 |
| 50 |
]; |
| 51 |
$documents = \Depicter::documentRepository()->select( ['id', 'name', 'status'] )->orderBy('modified_at', 'DESC')->findAll()->get(); |
| 52 |
$documents = $documents ? $documents->toArray() : []; |
| 53 |
foreach( $documents as $document ) { |
| 54 |
$list[ "[#{$document['id']}]: " . $document['name'] ] = $document['id']; |
| 55 |
} |
| 56 |
|
| 57 |
$fields = [ |
| 58 |
[ |
| 59 |
'type' => 'dropdown', |
| 60 |
'class' => '', |
| 61 |
'admin_label' => true, |
| 62 |
'heading' => __( 'Select Slider', 'depicter' ), |
| 63 |
'param_name' => 'id', |
| 64 |
'value' => $list, |
| 65 |
] |
| 66 |
]; |
| 67 |
|
| 68 |
if ( current_user_can( 'access_depicter' ) ) { |
| 69 |
foreach( $documents as $document ) { |
| 70 |
|
| 71 |
$fields[] = [ |
| 72 |
'type' => 'depicter_slider_buttons', |
| 73 |
'class' => '', |
| 74 |
'param_name' => 'slider_control_buttons_' . $document['id'], |
| 75 |
'isPublishedBefore' => \Depicter::documentRepository()->isPublishedBefore( $document['id'] ), |
| 76 |
'documentStatus' => $document['status'], |
| 77 |
'documentID' => $document['id'] |
| 78 |
]; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return $fields; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Add script to the end of editor fields after render |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function add_scripts() { |
| 91 |
if ( empty( $_POST['tag'] ) || $_POST['tag'] != 'depicter' ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
echo '<script type="text/javascript"> |
| 96 |
var $sliderButtons = document.querySelectorAll(\'[data-param_type="depicter_slider_buttons"]\'); |
| 97 |
document.querySelector(\'[data-vc-shortcode="depicter"]\').querySelector("select").addEventListener( "change", function(){ |
| 98 |
if ( this.value != 0 ) { |
| 99 |
$sliderButtons.forEach(function(el, index){ el.style.display = \'none\';}); |
| 100 |
document.querySelector(\'[data-vc-shortcode-param-name="slider_control_buttons_\' +this.value+ \'"]\').style.display = "block"; |
| 101 |
} |
| 102 |
}); |
| 103 |
$sliderButtons.forEach(function(el, index){ el.style.display = \'none\';}); |
| 104 |
var id = document.querySelector(\'[data-vc-shortcode="depicter"]\').querySelector("select").value; |
| 105 |
if ( id != "0" ) { |
| 106 |
document.querySelector(\'[data-vc-shortcode-param-name="slider_control_buttons_\' +id+ \'"]\').style.display = "block"; |
| 107 |
} |
| 108 |
|
| 109 |
document.querySelectorAll(".depicter-edit-slider").forEach(function(el, index){ |
| 110 |
el.addEventListener( "click", function(){ |
| 111 |
var id = document.querySelector(\'[data-vc-shortcode="depicter"]\').querySelector("select").value; |
| 112 |
var editorUrl = depicterParams.editorUrl.replace( \'document=1\', \'document=\' + id ); |
| 113 |
window.open(editorUrl); |
| 114 |
}); |
| 115 |
}); |
| 116 |
|
| 117 |
$sliderButtons.forEach(function(el, index){ |
| 118 |
var $publishButton = el.querySelector(".depicter-publish-slider:not([disabled])"); |
| 119 |
if ( $publishButton ) { |
| 120 |
$publishButton.addEventListener( "click", function(event){ |
| 121 |
event.target.querySelector(".btn-label").style.display = "none"; |
| 122 |
event.target.querySelector(".depicter-state-icon").style.display = "inline-block"; |
| 123 |
|
| 124 |
var id = document.querySelector(\'[data-vc-shortcode="depicter"]\').querySelector("select").value; |
| 125 |
var data = new FormData(); |
| 126 |
data.append(\'ID\', id); |
| 127 |
data.append(\'status\', \'published\'); |
| 128 |
|
| 129 |
fetch( depicterParams.ajaxUrl + "?action=depicter/document/store", { |
| 130 |
method: \'post\', |
| 131 |
body: data, |
| 132 |
headers: { |
| 133 |
\'X-DEPICTER-CSRF\': depicterParams.token |
| 134 |
} |
| 135 |
}) |
| 136 |
.then((response) => response.json()) |
| 137 |
.then((data) => { |
| 138 |
if (data.hits) { |
| 139 |
event.target.disabled = "true"; |
| 140 |
event.target.querySelector(".btn-label").innerHTML = depicterParams.publishedText; |
| 141 |
var $depicterNoticeWrapper = document.querySelector(".depicter-notice-wrapper"); |
| 142 |
if ( $depicterNoticeWrapper ) { |
| 143 |
$depicterNoticeWrapper.style.display = "none"; |
| 144 |
} |
| 145 |
} |
| 146 |
event.target.querySelector(".btn-label").style.display = "inline-block"; |
| 147 |
event.target.querySelector(".depicter-state-icon").style.display = "none"; |
| 148 |
el.querySelector(".depicter-notice-txts").style.display = "none"; |
| 149 |
}).catch((error) => { |
| 150 |
console.error(error); |
| 151 |
}); |
| 152 |
}); |
| 153 |
|
| 154 |
el.querySelector(".btn-label").addEventListener( "click", function(event) { |
| 155 |
event.target.parentNode.click(); |
| 156 |
}); |
| 157 |
} |
| 158 |
}); |
| 159 |
|
| 160 |
</script>'; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get slider buttons control field markup |
| 165 |
* |
| 166 |
* @param array $settings |
| 167 |
* @param string $value |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public function slider_buttons_markup( $settings, $value ) { |
| 171 |
return \Depicter::view('admin/notices/builders-draft-notice')->with('view_args', $settings)->toString(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
new Module(); |
| 176 |
|