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