Plugin Directory

source: mp-timetable/trunk/classes/class-offer.php

Last change on this file was 3034789, checked in by jetmonsters, 2 years ago

Version: 2.4.10

File size: 8.2 KB
Line 
1<?php
2
3namespace mp_timetable\plugin_core\classes;
4
5class Plugins_Offer {
6
7        public function __construct() { }
8
9        public static function activatePluginAjax() {
10
11        check_ajax_referer( 'mptt-install-plugins', 'nonce' );
12
13        $error = esc_html__( 'Could not activate the plugin.', 'mp-timetable' );
14
15        if ( ! current_user_can( 'activate_plugins' ) ) {
16
17            wp_send_json_error( $error );
18        }
19
20        if ( isset( $_POST[ 'plugin' ] ) ) {
21
22            $plugin   = sanitize_text_field( wp_unslash( $_POST['plugin'] ) );
23                        $activate = activate_plugins( $plugin );
24
25            if ( ! is_wp_error( $activate ) ) {
26                    wp_send_json_success(
27                    [
28                        'is_activated' => true
29                    ]
30                );
31            }
32        }
33
34            wp_send_json_error( $error );
35        }
36
37        public static function installPluginAjax() {
38
39        check_ajax_referer( 'mptt-install-plugins', 'nonce' );
40
41                $plugin = sanitize_text_field( wp_unslash( $_POST['plugin'] ) );
42        $slug = strtok( $plugin, '/' );
43
44        if ( empty( $_POST[ 'plugin' ] ) ) {
45            wp_send_json_error( esc_html__( 'Could not install the plugin.', 'mp-timetable' ) );
46        }
47
48        if ( ! current_user_can( 'install_plugins' ) ) {
49            wp_send_json_error( esc_html__( 'Sorry, you are not allowed to install plugins on this site.', 'mp-timetable' ) );
50        }
51
52        require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
53        include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
54
55        $api = plugins_api(
56            'plugin_information',
57            array(
58                'slug'   => sanitize_key( $slug ),
59                'fields' => array(
60                    'sections' => false,
61                ),
62            )
63        );
64
65        if ( is_wp_error( $api ) ) {
66
67            wp_send_json_error( $api->get_error_message() );
68        }
69
70        $skin     = new \WP_Ajax_Upgrader_Skin();
71        $upgrader = new \Plugin_Upgrader( $skin );
72        $result   = $upgrader->install( $api->download_link );
73
74        wp_cache_flush();
75
76        if ( is_wp_error( $result ) ) {
77
78            wp_send_json_error( $result->get_error_message() );
79        } elseif ( is_wp_error( $skin->result ) ) {
80
81            wp_send_json_error( $skin->result->get_error_message() );
82        } elseif ( $skin->get_errors()->has_errors() ) {
83
84            wp_send_json_error( $skin->get_error_messages() );
85        } elseif ( is_null( $result ) ) {
86
87            global $wp_filesystem;
88
89            $error = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.' );
90
91            if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
92                $error = esc_html__( $wp_filesystem->errors->get_error_message() );
93            }
94
95            wp_send_json_error( $error );
96        }
97
98        $install_status = install_plugin_install_status( $api );
99
100        if ( is_plugin_inactive( $install_status[ 'file' ] ) ) {
101             wp_send_json_success(
102                [
103                    'is_activated' => false
104                ]
105            );
106        }
107
108        wp_send_json_error( esc_html__( 'Could not install the plugin.', 'mp-timetable' ) );
109        }
110
111    private function getPluginLists() {
112
113        $plugins  = array(
114            'motopress-appointment' => array(
115                'slug' => 'motopress-appointment-lite',
116                'name' => 'Hourly Appointment Booking',
117                'path' => 'motopress-appointment-lite/motopress-appointment.php',
118                'icon' => 'https://ps.w.org/motopress-appointment-lite/assets/icon.svg',
119                'description' => 'Take automatic online appointment reservations through your website for events, classes, and any other type of services.'
120            ),
121            'getwid' => array(
122                'slug' => 'getwid',
123                'name' => 'Getwid: 40+ Free Gutenberg Blocks',
124                'path' => 'getwid/getwid.php',
125                'icon' => 'https://ps.w.org/getwid/assets/icon.svg',
126                'description' => 'Getwid is a collection of 40+ Gutenberg blocks that greatly extends the library of existing core WordPress blocks and 35+ unique pre-made block templates for the Block Editor.'
127            ),
128            'stratum' => array(
129                'slug' => 'stratum',
130                'name' => 'Stratum: 20+ Free Elementor Widgets',
131                'path' => 'stratum/stratum.php',
132                'icon' => 'https://ps.w.org/stratum/assets/icon.svg',
133                'description' => 'Stratum is a free collection of 20+ Elementor addons with the aim of enhancing the existing widget functionality of your favorite page builder.'
134            ),
135            'hotel-booking' => array(
136                'slug' => 'motopress-hotel-booking-lite',
137                'name' => 'Hotel Booking: WordPress Booking Plugin',
138                'path' => 'motopress-hotel-booking-lite/motopress-hotel-booking.php',
139                'icon' => 'https://ps.w.org/motopress-hotel-booking-lite/assets/icon-128x128.png',
140                'description' => 'Hotel Booking plugin by MotoPress is the ultimate WordPress property rental system with a real lodging business in mind.'
141            )
142        );
143
144        return $plugins;
145    }
146
147        private function getPluginInstallationLink( $slug ) {
148
149                $action = 'install-plugin';
150
151                return wp_nonce_url(
152                        add_query_arg(
153                                array(
154                                        'action' => $action,
155                                        'plugin' => $slug
156                                ),
157                                admin_url( 'update.php' )
158                        ),
159                        $action.'_'.$slug
160                );
161        }
162
163        // check status
164        private function getPluginData( $plugin ) {
165
166                if ( array_key_exists( $plugin['path'], get_plugins() ) ) {
167
168                        if ( is_plugin_active( $plugin['path'] ) ) {
169                                $plugin['status_text'] = esc_html__( 'Active', 'mp-timetable' );
170                                $plugin['status_class'] = 'active';
171                                $plugin['action_class'] = 'button button-secondary disabled';
172                                $plugin['action_text'] = esc_html__( 'Activated', 'mp-timetable' );
173                        } else {
174                                $plugin['status_text'] = esc_html__( 'Inactive', 'mp-timetable' );
175                                $plugin['status_class'] = 'inactive';
176                                $plugin['action_class'] = 'button button-secondary';
177                                $plugin['action_text'] = esc_html__( 'Activate', 'mp-timetable' );
178                        }
179                } else {
180                        $plugin['status_text'] = esc_html__( 'Not Installed', 'mp-timetable' );
181                        $plugin['status_class'] = 'not-installed';
182                        $plugin['action_class'] = 'button button-primary';
183                        $plugin['action_text'] = esc_html__( 'Install Plugin', 'mp-timetable' );
184                }
185
186                return $plugin;
187        }
188
189        public function render() {
190        ?>
191            <div class="motopress-offer-secondary">
192
193                <h2>More free plugins for you</h2>
194                <?php
195                    foreach ( $this->getPluginLists() as $key => $plugin ) :
196
197                    $plugin = $this->getPluginData( $plugin );
198                ?>
199                    <div class="plugin-container">
200                        <div class="plugin-item">
201                            <div class="details">
202                                <img src="<?php echo esc_url( $plugin['icon'] ); ?>">
203                                <h5 class="plugin-name"><?php echo esc_html( $plugin['name'] ); ?></h5>
204                                <p class="plugin-description"><?php echo esc_html( $plugin['description'] ); ?></p>
205                            </div>
206                            <div class="actions">
207                                <div class="status">
208                                    <strong>Status: <span class="status-label <?php echo esc_attr( $plugin['status_class'] ); ?>">
209                                        <?php echo esc_html( $plugin['status_text'] ); ?></span></strong>
210                                </div>
211                                <div class="action-button">
212                                    <button data-path="<?php echo esc_attr( $plugin[ 'path' ] ); ?>" class="<?php echo esc_attr( $plugin['action_class'] ); ?>">
213                                        <?php echo esc_html( $plugin['action_text'] ); ?>
214                                    </button>
215                                </div>
216                            </div>
217                        </div>
218                    </div>
219                <?php endforeach; ?>
220            </div>
221        <?php
222        }
223}
Note: See TracBrowser for help on using the repository browser.