PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / classes / admin / CategoriesController.php

CategoriesController.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/classes/admin/CategoriesController.php

126 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes\Admin;
4
5 use Bookit\Classes\Database\Categories;
6 use Bookit\Helpers\CleanHelper;
7
8 class CategoriesController extends DashboardController {
9
10 private static function getCleanRules() {
11 return array(
12 'id' => array( 'type' => 'intval' ),
13 'name' => array( 'type' => 'strval' ),
14 );
15 }
16
17 /**
18 * Validate post data
19 */
20 public static function validate( $data ) {
21 $errors = array();
22
23 if ( ! $data['name'] || ( $data['name'] && strlen( $data['name'] ) < 3 || strlen( $data['name'] ) > 25 ) ) {
24 $errors['category_name'] = __( "Category Name can't be empty and must be between 3 and 25 characters long", 'bookit' );
25 wp_send_json_error(
26 array(
27 'errors' => $errors,
28 'message' => __( 'Error occurred!', 'bookit' ),
29 )
30 );
31 }
32
33 if ( ! isset( $data['id'] ) ) {
34 $exist_category = Categories::get( 'name', $data['name'] );
35 if ( null !== $exist_category ) {
36 $errors['category_name'] = __( 'Category with such name already exist', 'bookit' );
37 wp_send_json_error(
38 array(
39 'errors' => $errors,
40 'message' => __( 'Error occurred!', 'bookit' ),
41 )
42 );
43 }
44 }
45 }
46
47 /**
48 * Save Category
49 */
50 public static function save() {
51 check_ajax_referer( 'bookit_save_category', 'nonce' );
52
53 if ( ! current_user_can( 'manage_options' ) ) {
54 return false;
55 }
56
57 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
58 self::validate( $data );
59
60 if ( ! empty( $data ) ) {
61 if ( ! empty( $data['id'] ) ) {
62 Categories::update( $data, array( 'id' => $data['id'] ) );
63 } else {
64 Categories::insert( $data );
65 $data['id'] = Categories::insert_id();
66 }
67
68 do_action( 'bookit_category_saved', $data['id'] );
69
70 wp_send_json_success(
71 array(
72 'id' => $data['id'],
73 'message' => __( 'Category Saved!', 'bookit' ),
74 )
75 );
76 }
77
78 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
79 }
80
81 /** Get Categories Assosiated data by id **/
82 public static function get_assosiated_total_data_by_id() {
83 check_ajax_referer( 'bookit_get_category_assosiated_total_data', 'nonce' );
84
85 if ( ! current_user_can( 'manage_options' ) ) {
86 return false;
87 }
88
89 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
90
91 if ( empty( $data['id'] ) ) {
92 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
93 }
94
95 $total = Categories::get_category_total_assosiated_data( $data['id'] );
96
97 $response = array( 'total' => (array) $total );
98 wp_send_json_success( $response );
99 }
100
101 /**
102 * Delete the Category
103 */
104 public static function delete() {
105 check_ajax_referer( 'bookit_delete_item', 'nonce' );
106
107 if ( ! current_user_can( 'manage_options' ) ) {
108 return false;
109 }
110
111 $data = CleanHelper::cleanData( $_GET, self::getCleanRules() );
112
113 if ( isset( $data['id'] ) ) {
114 $id = $data['id'];
115
116 Categories::deleteCategory( $id );
117
118 do_action( 'bookit_category_deleted', $id );
119
120 wp_send_json_success( array( 'message' => __( 'Category Deleted!', 'bookit' ) ) );
121 }
122
123 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
124 }
125 }
126