Plugin Directory

source: filebird/trunk/includes/Classes/Convert.php

Last change on this file was 3411587, checked in by ninjateam, 4 months ago

Version 6.5.2

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1<?php
2namespace FileBird\Classes;
3
4use FileBird\Controller\Convert as ConvertController;
5use FileBird\Model\Folder as FolderModel;
6use FileBird\Controller\Import\DataImport;
7
8defined( 'ABSPATH' ) || exit;
9
10class Convert {
11
12        protected static $instance = null;
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        public function __construct() {
22        }
23
24        private function doHooks() {
25                add_action( 'rest_api_init', array( $this, 'registerRestFields' ) );
26        }
27
28        public function registerRestFields() {
29                //get old data
30                register_rest_route(
31                        NJFB_REST_URL,
32                        'fb-get-old-data',
33                        array(
34                                'methods'             => 'POST',
35                                'callback'            => array( $this, 'ajaxGetOldData' ),
36                                'permission_callback' => array( $this, 'resPermissionsCheck' ),
37                        )
38                );
39                //insert old data
40                register_rest_route(
41                        NJFB_REST_URL,
42                        'fb-insert-old-data',
43                        array(
44                                'methods'             => 'POST',
45                                'callback'            => array( $this, 'ajaxInsertOldData' ),
46                                'permission_callback' => array( $this, 'resPermissionsCheck' ),
47                        )
48                );
49                //wipe old data
50                register_rest_route(
51                        NJFB_REST_URL,
52                        'fb-wipe-old-data',
53                        array(
54                                'methods'             => 'POST',
55                                'callback'            => array( $this, 'ajaxWipeOldData' ),
56                                'permission_callback' => array( $this, 'resPermissionsCheck' ),
57                        )
58                );
59                //wipe old data
60                register_rest_route(
61                        NJFB_REST_URL,
62                        'fb-wipe-clear-all-data',
63                        array(
64                                'methods'             => 'POST',
65                                'callback'            => array( $this, 'ajaxClearAllData' ),
66                                'permission_callback' => array( $this, 'resPermissionsCheck' ),
67                        )
68                );
69                register_rest_route(
70                        NJFB_REST_URL,
71                        'fb-no-thanks',
72                        array(
73                                'methods'             => 'POST',
74                                'callback'            => array( $this, 'ajaxNoThanks' ),
75                                'permission_callback' => array( $this, 'resPermissionsCheck' ),
76                        )
77                );
78        }
79        public function resPermissionsCheck() {
80                return current_user_can( 'upload_files' );
81        }
82
83        public function ajaxGetOldData() {
84                $folders       = ConvertController::getOldFolders();
85                $folders_chunk = array_chunk( $folders, 20 );
86                wp_send_json_success(
87                        array(
88                                'folders' => $folders_chunk,
89                        )
90                );
91        }
92        public function ajaxInsertOldData( $request ) {
93                if( !current_user_can( 'manage_options' ) ) {
94                        wp_send_json_error(
95                                array(
96                                        'mess' => __( 'You are not authorized to insert old data.', 'filebird' ),
97                                )
98                        );
99                }
100                $folders = isset( $request ) ? $request->get_params()['folders'] : '';
101                if ( $folders != '' ) {
102                        ConvertController::insertToNewTable( $folders );
103                        update_option( 'fbv_old_data_updated_to_v4', '1' );
104                        wp_send_json_success( array( 'mess' => __( 'success', 'filebird' ) ) );
105                } else {
106                        wp_send_json_error( array( 'mess' => __( 'validation failed', 'filebird' ) ) );
107                }
108        }
109
110        public function ajaxWipeOldData() {
111                global $wpdb;
112                if( !current_user_can( 'manage_options' ) ) {
113                        wp_send_json_error(
114                                array(
115                                        'mess' => __( 'You are not authorized to clear all data.', 'filebird' ),
116                                )
117                        );
118                }
119                $queries = array(
120                        'DELETE FROM ' . $wpdb->prefix . 'termmeta WHERE `term_id` IN (SELECT `term_id` FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s)',
121                        'DELETE FROM ' . $wpdb->prefix . 'term_relationships WHERE `term_taxonomy_id` IN (SELECT `term_taxonomy_id` FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s)',
122                        'DELETE FROM ' . $wpdb->prefix . 'terms WHERE `term_id` IN (SELECT `term_id` FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s)',
123                        'DELETE FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s',
124                );
125                foreach ( $queries as $k => $query ) {
126                        // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
127                        $wpdb->query( $wpdb->prepare( $query, 'nt_wmc_folder' ) );
128                }
129                wp_send_json_success(
130                        array(
131                                'mess' => __( 'Successfully wiped.', 'filebird' ),
132                        )
133                );
134        }
135        public function ajaxClearAllData() {
136                global $wpdb;
137                if( !current_user_can( 'manage_options' ) ) {
138                        wp_send_json_error(
139                                array(
140                                        'mess' => __( 'You are not authorized to clear all data.', 'filebird' ),
141                                )
142                        );
143                }
144                $table_name = $wpdb->prefix . 'fbv';
145                if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) ) == $table_name ) {
146                        FolderModel::deleteAll();
147
148                        foreach ( DataImport::get() as $data ) {
149                                update_option( "njt_fb_updated_from_{$data->prefix}", '0' );
150                        }
151
152                        wp_send_json_success(
153                                array(
154                                        'mess' => __( 'Successfully cleared!', 'filebird' ),
155                                )
156                        );
157                } else {
158                        wp_send_json_error(
159                                array(
160                                        'mess' => __( 'Please try again.', 'filebird' ),
161                                )
162                        );
163                }
164
165        }
166
167        public function ajaxNoThanks( $request ) {
168                $site = $request->get_param( 'site' );
169
170                $site = isset( $site ) ? sanitize_text_field( $site ) : '';
171
172                if ( $site === 'all' ) {
173                        foreach ( DataImport::get() as $data ) {
174                                update_option( "njt_fb_{$data->prefix}_no_thanks", '1' );
175                        }
176                } else {
177                        update_option( "njt_fb_{$site}_no_thanks", '1' );
178                }
179
180                return new \WP_REST_Response(
181                        array(
182                                'mess' => __( 'Success', 'filebird' ),
183                        )
184                );
185        }
186}
Note: See TracBrowser for help on using the repository browser.