Recommended
2 years ago
Support
2 years ago
Cross.php
2 years ago
Fallback.php
2 years ago
Fields.php
2 years ago
Helper.php
2 years ago
I18n.php
2 years ago
Plugin.php
2 years ago
Popup.php
2 years ago
PostType.php
2 years ago
Review.php
2 years ago
Settings.php
2 years ago
Shortcode.php
2 years ago
Upgrade.php
2 years ago
PostType.php
295 lines
| 1 | <?php |
| 2 | namespace NTA_WhatsApp; |
| 3 | |
| 4 | use NTA_WhatsApp\Fields; |
| 5 | use NTA_WhatsApp\Helper; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | class PostType |
| 10 | { |
| 11 | protected static $instance = null; |
| 12 | |
| 13 | public static function getInstance() |
| 14 | { |
| 15 | if (null == self::$instance) { |
| 16 | self::$instance = new self; |
| 17 | self::$instance->doHooks(); |
| 18 | } |
| 19 | return self::$instance; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | public function __construct() |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | private function doHooks(){ |
| 28 | add_action('init', array($this, 'register_post_type')); |
| 29 | add_action('save_post_whatsapp-accounts', [$this, 'save_account'], 10, 999); |
| 30 | add_action('add_meta_boxes', [$this, 'add_meta_boxes']); |
| 31 | |
| 32 | add_filter('manage_whatsapp-accounts_posts_columns', [$this, 'manager_accounts_columns'], 10, 1); |
| 33 | add_action('manage_whatsapp-accounts_posts_custom_column', [$this, 'manager_accounts_show_columns'], 10, 2); |
| 34 | add_filter('enter_title_here', [$this, 'replace_title'], 20, 2); |
| 35 | add_action('wp_print_scripts', [$this, 'disable_autosave']); |
| 36 | |
| 37 | add_action('wp_ajax_njt_wa_get_account', [$this, 'ajax_get_account']); |
| 38 | } |
| 39 | |
| 40 | public function register_post_type(){ |
| 41 | $labels = array( |
| 42 | 'name' => __('WhatsApp Accounts'), |
| 43 | 'singular_name' => __('Whatsapp Account'), |
| 44 | 'add_new' => __('Add New Account', 'ninjateam-whatsapp'), |
| 45 | 'add_new_item' => __('Add New Account', 'ninjateam-whatsapp'), |
| 46 | 'edit_item' => __('Edit Account', 'ninjateam-whatsapp'), |
| 47 | 'new_item' => __('New Account', 'ninjateam-whatsapp'), |
| 48 | 'all_items' => __('All Accounts', 'ninjateam-whatsapp'), |
| 49 | 'view_item' => __('View Accounts', 'ninjateam-whatsapp'), |
| 50 | 'search_items' => __('Search Account', 'ninjateam-whatsapp'), |
| 51 | 'featured_image' => __('Avatar (PRO)', 'ninjateam-whatsapp'), |
| 52 | 'set_featured_image' => __('Select an image', 'ninjateam-whatsapp'), |
| 53 | 'remove_featured_image' => __('Remove avatar', 'ninjateam-whatsapp'), |
| 54 | ); |
| 55 | |
| 56 | $args = array( |
| 57 | 'labels' => $labels, |
| 58 | 'description' => __('Manager Accounts', 'ninjateam-whatsapp'), |
| 59 | 'public' => false, |
| 60 | 'show_ui' => true, |
| 61 | 'has_archive' => true, |
| 62 | 'show_in_admin_bar' => false, |
| 63 | 'show_in_rest' => true, |
| 64 | 'show_in_menu' => 'nta_whatsapp', |
| 65 | 'menu_position' => 100, |
| 66 | 'query_var' => 'whatsapp-accounts', |
| 67 | 'supports' => array( |
| 68 | 'title', |
| 69 | 'thumbnail', |
| 70 | ), |
| 71 | 'capabilities' => array( |
| 72 | 'edit_post' => 'manage_options', |
| 73 | 'read_post' => 'manage_options', |
| 74 | 'delete_post' => 'manage_options', |
| 75 | 'edit_posts' => 'manage_options', |
| 76 | 'edit_others_posts' => 'manage_options', |
| 77 | 'delete_posts' => 'manage_options', |
| 78 | 'publish_posts' => 'manage_options', |
| 79 | 'read_private_posts' => 'manage_options' |
| 80 | ), |
| 81 | ); |
| 82 | register_post_type('whatsapp-accounts', $args); |
| 83 | } |
| 84 | |
| 85 | public function add_meta_boxes(){ |
| 86 | $current_screen = get_current_screen(); |
| 87 | |
| 88 | add_meta_box('whatsapp-account-info', 'WhatsApp Account Information', [$this, 'meta_form_account'], 'whatsapp-accounts', 'normal'); |
| 89 | add_meta_box('whatsapp-button-style', 'Button Style', [$this, 'meta_form_button_style'], 'whatsapp-accounts', 'normal'); |
| 90 | if ($current_screen->action !== 'add') { |
| 91 | add_meta_box('whatsapp-button-shortcode', 'Shortcode for this account', [$this, 'account_shortcode_form'], 'whatsapp-accounts', 'side'); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public function disable_autosave() { |
| 96 | if (get_post_type() === 'whatsapp-accounts') { |
| 97 | wp_deregister_script('autosave'); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public function replace_title($title, $post){ |
| 102 | if ($post->post_type == 'whatsapp-accounts') { |
| 103 | $my_title = "Account Name"; |
| 104 | return $my_title; |
| 105 | } |
| 106 | |
| 107 | return $title; |
| 108 | } |
| 109 | |
| 110 | public function get_posts($argsQuery = array()) |
| 111 | { |
| 112 | $defaultArgs = array( |
| 113 | 'post_type' => 'whatsapp-accounts', |
| 114 | 'post_status' => 'publish', |
| 115 | 'numberposts' => -1, |
| 116 | ); |
| 117 | |
| 118 | $args = apply_filters('njt_wa_get_post_type', $defaultArgs); |
| 119 | $args = wp_parse_args($argsQuery, $args); |
| 120 | $account_list = get_posts($args); |
| 121 | |
| 122 | return $account_list; |
| 123 | } |
| 124 | |
| 125 | public function get_active_widget_accounts() |
| 126 | { |
| 127 | return $this->get_posts( |
| 128 | array( |
| 129 | 'meta_key' => 'nta_wa_widget_position', |
| 130 | 'orderby' => 'meta_value_num', |
| 131 | 'order' => 'ASC', |
| 132 | 'meta_query' => array( |
| 133 | array( |
| 134 | 'key' => 'nta_wa_widget_show', |
| 135 | 'value' => 'ON', |
| 136 | 'compare' => '=' |
| 137 | ) |
| 138 | )) |
| 139 | |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | public function get_active_woocommerce_accounts() |
| 144 | { |
| 145 | return $this->get_posts( |
| 146 | array( |
| 147 | 'meta_key' => 'nta_wa_wc_position', |
| 148 | 'orderby' => 'meta_value_num', |
| 149 | 'order' => 'ASC', |
| 150 | 'meta_query' => array( |
| 151 | array( |
| 152 | 'key' => 'nta_wa_wc_show', |
| 153 | 'value' => 'ON', |
| 154 | 'compare' => '=' |
| 155 | ) |
| 156 | )) |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | public function ajax_get_account(){ |
| 161 | check_ajax_referer('njt-wa-gutenberg', 'nonce', true); |
| 162 | $id = sanitize_text_field($_POST['id']); |
| 163 | $metaInfo = get_post_meta($id, 'nta_wa_account_info', true); |
| 164 | $metaStyle = get_post_meta($id, 'nta_wa_button_styles', true); |
| 165 | wp_send_json([ |
| 166 | 'imageUrl' => get_the_post_thumbnail_url($id), |
| 167 | 'buttonTitle' => $buttonName, |
| 168 | 'metaInfo' => $metaInfo, |
| 169 | 'metaStyle' => $metaStyle |
| 170 | ]); |
| 171 | } |
| 172 | |
| 173 | public function save_account($post_id, $post, $update) |
| 174 | { |
| 175 | remove_all_actions('save_post'); |
| 176 | |
| 177 | if (isset($post->post_status) && 'auto-draft' == $post->post_status) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (!wp_verify_nonce($_POST['form_account_nonce'], 'save_form_account')) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | $new_account = array( |
| 186 | 'number' => sanitize_text_field($_POST['number']), |
| 187 | 'title' => sanitize_text_field($_POST['title']), |
| 188 | 'predefinedText' => wp_kses_post($_POST['predefinedText']), |
| 189 | 'willBeBackText' => sanitize_text_field($_POST['willBeBackText']), |
| 190 | 'dayOffsText' => sanitize_text_field($_POST['dayOffsText']), |
| 191 | 'isAlwaysAvailable' => 'ON' |
| 192 | ); |
| 193 | |
| 194 | $daysOfWeekWorking = $_POST['daysOfWeekWorking']; |
| 195 | |
| 196 | $new_account['daysOfWeekWorking'] = array_map(function($day){ |
| 197 | return [ |
| 198 | 'isWorkingOnDay' => isset($day['isWorkingOnDay']) ? 'ON' : 'OFF', |
| 199 | 'workHours' => $day['workHours'] |
| 200 | ]; |
| 201 | }, $daysOfWeekWorking); |
| 202 | |
| 203 | update_post_meta($post_id, 'nta_wa_account_info', $new_account); |
| 204 | |
| 205 | update_post_meta($post_id, 'nta_wa_button_styles', array( |
| 206 | 'type' => sanitize_text_field($_POST['btnType']), |
| 207 | 'backgroundColor' => sanitize_text_field($_POST['backgroundColor']), |
| 208 | 'textColor' => sanitize_text_field($_POST['textColor']), |
| 209 | 'label' => sanitize_text_field($_POST['label']), |
| 210 | 'width' => 300, |
| 211 | 'height' => 64, |
| 212 | )); |
| 213 | |
| 214 | $isSaveNewPost = Helper::isSaveNewPost(sanitize_text_field($_POST['_wp_http_referer'])); |
| 215 | if ( $isSaveNewPost ) { |
| 216 | update_post_meta($post_id, 'nta_wa_widget_show', 'OFF'); |
| 217 | update_post_meta($post_id, 'nta_wa_widget_position', 0); |
| 218 | update_post_meta($post_id, 'nta_wa_wc_show', 'OFF'); |
| 219 | update_post_meta($post_id, 'nta_wa_wc_position', 0); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | public function meta_form_account($post) |
| 224 | { |
| 225 | $screen = get_current_screen(); |
| 226 | $daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; |
| 227 | |
| 228 | wp_nonce_field('save_form_account', 'form_account_nonce'); |
| 229 | if ( $screen->action == 'add' ) { |
| 230 | $meta = Fields::getDefaultMetaAccount($daysOfWeek); |
| 231 | } else { |
| 232 | $meta = get_post_meta($post->ID, 'nta_wa_account_info', true); |
| 233 | $meta['nta_wa_widget_show'] = get_post_meta($post->ID, 'nta_wa_widget_show', true); |
| 234 | $meta['nta_wa_widget_position'] = get_post_meta($post->ID, 'nta_wa_widget_position', true); |
| 235 | $meta['nta_wa_wc_show'] = get_post_meta($post->ID, 'nta_wa_wc_show', true); |
| 236 | $meta['nta_wa_wc_position'] = get_post_meta($post->ID, 'nta_wa_wc_position', true); |
| 237 | } |
| 238 | require NTA_WHATSAPP_PLUGIN_DIR . 'views/meta-accounts.php'; |
| 239 | } |
| 240 | |
| 241 | public function account_shortcode_form() |
| 242 | { |
| 243 | ?> |
| 244 | <p>Copy the shortcode below and paste it into the editor to display the button.</p> |
| 245 | <p><input type="text" id="nta-button-shortcode-copy" value="[njwa_button id="<?php echo esc_attr(get_the_ID()) ?>"]" class="widefat" readonly=""></p> |
| 246 | <p class="nta-shortcode-copy-status hidden" style="color: green"><strong>Copied!</strong></p> |
| 247 | <?php |
| 248 | } |
| 249 | |
| 250 | public function meta_form_button_style($post) |
| 251 | { |
| 252 | $buttonStyles = Fields::getButtonStyles($post->ID); |
| 253 | $buttonStyles['avatar'] = get_the_post_thumbnail_url($post->ID); |
| 254 | $buttonStyles['title'] = $post->post_title; |
| 255 | require NTA_WHATSAPP_PLUGIN_DIR . 'views/meta-button-style.php'; |
| 256 | } |
| 257 | |
| 258 | public function manager_accounts_columns($columns) |
| 259 | { |
| 260 | $columns = array( |
| 261 | 'cb' => '<input type="checkbox" />', |
| 262 | 'title' => __('Account Name', 'ninjateam-whatsapp'), |
| 263 | 'avatar' => __('Avatar', 'ninjateam-whatsapp'), |
| 264 | 'number' => __('Number', 'ninjateam-whatsapp'), |
| 265 | 'nta_title' => __('Title', 'ninjateam-whatsapp'), |
| 266 | 'activedays' => __('Active Days', 'ninjateam-whatsapp'), |
| 267 | 'shortcode' => __('Shortcode', 'ninjateam-whatsapp'), |
| 268 | ); |
| 269 | return $columns; |
| 270 | } |
| 271 | |
| 272 | public function manager_accounts_show_columns($name, $post_id) |
| 273 | { |
| 274 | $data_account = get_post_meta($post_id, 'nta_wa_account_info', true); |
| 275 | if (empty($data_account)) return; |
| 276 | switch ($name) { |
| 277 | case 'avatar': |
| 278 | the_post_thumbnail('thumbnail', array('class' => 'img-size-table')); |
| 279 | break; |
| 280 | case 'number': |
| 281 | echo esc_html($data_account['number']); |
| 282 | break; |
| 283 | case 'nta_title': |
| 284 | echo esc_html($data_account['title']); |
| 285 | break; |
| 286 | case 'activedays': |
| 287 | echo esc_html(Helper::printWorkingDays($data_account)); |
| 288 | break; |
| 289 | case 'shortcode': |
| 290 | echo '<input type="text" class="nta-shortcode-table" name="country" value="[njwa_button id="' . esc_attr($post_id) . '"]" readonly>'; |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 |