| @@ -1,0 +1,352 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\WpInit; | |
| 4 | + | |
| 5 | +defined('ABSPATH') || die('Restricted Access'); | |
| 6 | + | |
| 7 | +use AcyMailing\Classes\FieldClass; | |
| 8 | +use AcyMailing\Classes\ListClass; | |
| 9 | +use AcyMailing\Core\AcymParameter; | |
| 10 | + | |
| 11 | +class Gutenberg | |
| 12 | +{ | |
| 13 | + public $lists; | |
| 14 | + public $fields; | |
| 15 | + | |
| 16 | + public function __construct() | |
| 17 | + { | |
| 18 | + add_filter('block_categories_all', [$this, 'registerAcymCategory'], 1, 2); | |
| 19 | + $this->registerBlock(); | |
| 20 | + } | |
| 21 | + | |
| 22 | + public function registerBlock() | |
| 23 | + { | |
| 24 | + if (!function_exists('register_block_type')) return; | |
| 25 | + | |
| 26 | + $listClass = new ListClass(); | |
| 27 | + $this->lists = $listClass->getAllForSelect(false); | |
| 28 | + | |
| 29 | + $fieldClass = new FieldClass(); | |
| 30 | + $allFields = $fieldClass->getAll(); | |
| 31 | + $fields = []; | |
| 32 | + foreach ($allFields as $field) { | |
| 33 | + if ($field->id == 2 || intval($field->active) === 0) continue; | |
| 34 | + $field->name = acym_translation($field->name); | |
| 35 | + $fields[$field->id] = $field; | |
| 36 | + } | |
| 37 | + $this->fields = $fields; | |
| 38 | + | |
| 39 | + $this->registerBlockSubscription(); | |
| 40 | + $this->registerBlockProfile(); | |
| 41 | + $this->registerBlockArchive(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function registerBlockSubscription() | |
| 45 | + { | |
| 46 | + wp_register_script( | |
| 47 | + 'gutenberg-acymailing-subscription-form', | |
| 48 | + ACYM_JS.'gutenberg/subscription.min.js?time='.time(), | |
| 49 | + [], | |
| 50 | + '11.1.0', | |
| 51 | + [ | |
| 52 | + 'in_footer' => false, | |
| 53 | + ] | |
| 54 | + ); | |
| 55 | + wp_add_inline_script( | |
| 56 | + 'gutenberg-acymailing-subscription-form', | |
| 57 | + 'var acym_lists = '.json_encode($this->lists).'; | |
| 58 | + var ACYM_JS_TXT = '.acym_getJSMessages().'; | |
| 59 | + var acym_fields = '.json_encode($this->fields).'; | |
| 60 | + var acym_subscription_nonce = '.json_encode(wp_create_nonce('acymnonce')).';' | |
| 61 | + ); | |
| 62 | + | |
| 63 | + $basicAttribute = [ | |
| 64 | + 'title' => [ | |
| 65 | + 'type' => 'string', | |
| 66 | + 'default' => 'Receive our newsletters', | |
| 67 | + ], | |
| 68 | + 'mode' => [ | |
| 69 | + 'type' => 'string', | |
| 70 | + 'default' => 'tableless', | |
| 71 | + ], | |
| 72 | + 'hiddenlists' => [ | |
| 73 | + 'type' => 'array', | |
| 74 | + ], | |
| 75 | + 'displists' => [ | |
| 76 | + 'type' => 'array', | |
| 77 | + ], | |
| 78 | + 'listschecked' => [ | |
| 79 | + 'type' => 'array', | |
| 80 | + ], | |
| 81 | + 'listposition' => [ | |
| 82 | + 'type' => 'string', | |
| 83 | + 'default' => 'before', | |
| 84 | + ], | |
| 85 | + 'fields' => [ | |
| 86 | + 'type' => 'array', | |
| 87 | + ], | |
| 88 | + 'textmode' => [ | |
| 89 | + 'type' => 'string', | |
| 90 | + 'default' => '1', | |
| 91 | + ], | |
| 92 | + 'termscontent' => [ | |
| 93 | + 'type' => 'string', | |
| 94 | + 'default' => '', | |
| 95 | + ], | |
| 96 | + 'privacypolicy' => [ | |
| 97 | + 'type' => 'string', | |
| 98 | + 'default' => '', | |
| 99 | + ], | |
| 100 | + 'termscontentURL' => [ | |
| 101 | + 'type' => 'string', | |
| 102 | + 'default' => '', | |
| 103 | + ], | |
| 104 | + 'privacypolicyURL' => [ | |
| 105 | + 'type' => 'string', | |
| 106 | + 'default' => '', | |
| 107 | + ], | |
| 108 | + 'trackingconsent' => [ | |
| 109 | + 'type' => 'string', | |
| 110 | + 'default' => '0', | |
| 111 | + ], | |
| 112 | + 'unsub' => [ | |
| 113 | + 'type' => 'string', | |
| 114 | + 'default' => '0', | |
| 115 | + ], | |
| 116 | + 'unsubtext' => [ | |
| 117 | + 'type' => 'string', | |
| 118 | + 'default' => 'Unsubscribe', | |
| 119 | + ], | |
| 120 | + 'unsubredirect' => [ | |
| 121 | + 'type' => 'string', | |
| 122 | + ], | |
| 123 | + 'successmode' => [ | |
| 124 | + 'type' => 'string', | |
| 125 | + 'default' => 'replace', | |
| 126 | + ], | |
| 127 | + | |
| 128 | + 'redirect' => [ | |
| 129 | + 'type' => 'string', | |
| 130 | + ], | |
| 131 | + 'userinfo' => [ | |
| 132 | + 'type' => 'string', | |
| 133 | + 'default' => '1', | |
| 134 | + ], | |
| 135 | + 'introtext' => [ | |
| 136 | + 'type' => 'string', | |
| 137 | + ], | |
| 138 | + 'posttext' => [ | |
| 139 | + 'type' => 'string', | |
| 140 | + ], | |
| 141 | + 'alignment' => [ | |
| 142 | + 'type' => 'string', | |
| 143 | + 'default' => 'none', | |
| 144 | + ], | |
| 145 | + 'source' => [ | |
| 146 | + 'type' => 'string', | |
| 147 | + 'default' => 'gutenberg_subscription_form', | |
| 148 | + ], | |
| 149 | + ]; | |
| 150 | + $moreAttributes = []; | |
| 151 | + if (acym_isMultilingual()) { | |
| 152 | + foreach (acym_getMultilingualLanguages() as &$language) { | |
| 153 | + $moreAttributes['subtext_'.$language->language] = ['type' => 'string', 'default' => 'Subscribe']; | |
| 154 | + $moreAttributes['subtextlogged_'.$language->language] = ['type' => 'string', 'default' => 'Subscribe']; | |
| 155 | + $moreAttributes['confirmation_message_'.$language->language] = ['type' => 'string', 'default' => '']; | |
| 156 | + } | |
| 157 | + } else { | |
| 158 | + $moreAttributes['subtext'] = [ | |
| 159 | + 'type' => 'string', | |
| 160 | + 'default' => 'Subscribe', | |
| 161 | + ]; | |
| 162 | + $moreAttributes['subtextlogged'] = [ | |
| 163 | + 'type' => 'string', | |
| 164 | + 'default' => 'Subscribe', | |
| 165 | + ]; | |
| 166 | + $moreAttributes['confirmation_message'] = [ | |
| 167 | + 'type' => 'string', | |
| 168 | + 'default' => '', | |
| 169 | + | |
| 170 | + ]; | |
| 171 | + } | |
| 172 | + $attributes = array_merge($basicAttribute, $moreAttributes); | |
| 173 | + | |
| 174 | + register_block_type( | |
| 175 | + 'acymailing/subscription-form', | |
| 176 | + [ | |
| 177 | + 'apiVersion' => 2, | |
| 178 | + 'editor_script' => 'gutenberg-acymailing-subscription-form', | |
| 179 | + 'render_callback' => [$this, 'renderCallback'], | |
| 180 | + 'attributes' => $attributes, | |
| 181 | + ] | |
| 182 | + ); | |
| 183 | + } | |
| 184 | + | |
| 185 | + public function registerAcymCategory($categories, $post) | |
| 186 | + { | |
| 187 | + return array_merge( | |
| 188 | + $categories, | |
| 189 | + [ | |
| 190 | + [ | |
| 191 | + 'slug' => 'acymailing', | |
| 192 | + 'title' => 'AcyMailing', | |
| 193 | + 'icon' => 'editor-table', | |
| 194 | + ], | |
| 195 | + ] | |
| 196 | + ); | |
| 197 | + } | |
| 198 | + | |
| 199 | + public function renderCallback($block_attributes, $content) | |
| 200 | + { | |
| 201 | + if (!array_key_exists('subtext', $block_attributes)) { | |
| 202 | + $moreBlockAttributes = []; | |
| 203 | + $currentLanguage = strtolower('subtext_'.acym_getLanguageTag()); | |
| 204 | + $currentLanguageLogged = strtolower('subtextlogged_'.acym_getLanguageTag()); | |
| 205 | + $confirmMessageLangue = strtolower('confirmation_message_'.acym_getLanguageTag()); | |
| 206 | + $moreBlockAttributes['subtext'] = $block_attributes[$currentLanguage]; | |
| 207 | + $moreBlockAttributes['subtextlogged'] = $block_attributes[$currentLanguageLogged]; | |
| 208 | + $moreBlockAttributes['confirmation_message'] = $block_attributes[$confirmMessageLangue]; | |
| 209 | + $block_attributes = array_merge($block_attributes, $moreBlockAttributes); | |
| 210 | + } | |
| 211 | + $params = new AcymParameter($block_attributes); | |
| 212 | + | |
| 213 | + ob_start(); | |
| 214 | + acym_renderForm( | |
| 215 | + $params, | |
| 216 | + ['disableButtons' => strpos(acym_currentURL(), 'block-renderer') !== false] | |
| 217 | + ); | |
| 218 | + | |
| 219 | + return ob_get_clean(); | |
| 220 | + } | |
| 221 | + | |
| 222 | + public function registerBlockProfile() | |
| 223 | + { | |
| 224 | + if (!function_exists('register_block_type')) return; | |
| 225 | + | |
| 226 | + wp_register_script( | |
| 227 | + 'gutenberg-acymailing-profile', | |
| 228 | + ACYM_JS.'gutenberg/profile.min.js?time='.time(), | |
| 229 | + [], | |
| 230 | + '11.1.0', | |
| 231 | + [ | |
| 232 | + 'in_footer' => false, | |
| 233 | + ] | |
| 234 | + ); | |
| 235 | + wp_add_inline_script( | |
| 236 | + 'gutenberg-acymailing-profile', | |
| 237 | + 'var acym_lists_profile = '.json_encode($this->lists).'; | |
| 238 | + var ACYM_JS_TXT = '.acym_getJSMessages().'; | |
| 239 | + var acym_fields_profile = '.json_encode($this->fields) | |
| 240 | + ); | |
| 241 | + | |
| 242 | + register_block_type( | |
| 243 | + 'acymailing/profile', | |
| 244 | + [ | |
| 245 | + 'apiVersion' => 2, | |
| 246 | + 'editor_script' => 'gutenberg-acymailing-profile', | |
| 247 | + 'render_callback' => [$this, 'renderCallbackProfile'], | |
| 248 | + 'attributes' => [ | |
| 249 | + 'title' => [ | |
| 250 | + 'type' => 'string', | |
| 251 | + 'default' => 'Profile', | |
| 252 | + ], | |
| 253 | + 'lists' => [ | |
| 254 | + 'type' => 'array', | |
| 255 | + ], | |
| 256 | + 'listsdropdown' => [ | |
| 257 | + 'type' => 'array', | |
| 258 | + ], | |
| 259 | + 'listschecked' => [ | |
| 260 | + 'type' => 'array', | |
| 261 | + ], | |
| 262 | + 'hiddenlists' => [ | |
| 263 | + 'type' => 'array', | |
| 264 | + ], | |
| 265 | + 'fields' => [ | |
| 266 | + 'type' => 'array', | |
| 267 | + ], | |
| 268 | + 'introtext' => [ | |
| 269 | + 'type' => 'string', | |
| 270 | + ], | |
| 271 | + 'posttext' => [ | |
| 272 | + 'type' => 'string', | |
| 273 | + ], | |
| 274 | + 'source' => [ | |
| 275 | + 'type' => 'string', | |
| 276 | + 'default' => 'gutenberg_profile', | |
| 277 | + ], | |
| 278 | + ], | |
| 279 | + ] | |
| 280 | + ); | |
| 281 | + } | |
| 282 | + | |
| 283 | + | |
| 284 | + public function renderCallbackProfile($block_attributes, $content) | |
| 285 | + { | |
| 286 | + $params = new AcymParameter($block_attributes); | |
| 287 | + | |
| 288 | + return acym_renderFormProfile( | |
| 289 | + $params, | |
| 290 | + ['disableButtons' => strpos(acym_currentURL(), 'block-renderer') !== false] | |
| 291 | + ); | |
| 292 | + } | |
| 293 | + | |
| 294 | + public function registerBlockArchive() | |
| 295 | + { | |
| 296 | + if (!function_exists('register_block_type')) return; | |
| 297 | + | |
| 298 | + wp_register_script( | |
| 299 | + 'gutenberg-acymailing-archive', | |
| 300 | + ACYM_JS.'gutenberg/archive.min.js?time='.time(), | |
| 301 | + [], | |
| 302 | + '11.1.0', | |
| 303 | + [ | |
| 304 | + 'in_footer' => false, | |
| 305 | + ] | |
| 306 | + ); | |
| 307 | + wp_add_inline_script( | |
| 308 | + 'gutenberg-acymailing-archive', | |
| 309 | + 'var acym_lists_archive = '.json_encode($this->lists).'; | |
| 310 | + var ACYM_JS_TXT = '.acym_getJSMessages() | |
| 311 | + ); | |
| 312 | + | |
| 313 | + register_block_type( | |
| 314 | + 'acymailing/archive', | |
| 315 | + [ | |
| 316 | + 'apiVersion' => 2, | |
| 317 | + 'editor_script' => 'gutenberg-acymailing-archive', | |
| 318 | + 'render_callback' => [$this, 'renderCallbackArchive'], | |
| 319 | + 'attributes' => [ | |
| 320 | + 'title' => [ | |
| 321 | + 'type' => 'string', | |
| 322 | + 'default' => 'See all newsletters', | |
| 323 | + ], | |
| 324 | + 'archiveNbNewslettersPerPage' => [ | |
| 325 | + 'type' => 'string', | |
| 326 | + 'default' => '20', | |
| 327 | + ], | |
| 328 | + 'lists' => [ | |
| 329 | + 'type' => 'array', | |
| 330 | + ], | |
| 331 | + 'popup' => [ | |
| 332 | + 'type' => 'array', | |
| 333 | + ], | |
| 334 | + 'displayUserListOnly' => [ | |
| 335 | + 'type' => 'array', | |
| 336 | + ], | |
| 337 | + ], | |
| 338 | + ] | |
| 339 | + ); | |
| 340 | + } | |
| 341 | + | |
| 342 | + | |
| 343 | + public function renderCallbackArchive($block_attributes, $content) | |
| 344 | + { | |
| 345 | + $params = new AcymParameter($block_attributes); | |
| 346 | + | |
| 347 | + return acym_renderFormArchive( | |
| 348 | + $params, | |
| 349 | + ['disableButtons' => strpos(acym_currentURL(), 'block-renderer') !== false] | |
| 350 | + ); | |
| 351 | + } | |
| 352 | +} | |