PluginProbe ʕ •ᴥ•ʔ
WP Chat App / 3.6.9
WP Chat App v3.6.9
3.8.1 3.8.2 trunk 2.6 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5 3.6 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0
wp-whatsapp / includes / PostType.php
wp-whatsapp / includes Last commit date
Recommended 1 year ago Support 1 year ago Cross.php 1 year ago Fallback.php 1 year ago Fields.php 1 year ago Helper.php 1 year ago I18n.php 1 year ago Plugin.php 1 year ago Popup.php 1 year ago PostType.php 1 year ago Review.php 1 year ago Settings.php 1 year ago Shortcode.php 1 year ago Upgrade.php 1 year ago
PostType.php
296 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' => 'manage_options',
71 'read_post' => 'manage_options',
72 'delete_post' => 'manage_options',
73 'edit_posts' => 'manage_options',
74 'edit_others_posts' => 'manage_options',
75 'delete_posts' => 'manage_options',
76 'publish_posts' => 'manage_options',
77 'read_private_posts' => 'manage_options',
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( $_POST['predefinedText'] ),
186 'willBeBackText' => sanitize_text_field( $_POST['willBeBackText'] ),
187 'dayOffsText' => sanitize_text_field( $_POST['dayOffsText'] ),
188 'isAlwaysAvailable' => 'ON',
189 );
190
191 $daysOfWeekWorking = $_POST['daysOfWeekWorking'];
192
193 $new_account['daysOfWeekWorking'] = array_map(
194 function ( $day ) {
195 return array(
196 'isWorkingOnDay' => isset( $day['isWorkingOnDay'] ) ? 'ON' : 'OFF',
197 'workHours' => $day['workHours'],
198 );
199 },
200 $daysOfWeekWorking
201 );
202
203 update_post_meta( $post_id, 'nta_wa_account_info', $new_account );
204
205 update_post_meta(
206 $post_id,
207 'nta_wa_button_styles',
208 array(
209 'type' => sanitize_text_field( $_POST['btnType'] ),
210 'backgroundColor' => sanitize_text_field( $_POST['backgroundColor'] ),
211 'textColor' => sanitize_text_field( $_POST['textColor'] ),
212 'label' => sanitize_text_field( $_POST['label'] ),
213 'width' => 300,
214 'height' => 64,
215 )
216 );
217
218 $isSaveNewPost = Helper::isSaveNewPost( sanitize_text_field( $_POST['_wp_http_referer'] ) );
219 if ( $isSaveNewPost ) {
220 update_post_meta( $post_id, 'nta_wa_widget_show', 'OFF' );
221 update_post_meta( $post_id, 'nta_wa_widget_position', 0 );
222 update_post_meta( $post_id, 'nta_wa_wc_show', 'OFF' );
223 update_post_meta( $post_id, 'nta_wa_wc_position', 0 );
224 }
225 }
226
227 public function meta_form_account( $post ) {
228 $screen = get_current_screen();
229 $daysOfWeek = array( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' );
230
231 wp_nonce_field( 'save_form_account', 'form_account_nonce' );
232 if ( $screen->action === 'add' ) {
233 $meta = Fields::getDefaultMetaAccount( $daysOfWeek );
234 } else {
235 $meta = get_post_meta( $post->ID, 'nta_wa_account_info', true );
236 $meta['nta_wa_widget_show'] = get_post_meta( $post->ID, 'nta_wa_widget_show', true );
237 $meta['nta_wa_widget_position'] = get_post_meta( $post->ID, 'nta_wa_widget_position', true );
238 $meta['nta_wa_wc_show'] = get_post_meta( $post->ID, 'nta_wa_wc_show', true );
239 $meta['nta_wa_wc_position'] = get_post_meta( $post->ID, 'nta_wa_wc_position', true );
240 }
241 require NTA_WHATSAPP_PLUGIN_DIR . 'views/meta-accounts.php';
242 }
243
244 public function account_shortcode_form() {
245 ?>
246 <p>Copy the shortcode below and paste it into the editor to display the button.</p>
247 <p><input type="text" id="nta-button-shortcode-copy" value="[njwa_button id=&quot;<?php echo esc_attr( get_the_ID() ); ?>&quot;]" class="widefat" readonly=""></p>
248 <p class="nta-shortcode-copy-status hidden" style="color: green"><strong>Copied!</strong></p>
249 <?php
250 }
251
252 public function meta_form_button_style( $post ) {
253 $buttonStyles = Fields::getButtonStyles( $post->ID );
254 $buttonStyles['avatar'] = get_the_post_thumbnail_url( $post->ID );
255 $buttonStyles['title'] = $post->post_title;
256 require NTA_WHATSAPP_PLUGIN_DIR . 'views/meta-button-style.php';
257 }
258
259 public function manager_accounts_columns( $columns ) {
260 $columns = array(
261 'cb' => '<input type="checkbox" />',
262 'title' => __( 'Account Name', 'wp-whatsapp' ),
263 'avatar' => __( 'Avatar', 'wp-whatsapp' ),
264 'number' => __( 'Number', 'wp-whatsapp' ),
265 'nta_title' => __( 'Title', 'wp-whatsapp' ),
266 'activedays' => __( 'Active Days', 'wp-whatsapp' ),
267 'shortcode' => __( 'Shortcode', 'wp-whatsapp' ),
268 );
269 return $columns;
270 }
271
272 public function manager_accounts_show_columns( $name, $post_id ) {
273 $data_account = get_post_meta( $post_id, 'nta_wa_account_info', true );
274 if ( empty( $data_account ) ) {
275 return;
276 }
277 switch ( $name ) {
278 case 'avatar':
279 the_post_thumbnail( 'thumbnail', array( 'class' => 'img-size-table' ) );
280 break;
281 case 'number':
282 echo esc_html( $data_account['number'] );
283 break;
284 case 'nta_title':
285 echo esc_html( $data_account['title'] );
286 break;
287 case 'activedays':
288 echo esc_html( Helper::printWorkingDays( $data_account ) );
289 break;
290 case 'shortcode':
291 echo '<input type="text" class="nta-shortcode-table" name="country" value="[njwa_button id=&quot;' . esc_attr( $post_id ) . '&quot;]" readonly>';
292 break;
293 }
294 }
295 }
296