PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.57
Timetics – Appointment Booking Calendar & Scheduling v1.0.57
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / vendor / arraytics / tools-sdk / src / PluginManager.php

PluginManager.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.57, at vendor/arraytics/tools-sdk/src/PluginManager.php

169 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Extentions class
4 *
5 * @package Arraytics\ToolsSdk
6 */
7 namespace Arraytics\ToolsSdk;
8
9 /**
10 * Plugin manager
11 */
12 class PluginManager {
13
14 /**
15 * Check if a plugin is installed.
16 *
17 * @param string $slug The slug of the plugin.
18 * @return bool True if installed, false otherwise.
19 */
20 public static function is_installed( $slug ) {
21 // Ensure the get_plugins function is available
22 if ( ! function_exists( 'get_plugins' ) ) {
23 require_once ABSPATH . 'wp-admin/includes/plugin.php';
24 }
25
26 $plugins = get_plugins();
27
28 if ( is_array( $plugins ) ) {
29 foreach( $plugins as $plugin ) {
30 if ( $plugin['TextDomain'] === $slug ) {
31 return true;
32 }
33 }
34 }
35
36 return false;
37 }
38
39 /**
40 * Check if a plugin is active.
41 *
42 * @param string $slug The slug of the plugin.
43 * @return bool True if activated, false otherwise.
44 */
45 public static function is_activated( $slug ) {
46 if ( ! self::is_installed( $slug ) ) {
47 return false;
48 }
49
50 return is_plugin_active( self::get_plugin_path( $slug ) );
51 }
52
53 /**
54 * Install a plugin from the WordPress repository by slug.
55 *
56 * @param string $slug The slug of the plugin.
57 * @return bool True if installation succeeds, false otherwise.
58 */
59 public static function install_plugin( $slug ) {
60 if ( self::is_installed( $slug ) ) {
61 return true; // Already installed
62 }
63
64 include_once ABSPATH . 'wp-admin/includes/file.php';
65 include_once ABSPATH . 'wp-admin/includes/misc.php';
66 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
67
68 $skin = new \Automatic_Upgrader_Skin();
69 $upgrader = new \Plugin_Upgrader($skin);
70 $result = $upgrader->install('https://downloads.wordpress.org/plugin/' . $slug . '.latest-stable.zip');
71
72 return $result ? true : false;
73 }
74
75 /**
76 * Activate a plugin by slug.
77 *
78 * @param string $slug The slug of the plugin.
79 * @return bool True if activated, false otherwise.
80 */
81 public static function activate_plugin( $slug ) {
82 if ( ! self::is_installed( $slug ) ) {
83 return false; // Plugin not installed
84 }
85
86 $plugin_path = self::get_plugin_path( $slug );
87 $result = activate_plugin( $plugin_path );
88
89 if ( is_wp_error( $result ) ) {
90 return $result;
91 }
92
93 return true;
94 }
95
96 /**
97 * Deactivate a plugin by slug.
98 *
99 * @param string $slug The slug of the plugin.
100 * @return bool True if deactivated, false otherwise.
101 */
102 public static function deactivate_plugin( $slug ) {
103 if ( ! self::is_activated( $slug ) ) {
104 return false; // Plugin not activated
105 }
106
107 $plugin_path = self::get_plugin_path( $slug );
108
109 deactivate_plugins( $plugin_path );
110
111 return true;
112 }
113
114 /**
115 * Get the plugin path by slug.
116 *
117 * @param string $slug The slug of the plugin.
118 * @return string The path to the plugin file.
119 */
120 private static function get_plugin_path( $slug ) {
121 $plugins = get_plugins();
122
123 if ( is_array( $plugins ) ) {
124 foreach( $plugins as $plugin_path => $plugin ) {
125 if ( $plugin['TextDomain'] === $slug ) {
126 return $plugin_path;
127 }
128 }
129 }
130
131 return false;
132 }
133
134 /**
135 * Check if the provided file is the main plugin file.
136 *
137 * @param string $file Path to the file.
138 * @return bool True if it is the main plugin file, false otherwise.
139 */
140 private static function is_main_plugin_file( $file ) {
141
142 $plugin_data = get_file_data( $file, ['Name' => 'Plugin Name'] );
143
144 return ! empty( $plugin_data['Name'] );
145 }
146
147 /**
148 * Get plugin name by slug.
149 *
150 * @param string $slug Plugin slug (e.g., 'woocommerce).
151 * @return string|null Returns plugin name if found, or null if not found.
152 */
153 public static function get_plugin_name_by_slug( $slug ) {
154 if ( ! function_exists( 'get_plugins' ) ) {
155 require_once ABSPATH . 'wp-admin/includes/plugin.php';
156 }
157
158 $all_plugins = get_plugins();
159
160 foreach ( $all_plugins as $plugin_file => $plugin_data ) {
161 if ( strpos( $plugin_file, $slug ) !== false ) {
162 return $plugin_data['Name'];
163 }
164 }
165
166 return null;
167 }
168 }
169