| @@ -13,8 +13,11 @@ | ||
| 13 | 13 | */ |
| 14 | 14 | public function __construct() { |
| 15 | 15 | if ( function_exists( 'vc_map' ) ) { |
| 16 | 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' ] ); | |
| 17 | 20 | } |
| 18 | 21 | } |
| 19 | 22 | |
| 20 | 23 | /** |
| @@ -29,40 +32,144 @@ | ||
| 29 | 32 | 'base' => 'depicter', |
| 30 | 33 | 'category' => __( 'Content', 'depicter' ), |
| 31 | 34 | 'description' => __( 'Make animated and interactive slider', 'depicter' ), |
| 32 | 35 | 'icon' => \Depicter::core()->assets()->getUrl() . '/resources/images/svg/logo-without-text.svg', |
| 33 | - 'params' => [ | |
| 34 | - [ | |
| 35 | - 'type' => 'dropdown', | |
| 36 | - 'class' => '', | |
| 37 | - 'admin_label' => true, | |
| 38 | - 'heading' => __( 'Select Slider', 'depicter' ), | |
| 39 | - 'param_name' => 'id', | |
| 40 | - 'value' => $this->get_sliders_list(), | |
| 41 | - ] | |
| 42 | - ] | |
| 36 | + 'params' => $this->get_fields() | |
| 43 | 37 | ]); |
| 44 | 38 | } |
| 45 | 39 | |
| 46 | 40 | /** |
| 47 | - * get sliders list | |
| 41 | + * get fields | |
| 48 | 42 | * |
| 49 | 43 | * @return array |
| 50 | 44 | * @throws \Exception |
| 51 | 45 | */ |
| 52 | - public function get_sliders_list() { | |
| 53 | - if ( ! isset( $_POST['action'] ) || $_POST['action'] != 'vc_edit_form' ) { | |
| 54 | - return []; | |
| 55 | - } | |
| 46 | + public function get_fields() { | |
| 56 | 47 | |
| 57 | 48 | $list = [ |
| 58 | 49 | __( 'Select Slider', 'depicter' ) => 0 |
| 59 | 50 | ]; |
| 60 | - $documents = \Depicter::documentRepository()->getList(); | |
| 51 | + $documents = \Depicter::documentRepository()->select( ['id', 'name', 'status'] )->orderBy('modified_at', 'DESC')->findAll()->get(); | |
| 52 | + $documents = $documents ? $documents->toArray() : []; | |
| 61 | 53 | foreach( $documents as $document ) { |
| 62 | 54 | $list[ "[#{$document['id']}]: " . $document['name'] ] = $document['id']; |
| 63 | 55 | } |
| 64 | - return $list; | |
| 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; | |
| 65 | 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 | + } | |
| 66 | 173 | } |
| 67 | 174 | |
| 68 | 175 | new Module(); |