PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.61
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.61
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / public / admin / class-contentembedinstaller.php

class-contentembedinstaller.php in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.61, at public/admin/class-contentembedinstaller.php

155 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Leadin\admin;
4
5 use Leadin\utils\QueryParameters;
6
7
8
9 /**
10 * Class used to install the Content embed plugin
11 */
12 class ContentEmbedInstaller {
13
14 const CONTENT_EMBED_LINK = 'https://api.hubapi.com/content-embed/v1/plugin/download/content-hub-embed.zip';
15 const INSTALL_ARG = 'contentembed_install';
16 const INSTALL_OPTION = LEADIN_PREFIX . '_content_embed_ui_install';
17
18 /**
19 * Class constructor, adds the necessary hooks.
20 */
21 public function __construct() {
22 if ( is_admin() ) {
23 add_action( 'wp_ajax_content_embed_install', array( $this, 'wp_ajax_install_content_embed' ) );
24 }
25 }
26
27
28 /**
29 * AJAX to install Content embed plugin by downloading from URL in Content embed API.
30 * Modified from: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-admin/includes/ajax-actions.php#L4444
31 */
32 public function wp_ajax_install_content_embed() {
33 $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( ( $_REQUEST['_wpnonce'] ) ) ) : '';
34
35 if ( ! current_user_can( 'install_plugins' ) || ! wp_verify_nonce( $nonce, self::INSTALL_ARG ) ) {
36 $status['errorCode'] = 'PERMISSIONS_ERROR';
37 $status['errorMessage'] = 'User does not have permission to install or activate plugins.';
38 return wp_send_json_error( $status, 403 );
39 }
40
41 $active_or_installed = self::is_content_embed_active_installed();
42 $activated = $active_or_installed['active'];
43 $installed = $active_or_installed['installed'];
44
45 if ( $activated || $installed ) {
46 $status['errorCode'] = $activated ? 'ALREADY_ACTIVATED' : 'ALREADY_INSTALLED';
47 $status['errorMessage'] = 'Content embed already installed or activated';
48 wp_send_json_error( $status );
49 }
50
51 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
52 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
53 require_once ABSPATH . 'wp-admin/includes/plugin.php';
54
55 $skin = new \WP_Ajax_Upgrader_Skin();
56 $upgrader = new \Plugin_Upgrader( $skin );
57 $result = $upgrader->install( self::CONTENT_EMBED_LINK );
58
59 if ( is_wp_error( $result ) ) {
60 $status['errorCode'] = 'GENERIC_ERROR';
61 $status['errorMessage'] = $result->get_error_message();
62 wp_send_json_error( $status );
63 }
64
65 if ( is_wp_error( $skin->result ) ) {
66 $status['errorCode'] = 'GENERIC_ERROR';
67 $status['errorMessage'] = $skin->result->get_error_message();
68 wp_send_json_error( $status );
69 }
70
71 if ( $skin->get_errors()->has_errors() ) {
72 $status['errorCode'] = 'GENERIC_ERROR';
73 $status['errorMessage'] = $skin->get_error_messages();
74 wp_send_json_error( $status );
75 }
76
77 if ( is_null( $result ) ) {
78 global $wp_filesystem;
79 $status['errorCode'] = 'FILESYSTEM_ERROR';
80 $status['errorMessage'] = 'Unable to connect to the filesystem. Please confirm your credentials.';
81
82 // Pass through the error from WP_Filesystem if one was raised.
83 if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
84 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
85 }
86 wp_send_json_error( $status );
87 }
88
89 $plugin_info = $upgrader->plugin_info();
90 if ( ! $plugin_info ) {
91 return wp_send_json_error(
92 array(
93 'errorCode' => 'INFO_FETCH_ERROR',
94 'errorMessage' => 'Plugin installation failed, could not retrieve plugin info',
95 ),
96 500
97 );
98 }
99
100 $status = array(
101 'message' => 'Plugin installed and activated successfully',
102 'plugin_info' => $plugin_info,
103 'activated' => false,
104 );
105
106 if ( current_user_can( 'activate_plugins' ) ) {
107 $activation_response = activate_plugin( $plugin_info );
108 if ( is_wp_error( $activation_response ) ) {
109 $status['errorCode'] = 'ACTIVATION_ERROR';
110 $status['errorMessage'] = $activation_response->get_error_message();
111 } else {
112 $status['activated'] = true;
113 }
114 }
115
116 update_option( self::INSTALL_OPTION, true );
117 return wp_send_json_success( $status );
118 }
119
120 /**
121 * Determine if there is a copy of Content embed installed, and if so, if it is active.
122 * Checks for path names with junk at the end to handle multiple installs
123 */
124 public static function is_content_embed_active_installed() {
125 $content_embed_regex = '/hubspot-content-embed((-.*)?)\/content-embed.php/';
126 if ( ! function_exists( 'get_plugins' ) ) {
127 require_once ABSPATH . 'wp-admin/includes/plugin.php';
128 }
129
130 // Filter based on regex in case there are multiple copies installed for some reason.
131 $all_plugins = array_keys( get_plugins() );
132 $content_embed_paths = array_filter(
133 $all_plugins,
134 function ( $plugin_path ) use ( $content_embed_regex ) {
135 return preg_match( $content_embed_regex, $plugin_path );
136 }
137 );
138
139 $installed = ! empty( $content_embed_paths );
140 $active = ! empty(
141 array_filter(
142 $content_embed_paths,
143 function ( $plugin_path ) {
144 return is_plugin_active( $plugin_path );
145 }
146 )
147 );
148
149 return array(
150 'installed' => $installed,
151 'active' => $active,
152 );
153 }
154 }
155