init.php
151 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | use NTA_WhatsApp\Helper; |
| 7 | |
| 8 | function njt_wa_block_assets() |
| 9 | { // phpcs:ignore |
| 10 | // Register block styles for both frontend + backend. |
| 11 | |
| 12 | // wp_register_style( |
| 13 | // 'block-cgb-style-css', // Handle. |
| 14 | // plugins_url('dist/blocks.style.build.css', dirname(__FILE__)), // Block style CSS. |
| 15 | // is_admin() ? array('wp-editor') : null, // Dependency to include the CSS after it. |
| 16 | // null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time. |
| 17 | // ); |
| 18 | |
| 19 | // Register block editor script for backend. |
| 20 | wp_register_script( |
| 21 | 'block-cgb-block-js', // Handle. |
| 22 | plugins_url('/dist/blocks.build.js', dirname(__FILE__)), // Block.build.js: We register the block here. Built with Webpack. |
| 23 | array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor'), // Dependencies, defined above. |
| 24 | null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. |
| 25 | true // Enqueue the script in the footer. |
| 26 | ); |
| 27 | |
| 28 | // Register block editor styles for backend. |
| 29 | // wp_register_style( |
| 30 | // 'block-cgb-block-editor-css', // Handle. |
| 31 | // plugins_url('dist/blocks.editor.build.css', dirname(__FILE__)), // Block editor CSS. |
| 32 | // array('wp-edit-blocks'), // Dependency to include the CSS after it. |
| 33 | // null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time. |
| 34 | // ); |
| 35 | |
| 36 | // WP Localized globals. Use dynamic PHP stuff in JavaScript via `cgbGlobal` object. |
| 37 | wp_localize_script( |
| 38 | 'block-cgb-block-js', |
| 39 | 'njtwa', // Array containing dynamic data for a JS Global. |
| 40 | [ |
| 41 | 'pluginDirPath' => plugin_dir_path(__DIR__), |
| 42 | 'pluginDirUrl' => plugin_dir_url(__DIR__), |
| 43 | 'avatarDefaultUrl' => NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp_logo.svg', |
| 44 | 'gutenbergPreview' => NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp-button-preview.png', |
| 45 | 'nonce' => wp_create_nonce('njt-wa-gutenberg') |
| 46 | // Add more data here that you want to access from `cgbGlobal` object. |
| 47 | ] |
| 48 | ); |
| 49 | |
| 50 | register_block_type( |
| 51 | 'ninjateam/nta-whatsapp', |
| 52 | array( |
| 53 | 'style' => 'block-cgb-style-css', |
| 54 | 'editor_script' => 'block-cgb-block-js', |
| 55 | 'editor_style' => 'block-cgb-block-editor-css', |
| 56 | 'render_callback' => 'njt_wa_button_render', |
| 57 | 'attributes' => array( |
| 58 | 'isSelectedAccount' => array( |
| 59 | 'type' => 'string', |
| 60 | 'default' => -1, |
| 61 | ), |
| 62 | 'phoneNumber' => array( |
| 63 | 'type' => 'string', |
| 64 | 'default' => "", |
| 65 | ), |
| 66 | 'imageID' => array( |
| 67 | 'type' => 'number', |
| 68 | 'default' => 0, |
| 69 | ), |
| 70 | 'imageAlt' => array( |
| 71 | 'type' => 'string', |
| 72 | 'default' => "img", |
| 73 | ), |
| 74 | 'imageUrl' => array( |
| 75 | 'type' => 'string', |
| 76 | 'default' => "", |
| 77 | ), |
| 78 | 'buttonStyle' => array( |
| 79 | 'type' => 'string', |
| 80 | 'default' => "round", |
| 81 | ), |
| 82 | 'buttonColor' => array( |
| 83 | 'type' => 'string', |
| 84 | 'default' => "#2DB742", |
| 85 | ), |
| 86 | 'buttonTitle' => array( |
| 87 | 'type' => 'string', |
| 88 | 'default' => "John Doe", |
| 89 | ), |
| 90 | 'buttonInfo' => array( |
| 91 | 'type' => 'string', |
| 92 | 'default' => "Need help? Chat with us", |
| 93 | ), |
| 94 | 'textColor' => array( |
| 95 | 'type' => 'string', |
| 96 | 'default' => "#fff", |
| 97 | ), |
| 98 | 'waUrl' => array( |
| 99 | 'type' => 'string', |
| 100 | 'default' => "", |
| 101 | ), |
| 102 | 'className' => array( |
| 103 | 'type' => 'string', |
| 104 | ), |
| 105 | 'preview' => array( |
| 106 | 'type' => 'boolean', |
| 107 | 'default' => false |
| 108 | ) |
| 109 | ), |
| 110 | ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | function njt_wa_button_render($attributes) |
| 115 | { |
| 116 | if ($attributes['isSelectedAccount'] != -1) { |
| 117 | return do_shortcode("[njwa_button id={$attributes['isSelectedAccount']}]"); |
| 118 | } else { |
| 119 | $avatarClass = $attributes['imageUrl'] ? "wa__btn_w_img" : "wa__btn_w_icon"; |
| 120 | $btnStyleClass = $attributes['buttonStyle'] == "round" ? "wa__r_button" : "wa__sq_button"; |
| 121 | $btnStyleClass .= empty($attributes['buttonTitle']) ? ' wa__button_text_only' : ''; |
| 122 | $btn_icon_or_image = ''; |
| 123 | if (empty($attributes['imageUrl'])) { |
| 124 | $btn_icon_or_image = '<div class="wa__btn_icon"><img src="' . NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp_logo.svg' . '" alt=' . $attributes['imageAlt'] . '/></div>'; |
| 125 | } else { |
| 126 | $btn_icon_or_image = '<div class="wa__cs_img"><div class="wa__cs_img_wrap" style="background: url(' . $attributes['imageUrl'] . ') center center no-repeat; background-size: cover;"></div></div>'; |
| 127 | } |
| 128 | $html = ''; |
| 129 | $html .= '<div style="margin: 30px 0 30px;">'; |
| 130 | $html .= '<a target="_blank" href="https://api.whatsapp.com/send?phone=' . $attributes['phoneNumber'] . '" class="wa__button ' . $btnStyleClass . ' wa__stt_online ' . $avatarClass . '" style="background-color: ' . $attributes['buttonColor'] . '; color: ' . $attributes['textColor'] . '">'; |
| 131 | $html .= $btn_icon_or_image; |
| 132 | $html .= '<div class="wa__btn_txt">'; |
| 133 | if (!empty($attributes['buttonTitle'])) { |
| 134 | $html .= '<div class="wa__cs_info">'; |
| 135 | $html .= '<div class="wa__cs_name" style="color: ' . $attributes['textColor'] . '">' . $attributes['buttonTitle'] . '</div>'; |
| 136 | $html .= '<div class="wa__cs_status">Online</div></div>'; |
| 137 | } |
| 138 | $html .= '<div class="wa__btn_title">' . $attributes['buttonInfo'] . '</div></div></a></div>'; |
| 139 | return $html; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | function njt_wa_assets() |
| 144 | { |
| 145 | wp_enqueue_style('nta-css-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/css/style.css'); |
| 146 | } |
| 147 | |
| 148 | // Hook: Block assets. |
| 149 | add_action('init', 'njt_wa_block_assets'); |
| 150 | add_action('enqueue_block_assets', 'njt_wa_assets'); |
| 151 |