diff options
Diffstat (limited to 'plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php')
-rw-r--r-- | plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php | 178 |
1 files changed, 128 insertions, 50 deletions
diff --git a/plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php b/plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php index 04e56550..daa9eab3 100644 --- a/plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php +++ b/plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php @@ -32,6 +32,25 @@ abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoi 'action_links' => '(array) An array of action links that the plugin uses.', ); + static $_response_format_v1_2 = array( + 'slug' => '(safehtml) The plugin\'s .org slug', + 'active' => '(boolean) The plugin status.', + 'update' => '(object) The plugin update info.', + 'name' => '(safehtml) The plugin\'s ID', + 'display_name' => '(safehtml) The name of the plugin.', + 'version' => '(safehtml) The plugin version number.', + 'description' => '(safehtml) Description of what the plugin does and/or notes from the author', + 'author' => '(safehtml) The author\'s name', + 'author_url' => '(url) The authors web site address', + 'plugin_url' => '(url) Link to the plugin\'s web site.', + 'network' => '(boolean) Whether the plugin can only be activated network wide.', + 'autoupdate' => '(boolean) Whether the plugin is automatically updated', + 'autoupdate_translation' => '(boolean) Whether the plugin is automatically updating translations', + 'uninstallable' => '(boolean) Whether the plugin is unistallable.', + 'action_links' => '(array) An array of action links that the plugin uses.', + 'log' => '(array:safehtml) An array of update log strings.', + ); + protected function result() { $plugins = $this->get_plugins(); @@ -101,6 +120,9 @@ abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoi } protected function format_plugin( $plugin_file, $plugin_data ) { + if ( version_compare( $this->min_version, '1.2', '>=' ) ) { + return $this->format_plugin_v1_2( $plugin_file, $plugin_data ); + } $plugin = array(); $plugin['id'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); $plugin['slug'] = Jetpack_Autoupdate::get_plugin_slug( $plugin_file ); @@ -130,11 +152,90 @@ abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoi return $plugin; } + protected function format_plugin_v1_2( $plugin_file, $plugin_data ) { + $plugin = array(); + $plugin['slug'] = Jetpack_Autoupdate::get_plugin_slug( $plugin_file ); + $plugin['active'] = Jetpack::is_plugin_active( $plugin_file ); + $plugin['name'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); + $plugin['display_name'] = $plugin_data['Name']; + $plugin['plugin_url'] = $plugin_data['PluginURI']; + $plugin['version'] = $plugin_data['Version']; + $plugin['description'] = $plugin_data['Description']; + $plugin['author'] = $plugin_data['Author']; + $plugin['author_url'] = $plugin_data['AuthorURI']; + $plugin['network'] = $plugin_data['Network']; + $plugin['update'] = $this->get_plugin_updates( $plugin_file ); + $plugin['action_links'] = $this->get_plugin_action_links( $plugin_file ); + + $autoupdate = $this->plugin_has_autoupdates_enabled( $plugin_file ); + $plugin['autoupdate'] = $autoupdate; + + $autoupdate_translation = $this->plugin_has_translations_autoupdates_enabled( $plugin_file ); + $plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); + $plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); + + if ( ! empty ( $this->log[ $plugin_file ] ) ) { + $plugin['log'] = $this->log[ $plugin_file ]; + } + + return $plugin; + } + + protected function plugin_has_autoupdates_enabled( $plugin_file ) { + return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); + } + + protected function plugin_has_translations_autoupdates_enabled( $plugin_file ) { + return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) ); + } + + + protected function get_file_mod_capabilities() { + $reasons_can_not_autoupdate = array(); + $reasons_can_not_modify_files = array(); + + $has_file_system_write_access = Jetpack_Sync_Functions::file_system_write_access(); + if ( ! $has_file_system_write_access ) { + $reasons_can_not_modify_files['has_no_file_system_write_access'] = __( 'The file permissions on this host prevent editing files.', 'jetpack' ); + } + + $disallow_file_mods = Jetpack_Constants::get_constant('DISALLOW_FILE_MODS' ); + if ( $disallow_file_mods ) { + $reasons_can_not_modify_files['disallow_file_mods'] = __( 'File modifications are explicitly disabled by a site administrator.', 'jetpack' ); + } + + $automatic_updater_disabled = Jetpack_Constants::get_constant( 'AUTOMATIC_UPDATER_DISABLED' ); + if ( $automatic_updater_disabled ) { + $reasons_can_not_autoupdate['automatic_updater_disabled'] = __( 'Any autoupdates are explicitly disabled by a site administrator.', 'jetpack' ); + } + + if ( is_multisite() ) { + // is it the main network ? is really is multi network + if ( Jetpack::is_multi_network() ) { + $reasons_can_not_modify_files['is_multi_network'] = __( 'Multi network install are not supported.', 'jetpack' ); + } + // Is the site the main site here. + if ( ! is_main_site() ) { + $reasons_can_not_modify_files['is_sub_site'] = __( 'The site is not the main network site', 'jetpack' ); + } + } + + $file_mod_capabilities = array( + 'modify_files' => (bool) empty( $reasons_can_not_modify_files ), // install, remove, update + 'autoupdate_files' => (bool) empty( $reasons_can_not_modify_files ) && empty( $reasons_can_not_autoupdate ), // enable autoupdates + ); + + if ( ! empty( $reasons_can_not_modify_files ) ) { + $file_mod_capabilities['reasons_modify_files_unavailable'] = $reasons_can_not_modify_files; + } + + if ( ! $file_mod_capabilities['autoupdate_files'] ) { + $file_mod_capabilities['reasons_autoupdate_unavailable'] = array_merge( $reasons_can_not_autoupdate, $reasons_can_not_modify_files ); + } + return $file_mod_capabilities; + } + protected function get_plugins() { - // Do the admin_init action in order to capture plugin action links. - // See get_plugin_action_links() - /** This action is documented in wp-admin/admin.php */ - do_action( 'admin_init' ); $plugins = array(); /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ $installed_plugins = apply_filters( 'all_plugins', get_plugins() ); @@ -184,54 +285,31 @@ abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoi protected function get_plugin_updates( $plugin_file ) { $plugin_updates = get_plugin_updates(); - if ( isset( $plugin_updates[ $plugin_file ] ) ){ - return $plugin_updates[ $plugin_file ]->update; + if ( isset( $plugin_updates[ $plugin_file ] ) ) { + $update = $plugin_updates[ $plugin_file ]->update; + $cleaned_update = array(); + foreach( (array) $update as $update_key => $update_value ) { + switch ( $update_key ) { + case 'id': + case 'slug': + case 'plugin': + case 'new_version': + case 'tested': + $cleaned_update[ $update_key ] = wp_kses( $update_value, array() ); + break; + case 'url': + case 'package': + $cleaned_update[ $update_key ] = esc_url( $update_value ); + break; + } + } + return (object) $cleaned_update; } return null; } - /** - * Get custom action link tags that the plugin is using - * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) - * @return array of plugin action links (key: link name value: url) - */ - protected function get_plugin_action_links( $plugin_file ) { - $formatted_action_links = array(); - - // Some sites may have DOM disabled in PHP - if ( ! class_exists( 'DOMDocument' ) ) { - return $formatted_action_links; - } - - $action_links = array(); - /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ - $action_links = apply_filters( 'plugin_action_links', $action_links, $plugin_file, null, 'all' ); - /** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */ - $action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, null, 'all' ); - if ( count( $action_links ) > 0 ) { - $dom_doc = new DOMDocument; - foreach( $action_links as $action_link ) { - $dom_doc->loadHTML( mb_convert_encoding( $action_link, 'HTML-ENTITIES', 'UTF-8' ) ); - $link_elements = $dom_doc->getElementsByTagName( 'a' ); - if ( $link_elements->length == 0 ) { - continue; - } - - $link_element = $link_elements->item( 0 ); - if ( $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) { - $link_url = trim( $link_element->getAttribute( 'href' ) ); - - // Add the full admin path to the url if the plugin did not provide it - $link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME ); - if ( empty( $link_url_scheme ) ) { - $link_url = admin_url( $link_url ); - } - - $formatted_action_links[ $link_element->nodeValue ] = $link_url; - } - } - } - - return $formatted_action_links; - } + protected function get_plugin_action_links( $plugin_file ) { + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; + return Jetpack_Sync_Functions::get_plugins_action_links( $plugin_file ); + } } |