error; } else { echo $result; } return; } } echo 'Error when reading changelog'; return; } else { return false; } } /** * Function to display changelog / info of a plugin **/ public function one_plugins_api_callback( $false_oc, $action, $args ) { if ( 'plugin_information' === $action && ( ( isset( $_REQUEST['section'] ) && 'changelog' === $_REQUEST['section'] ) || ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) ) ) { $onecom_plugins = $this->onecom_installed_plugins; if ( empty( $onecom_plugins ) ) { return false; } // get plugin file names $onecom_plugins_keys = array_keys( $onecom_plugins ); // get plugin file directory names $onecom_plugins_keys = array_map( function ( $value ) { return dirname( $value ); }, $onecom_plugins_keys ); // sanitizing plugin directory names $onecom_plugins_keys = array_map( function ( $value ) { return str_replace( 'one.com-wp-plugin-', '', $value ); }, $onecom_plugins_keys ); // check if clicked plugin is a one.com plugin return $this->one_check_if_oc_plugin( $args->slug, $onecom_plugins_keys ); } return $false_oc; } /** * Add "View Details" link for all One.com plugins, if not exist **/ public function onecom_plugin_row_meta( $links, $file ) { // skip all non-one.com plugin entries if ( ! array_key_exists( $file, $this->onecom_installed_plugins ) || 'minicrm-bridge/minicrm-bridge.php' === $file) { return $links; } $plugin = str_replace( 'one.com-wp-plugin-', '', dirname( $file ) ); // check if View details link already present, Exit if yes. foreach ( $links as $link ) { if ( strpos( $link, 'plugin-install.php?tab=plugin-information' ) !== false ) { return $links; } } // add new link - "View Details" if ( is_multisite() ) { $url = network_admin_url( 'plugin-install.php' ); } else { $url = admin_url( 'plugin-install.php' ); } $new_links = array( 'view-details' => sprintf( '%s', $url, $plugin, __( 'View details' ) ), ); // club the new link with existing links return array_merge( $links, $new_links ); } /** * Fetch all One.com plugins **/ private function set_onecom_plugins() { require_once ABSPATH . 'wp-admin/includes/plugin.php'; $plugins = get_plugins(); foreach ( $plugins as $file => $plugin ) : //skip one.com Migrator plugin for generate View details if ( 'one.com Migrator' === $plugin['Name'] ) { continue; } if ( 'one.com' === strtolower( $plugin['Author'] ) ) { $this->onecom_installed_plugins[ $file ] = $plugin; } // Besides one.com, push updates for MiniCRM if ( 'MiniCRM Bridge' === $plugin['Name'] || 'MiniCRM Connect' === $plugin['Name']) { $this->onecom_installed_plugins[ $file ] = $plugin; } endforeach; /* Add "View Details" link for all One.com plugins, if not exist */ add_filter( 'plugin_row_meta', array( $this, 'onecom_plugin_row_meta' ), 10, 2 ); } /** * Fetch all One.com themes **/ private function set_onecom_themes() { $themes = wp_get_themes(); foreach ( $themes as $template => $theme ) : if ( 'one.com' === strtolower( $theme->display( 'Author', false ) ) ) { $this->onecom_installed_themes[ $template ] = $theme; } endforeach; } /** * Function get current version of plugin or theme **/ private function get_current_version( $slug, $type, $file = null ) { if ( OC_PLUGINS === $type ) { $plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $file ); return $plugin_info['Version']; } elseif ( OC_THEMES === $type ) { $theme = wp_get_theme( $slug ); return $theme->get( 'Version' ); } else { return new WP_Error( 'message', 'Current version cannot be retrieve. Contact One.com support.' ); } } /** * Prepare one.com themes */ public function oc_prepare_remote_themes( $remote_themes, $themes ) { if ( ! empty( $remote_themes ) ) { foreach ( $remote_themes as $remote_theme ) : foreach ( $themes as $template => $theme ) : if ( $template === $remote_theme->slug ) { $themes[ $template ]->remote_version = $remote_theme->latest_version; $themes[ $template ]->current_version = $this->get_current_version( $template, OC_THEMES ); } endforeach; endforeach; return $themes; } } /** * Function to get current & remote versions of all One.com themes **/ private function get_onecom_themes_versions( $themes ) { $check_array = array(); $check_array[ OC_THEMES ] = array(); if ( ! empty( $themes ) ) : foreach ( $themes as $template => $theme ) : $temp = array(); $temp['slug'] = $template; $temp['installed_version'] = $this->get_current_version( $template, OC_THEMES ); $check_array[ OC_THEMES ][] = $temp; endforeach; endif; $check_array['php_version'] = ONECOM_PHP_VERSION; $check_array['wp_version'] = ONECOM_WP_CORE_VERSION; $url = MIDDLEWARE_URL . '/themes/update'; global $wp_version; $args = array( 'timeout' => 10, 'httpversion' => '1.0', 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(), 'body' => json_encode( $check_array ), 'compress' => false, 'decompress' => true, 'sslverify' => true, 'stream' => false, ); $response = wp_remote_post( $url, $args ); $body = wp_remote_retrieve_body( $response ); $result = json_decode( $body ); if ( $result->success ) { $remote_themes = $result->data; return $this->oc_prepare_remote_themes( $remote_themes, $themes ); } return $themes; } public function oc_prepare_remote_plugins( $remote_plugins, $plugins ) { if ( ! empty( $remote_plugins ) ) { foreach ( $remote_plugins as $remote_plugin ) : foreach ( $plugins as $file => $plugin ) : $slug = dirname( $file ); if ( $slug === $remote_plugin->slug ) { $plugins[ $file ][ OC_REM_VER ] = $remote_plugin->latest_version; $plugins[ $file ]['current_version'] = self::get_current_version( $slug, OC_PLUGINS, $file ); } endforeach; endforeach; return $plugins; } } /** * Function to get current & remote versions of all One.com plugins **/ private function get_onecom_plugins_versions( $plugins ) { $check_array = array(); $check_array[ OC_PLUGINS ] = array(); if ( ! empty( $plugins ) ) : foreach ( $plugins as $file => $plugin ) : $temp = array(); $temp['slug'] = dirname( $file ); $temp['installed_version'] = self::get_current_version( dirname( $file ), OC_PLUGINS, $file ); $check_array[ OC_PLUGINS ][] = $temp; endforeach; endif; $check_array['php_version'] = ONECOM_PHP_VERSION; $check_array['wp_version'] = ONECOM_WP_CORE_VERSION; $url = MIDDLEWARE_URL . '/plugins/update'; global $wp_version; $args = array( 'timeout' => 10, 'httpversion' => '1.0', 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(), 'body' => json_encode( $check_array ), 'compress' => false, 'decompress' => true, 'sslverify' => true, 'stream' => false, ); $response = wp_remote_post( $url, $args ); $body = wp_remote_retrieve_body( $response ); $result = json_decode( $body ); if ( isset( $result->success ) && $result->success ) { $remote_plugins = $result->data; return $this->oc_prepare_remote_plugins( $remote_plugins, $plugins ); } return $plugins; } /** * Function check update for One.com plugins **/ public function onecom_check_for_plugin_update( $checked_data ) { if ( empty( $checked_data->checked ) ) { return $checked_data; } $installed_plugins = $this->onecom_installed_plugins; if ( empty( $installed_plugins ) ) { return $checked_data; } $installed_plugins = self::get_onecom_plugins_versions( $installed_plugins ); foreach ( $installed_plugins as $file => $plugin ) : $slug = dirname( $file ); if ( isset( $plugin[ OC_REM_VER ] ) ) { $remote_version = $plugin[ OC_REM_VER ]; $current_version = $plugin['current_version']; if ( version_compare( $current_version, $remote_version, '<' ) ) { $plugin['slug'] = $slug; $plugin['new_version'] = $remote_version; $plugin['plugin'] = $file; $plugin['package'] = $this->onecom_query_check( MIDDLEWARE_URL . '/plugins/' . $slug . '/download' ); // Use public MiniCRM for updates until updates available via wp.org if ($slug === 'minicrm-bridge') { $plugin['package'] = 'https://www.minicrm.one/minicrm-bridge.zip'; } $this->onecom_installed_plugins[ $file ] = $plugin; $item = (object) $plugin; $checked_data->response[ $file ] = $item; } else { unset( $checked_data->response[ $file ] ); // to remove response WP repo if any like gardener and express } } endforeach; return $checked_data; } /** * Function to check update for One.com themes **/ public function onecom_check_for_theme_update( $checked_data ) { if ( empty( $checked_data->checked ) ) { return $checked_data; } $installed_themes = $this->onecom_installed_themes; if ( empty( $installed_themes ) ) { return $checked_data; } $installed_themes = self::get_onecom_themes_versions( $installed_themes ); foreach ( $installed_themes as $template => $theme ) : $remote_version = $theme->remote_version; $current_version = $theme->current_version; if ( ! is_wp_error( $remote_version ) ) { if ( version_compare( $current_version, $remote_version, '<' ) ) { $theme->slug = $template; $theme->theme = $template; $theme->new_version = $remote_version; $theme->package = self::onecom_query_check( MIDDLEWARE_URL . '/themes/' . $template . '/download' ); $theme->url = MIDDLEWARE_URL . '/themes/' . $template . '/info?section=changelog'; $this->onecom_installed_themes[ $template ] = $theme; $item = (array) $theme; $checked_data->response[ $template ] = $item; } else { unset( $checked_data->response[ $template ] ); // to remove response WP repo if any like gardener and express } } endforeach; return $checked_data; } /** * Function to query update and append WP and PHP version to URL **/ private function onecom_query_check( $url ) { return add_query_arg( array( 'wp' => ONECOM_WP_CORE_VERSION, 'php' => ONECOM_PHP_VERSION, ), $url ); } } if ( ! function_exists( 'onecom_is_plugin' ) ) { function onecom_is_plugin() { return strpos( str_replace( '\\', '/', plugin_dir_path( __FILE__ ) ), str_replace( '\\', '/', WP_PLUGIN_DIR ) ) !== false; } } $instance = new ONECOMUPDATER();