From 687cda22373cf71bb3a35f946df356fede08b1e8 Mon Sep 17 00:00:00 2001 From: Laurence Bahiirwa Date: Thu, 26 Dec 2019 11:11:16 +0300 Subject: [PATCH 1/8] Initial Idea --- cc-compat.php | 49 ++++++++++++++++++++++++++++++ woocommerce.php => woocommerce.txt | 0 2 files changed, 49 insertions(+) create mode 100644 cc-compat.php rename woocommerce.php => woocommerce.txt (100%) diff --git a/cc-compat.php b/cc-compat.php new file mode 100644 index 0000000..4418cc4 --- /dev/null +++ b/cc-compat.php @@ -0,0 +1,49 @@ + Date: Thu, 26 Dec 2019 11:41:22 +0300 Subject: [PATCH 2/8] Fix-user-finding-error --- cc-compat.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cc-compat.php b/cc-compat.php index 4418cc4..d6b3a24 100644 --- a/cc-compat.php +++ b/cc-compat.php @@ -15,6 +15,10 @@ $file_to_copy = 'woocommerce.txt'; $cc_compat_woo_folder = dirname( __FILE__ ); +if( ! function_exists( 'wp_get_current_user' ) ) { + require( ABSPATH . "wp-includes/pluggable.php") ; +} + // if user can install plugins, then create access. if ( current_user_can( 'install_plugins' ) ) { @@ -33,17 +37,13 @@ require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); // activate it - activate_plugins( array( 'woocommerce/woocommerce.php' ) ); + activate_plugins( 'woocommerce/woocommerce.php' ); // deactivate cc-compat-woo - deactivate_plugins( array( 'cc-compat-woo/cc-compat-woo.php' ) ); + deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); // delete cc-compat-woo unlink('cc-compat-woo/cc-compat-woo.php'); rmdir( $cc_compat_woo_folder ); -} - - - - +} \ No newline at end of file From 2e7326907b1592edaae1559ca4620d88cadc209e Mon Sep 17 00:00:00 2001 From: Laurence Bahiirwa Date: Thu, 26 Dec 2019 18:13:38 +0300 Subject: [PATCH 3/8] Proof of concept --- cc-compat.php | 52 ++++++++++++++++++++++++------------------------- woocommerce.txt | 43 +++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 36 deletions(-) diff --git a/cc-compat.php b/cc-compat.php index d6b3a24..0413808 100644 --- a/cc-compat.php +++ b/cc-compat.php @@ -11,39 +11,37 @@ * Contributors: SAThemba, ZigPress */ -$dir = "woocommerce"; -$file_to_copy = 'woocommerce.txt'; -$cc_compat_woo_folder = dirname( __FILE__ ); - if( ! function_exists( 'wp_get_current_user' ) ) { require( ABSPATH . "wp-includes/pluggable.php") ; } + +// Require because it is not always loaded. +require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + +global $wp_filesystem; +$dir = "woocommerce"; +$file_to_copy = 'woocommerce.txt'; +$cc_compat_woo_folder = dirname( __FILE__ ); + // if user can install plugins, then create access. if ( current_user_can( 'install_plugins' ) ) { - + // Make a folder called woocommerce - if( is_dir($dir) === false ) { - mkdir($dir); + if( $wp_filesystem->is_dir($dir) === false ) { + $wp_filesystem->mkdir($dir); + // copy woocommerce.txt into .php in woocommerce folder + $wp_filesystem->move( $dir . $file_to_copy, $dir . 'woocommerce.php'); + + // activate it + activate_plugins( 'woocommerce/woocommerce.php' ); + + // deactivate cc-compat-woo + deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); + + // delete cc-compat-woo + $wp_filesystem->delete('cc-compat-woo/cc-compat-woo.php'); + + $wp_filesystem->rmdir( $cc_compat_woo_folder ); } - - // copy woocommerce.txt to woocommerce folder - copy( $file_to_copy, $dir ); - - // Rename woocommerce.txt to woocommerce.php - rename( $dir . 'woocommerce.txt', $dir . 'woocommerce.php'); - - // Require because it is not always loaded. - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); - - // activate it - activate_plugins( 'woocommerce/woocommerce.php' ); - - // deactivate cc-compat-woo - deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); - - // delete cc-compat-woo - unlink('cc-compat-woo/cc-compat-woo.php'); - - rmdir( $cc_compat_woo_folder ); } \ No newline at end of file diff --git a/woocommerce.txt b/woocommerce.txt index d6630fb..8b98f3d 100644 --- a/woocommerce.txt +++ b/woocommerce.txt @@ -1,16 +1,41 @@ is_dir( WP_PLUGIN_DIR . 'woocommerce' ) ) { + // deactivate cc-compat-woo + deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); + + // delete cc-compat-woo + $wp_filesystem->rmdir( WP_PLUGIN_DIR . 'cc-compat-woo'); + } + +} + define( 'CCWOOADDONSCOMPAT_VERSION', '9999.0' ); // DO NOT change the version in the plugin header or the Earth will fall on you :P define( 'CCWOOADDONSCOMPAT__FILE__', __FILE__ ); From a0d6648a73a9f44264b9100ff7ec19380fdf5774 Mon Sep 17 00:00:00 2001 From: Laurence Bahiirwa Date: Thu, 26 Dec 2019 18:18:52 +0300 Subject: [PATCH 4/8] Fix: delete bug --- cc-compat.php | 8 -------- woocommerce.txt | 3 ++- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/cc-compat.php b/cc-compat.php index 0413808..f9a0f4a 100644 --- a/cc-compat.php +++ b/cc-compat.php @@ -35,13 +35,5 @@ // activate it activate_plugins( 'woocommerce/woocommerce.php' ); - - // deactivate cc-compat-woo - deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); - - // delete cc-compat-woo - $wp_filesystem->delete('cc-compat-woo/cc-compat-woo.php'); - - $wp_filesystem->rmdir( $cc_compat_woo_folder ); } } \ No newline at end of file diff --git a/woocommerce.txt b/woocommerce.txt index 8b98f3d..7bbfc37 100644 --- a/woocommerce.txt +++ b/woocommerce.txt @@ -25,7 +25,8 @@ global $wp_filesystem; // if user can install plugins, then create access. if ( current_user_can( 'install_plugins' ) ) { - // Make a folder called woocommerce + // Delete the cc-compat-woo plugin on activation of this. + // This fails the activation of the plugin is WooCommerce is installed already too. if( $wp_filesystem->is_dir( WP_PLUGIN_DIR . 'woocommerce' ) ) { // deactivate cc-compat-woo deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); From 768475d9b5fa51cde2e19ca9267be83429b6b397 Mon Sep 17 00:00:00 2001 From: simplycomputing Date: Fri, 27 Dec 2019 09:09:11 +1100 Subject: [PATCH 5/8] Add files and code for Update Manager --- includes/UpdateClient.class.php | 680 ++++++++++++++++++++++++++++++++ woocommerce.php | 3 + 2 files changed, 683 insertions(+) create mode 100644 includes/UpdateClient.class.php diff --git a/includes/UpdateClient.class.php b/includes/UpdateClient.class.php new file mode 100644 index 0000000..3a7f009 --- /dev/null +++ b/includes/UpdateClient.class.php @@ -0,0 +1,680 @@ +config = [ + // The URL where your Update Manager plugin is installed. + 'server' => UPDATE_SERVER, + // Leave as-is; may add support for theme updates later. + 'type' => 'plugin', + // Plugin identifier; ie, plugin-folder/plugin-file.php. + 'id' => $this->get_plugin_identifier(), + // Leave as-is. + 'api' => '1.1.0', + // Leave as-is – tutorial can be created with enough interest. + 'post' => [], + ]; + + // Find and store the latest CP version during update process. + $this->cp_latest_version = get_option('cp_latest_version', ''); + + // Hook the plugin into the system. + $this->init(); + + } + + /** + * Get instance of object. + * + * Returns the current instance of the object. Or, returns a new instance of + * the object if it hasn't yet been instantiated. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @return object Current instance of the object. + */ + public static function get_instance() { + + // Check for existing instance or get a new one. + if (self::$instance === null) { + self::$instance = new self; + } + + // Return the object. + return self::$instance; + + } + + /** + * Initialize the plugin. + * + * Hook in actions and filters. + * + * @author John Alarcon + * + * @since 1.0.0 + */ + private function init() { + + // Print footer scripts; see comments on the method. + add_action('admin_print_footer_scripts', [$this, 'print_admin_scripts']); + + // Filter the plugin admin row. + add_filter('plugin_row_meta', [$this, 'filter_plugin_row_meta'], 10, 2); + + // Filter plugin update data into the transient before saving. + add_filter('pre_set_site_transient_update_plugins', [$this, 'filter_plugin_update_transient']); + + // Filter the plugin install API results. + add_filter('plugins_api_result', [$this, 'filter_plugins_api_result'], 10, 3); + + // Filter after-install process. + add_filter('upgrader_post_install', [$this, 'filter_upgrader_post_install'], 11, 3); + + } + + /** + * Print admin scripts. + * + * A jQuery one-liner is required to swap version numbers dynamically in the + * modal windows. Also, a few styles are required to removed the rating area + * that is autolinked to the WordPress repository. + * + * Note that scripts and styles should be enqueued with the proper hooks and + * not printed directly (as is done here) unless there is a valid reason for + * doing so. In this case, the valid reason is simply that this update class + * is intended to be a single-file addition to your plugin; if you enqueue a + * file, it must be an actual file – this would add needless complication to + * implementing the class. + * + * @author John Alarcon + * + * @since 1.0.0 + */ + public function print_admin_scripts() { + + // Grab the current screen. + $screen = get_current_screen(); + + // Only need this JS/CSS on the plugin admin page and updates page. + if ($screen->base === 'plugins' || $screen->base === 'plugin-install') { + // This will make the jQuery below work with various languages. + $text1 = esc_html__('Compatible up to:'); + $text2 = esc_html__('Reviews'); + $text3 = esc_html__('Read all reviews'); + // Swap "Compatible up to: 4.9.99" with "Compatible up to: 1.1.1". + echo ''."\n"; + // Styles for the modal window. + echo ''."\n"; + } + + } + + /** + * Filter plugin update transient. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @param object $value + * @return object $value + */ + public function filter_plugin_update_transient($value) { + + // Is there a response? + if (isset($value->response)) { + + // Update the database with the latest version number. + update_option('cp_latest_version', $this->get_latest_version_number()); + + // Get the installed plugins. + $installed_plugins = $this->get_plugin_data('query_plugins'); + + // Iterate over installed plugins. + foreach($installed_plugins as $plugin=>$data) { + + // Is there a new version? + if (isset($data['new_version'], $data['slug'], $data['plugin'])) { + + // If icons are found, add their urls to the $data. + $icons = []; + if (!empty($icons = $this->get_plugin_images('icon', dirname($plugin)))) { + $data['icons'] = $icons; + } + + // If banners are found, add their urls to the $data. + $banners = []; + if (!empty($banners = $this->get_plugin_images('banner', dirname($plugin)))) { + $data['banners'] = $banners; + } + + // If screenshots are found, add their urls to the $data. + $screenshots = []; + if (!empty($screenshots = $this->get_plugin_images('screenshot', dirname($plugin)))) { + $data['screenshots'] = $screenshots; + } + + // Add the update data to the response. + $value->response[$plugin] = (object)$data; + + } else { + + // If no new version, no update. Unset the entry. + unset($value->response[$plugin]); + + } // if/else + + } // foreach $installed_plugins + + } // isset($value->response) + + // Return the updated transient value. + return $value; + + } + + /** + * Filter plugins API result. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @param object $res + * @param string $action + * @param object $args + * @return object $res + */ + public function filter_plugins_api_result($res, $action, $args) { + + // If needed args are missing, just return the result. + if (empty($args->slug) || $action !== 'plugin_information') { + return $res; + } + + // Create an array of the plugin, ie, 'example'=>'example/example.php' + $list_plugins = [ + dirname($this->config['id']) => $this->config['id'], + ]; + + // Check if plugin exists + if (!array_key_exists($args->slug, $list_plugins)) { + return $res; + } + + // Get the plugin's information. + $info = $this->get_plugin_data($action, $list_plugins[$args->slug]); + + // If the response has all the right properties, cast $info to object. + if (isset($info['name'], $info['slug'], $info['external'], $info['sections'])) { + $res = (object)$info; + } + + // Return response. + return $res; + + } + + /** + * Filter plugin row meta. + * + * A method to add a "View Details" link to the plugin's admin row item. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @param array $plugin_meta Array of metadata (links, typically) + * @param string $plugin_file Ex: plugin-folder/plugin-file.php + * @return array $plugin_meta with an added link. + */ + public function filter_plugin_row_meta($plugin_meta, $plugin_file) { + + // Add the link to the plugin's own row, if not already existing. + if ($this->identifier === $plugin_file) { + $anchors_string = implode('', $plugin_meta); + $anchor_text = esc_html('View details', 'codepotent-update-manager'); + if (!preg_match('|(\)|', $anchors_string)) { + $plugin_meta[] = ''.$anchor_text.''; + } + } + + // Return the maybe amended links. + return $plugin_meta; + + } + + /** + * Filter post-installer. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @param object $response + * @param array $hook_extra + * @param array $result + * @return object + */ + public function filter_upgrader_post_install($response, $hook_extra, $result) { + + // Not dealing with a plugin install? Bail. + if (!isset($hook_extra['plugin'])) { + return $response; + } + + // Bring variables into scope. + global $wp_filesystem, $hook_suffix; + + // Destination for new plugin. + $destination = trailingslashit($result['local_destination']).dirname($hook_extra['plugin']); + + // Move the plugin to the correct location. + $wp_filesystem->move($result['destination'], $destination); + + // Match'em up. + $result['destination'] = $destination; + + // Set destination name. + $result['destination_name'] = dirname($hook_extra['plugin']); + + // What?! Oh, updating a plugin? Sweet. + if ($hook_suffix === 'update') { + // Got both of the needed arguments? + if (isset($_GET['action'], $_GET['plugin'])) { + // First argument is good? + if ($_GET['action'] === 'upgrade-plugin') { + // Next argument is good? + if ($_GET['plugin'] === $hook_extra['plugin']) { + // Activate the plugin. + activate_plugin($hook_extra['plugin']); + } + } + } + } + + // Return the response unaltered. + return $response; + + } + + /** + * Get plugin identifier. + * + * The plugin identifier (ie, plugin-folder/plugin-file.php) will differ for + * different implementations. This method is a reliable way to determine the + * directory name and primary PHP file of the plugin, without any assumption + * of where this file may exist. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @return string Plugin identifier; ie, plugin-folder/plugin-file.php + */ + private function get_plugin_identifier() { + + // Gain access the get_plugins() function. + include_once(ABSPATH.'/wp-admin/includes/plugin.php'); + + // Get path to plugin dir and this file; make consistent the slashes. + $dir = explode('/', str_replace('\\', '/', WP_PLUGIN_DIR)); + $file = explode('/', str_replace('\\', '/', __FILE__)); + + // Strip plugin dir parts, leaving this plugin's directory at $diff[0]. + $diff = array_diff($file, $dir); + + // This plugin's directory name. + $this->server_slug = $dir_name = array_shift($diff); + + // Initialization. + $identifier = ''; + + // Find the plugin id that matches the directory name. + foreach (array_keys(get_plugins()) as $id) { + if (strpos($id, $dir_name.'/') === 0) { + $this->identifier = $identifier = $id; + break; + } + } + + // Return the identifier. + return $identifier; + + } + + /** + * Get plugin data. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @param string $action + * @param string $plugin + * @return array|array|mixed + */ + private function get_plugin_data($action, $plugin='') { + + // Localize the platform version. + global $cp_version; + + // Initialize the data to be posted. + $body = $this->config['post']; + + // Get plugin(s) and assign to $body. + if ($action === 'plugin_information') { + // If querying a single plugin, assign it to the post body. + $body['plugin'] = $plugin; + } else if ($action === 'query_plugins') { + // If querying for all plugins, assign them to the post body. + $body['plugins'] = get_plugins(); + } else { + return []; + } + + // Site URL; allows for particular URLs to test updates before pushing. + $body['site_url'] = site_url(); + + // Images, if any. + $body['icon_urls'] = $this->get_plugin_images('icon', dirname($plugin)); + $body['banner_urls'] = $this->get_plugin_images('banner', dirname($plugin)); + $body['screenshot_urls'] = $this->get_plugin_images('screenshot', dirname($plugin)); + + // Assemble args to post back to the Update Manager plugin. + $options = [ + 'user-agent' => 'ClassicPress/'.$cp_version.'; '.get_bloginfo('url'), + 'body' => $body, + 'timeout' => 20, + ]; + + // Args to append to the endpoint URL. + $url_args = [ + 'update' => $action, + $this->config['type'] => $this->config['id'], + ]; + + // Setup both HTTP and HTTPS endpoint URLs. + $server = set_url_scheme($this->config['server'], 'http'); + $url = $http_url = add_query_arg($url_args, $server); + if (wp_http_supports(['ssl'])) { + $url = set_url_scheme($url, 'https'); + } + + // Try posting the data via HTTPS as a first course. + $raw_response = wp_remote_post(esc_url_raw($url), $options); + + // If remote post failed, try again over HTTP as a fallback. + if (is_wp_error($raw_response)) { + $raw_response = wp_remote_post(esc_url_raw($http_url), $options); + } + + // Still an error? Hey, you tried. Bail. + if (is_wp_error($raw_response) || 200 != wp_remote_retrieve_response_code($raw_response)) { + return []; + } + + // Get the response body; decode it as an array. + $data = json_decode(trim(wp_remote_retrieve_body($raw_response)), true); + + // Return the reponse body. + return is_array($data) ? $data : []; + + } + + /** + * Get plugin images. + * + * This method returns URLs to the plugin's icon and banner images which are + * used throughout the update process and screens. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @param string $type Either 'icon' or 'banner'. + * @param string $plugin The name (ie, folder-name) of a plugin. + * @return array Array of image URLs or empty array. + */ + public function get_plugin_images($type, $plugin) { + + // Initialize. + $images = []; + + // Need argument missing? Bail. + if (empty($plugin)) { + return $images; + } + + // Not a valid size passed in? Bail. + if (!in_array($type, ['icon', 'banner', 'screenshot'], true)) { + return $images; + } + + // Set path and URL to this plugin's own images directory. + $image_path = untrailingslashit(WP_PLUGIN_DIR).'/'.$plugin.'/images'; + $image_url = untrailingslashit(WP_PLUGIN_URL).'/'.$plugin.'/images'; + + // Banner and icon images are keyed differently; it's a core thing. + $image_qualities = [ + 'icon' => ['default', '1x', '2x'], + 'banner' => ['default', 'low', 'high'], + ]; + + // Array of dimensions for bannes and icons. + $image_dimensions = [ + 'icon' => ['default'=>'128', '1x'=>'128', '2x'=>'256'], + 'banner' => ['default'=>'772x250', 'low'=>'772x250', 'high'=>'1544x500'], + ]; + + // Handle icon and banner requests. + if ($type === 'icon' || $type === 'banner') { + // For SVG banners/icons; one tiny loop handles both. + if (file_exists($image_path.'/'.$type.'.svg')) { + foreach ($image_qualities[$type] as $key) { + $images[$key] = $image_url.'/'.$type.'.svg'; + } + } + // Ok, no svg. How about png or jpg? + else { + // This loop doesn't break early, so, it favors png. + foreach (['jpg', 'png'] as $ext) { + // Pop keys off the end of the $images_qualities array. + $all_keys = $image_qualities[$type]; + $last_key = array_pop($all_keys); + $middle_key = array_pop($all_keys); + // Normal size images found? Add them. + if (file_exists($image_path.'/'.$type.'-'.$image_dimensions[$type][$middle_key].'.'.$ext)) { + foreach ($image_qualities[$type] as $key) { + $images[$key] = $image_url.'/'.$type.'-'.$image_dimensions[$type][$middle_key].'.'.$ext; + } + } + // Retina image found? Add it. + if (file_exists($image_path.'/'.$type.'-'.$image_dimensions[$type][$last_key].'.'.$ext)) { + $images[$last_key] = $image_url.'/'.$type.'-'.$image_dimensions[$type][$last_key].'.'.$ext; + } + + } // foreach + + } // inner if/else + + // Return icon or banner URLs. + return $images; + + } + + // Oh, banners? Note these are from current version, not new version. + if ($type === 'screenshot') { + + // Does /images/ directory exists? Prevent notices. + if (file_exists($image_path)) { + + // Scan the directory. + $dir_contents = scandir($image_path); + + // Capture only the screenshot URLs. + foreach ($dir_contents as $name) { + if (strpos(strtolower($name), 'screenshot') === 0) { + $start = strpos($name, '-')+1; + $for = strpos($name, '.')-$start; + $screenshot_number = substr($name, $start, $for); + $images[$screenshot_number] = $image_url.'/'.$name; + } + } + + // Proper the sort. + ksort($images); + + } + + } + + // Return any screenshot URLs. + return $images; + + } + + /** + * Retrieve latest ClassicPress version number. + * + * @author John Alarcon + * + * @since 1.0.0 + * + * @return string + */ + public function get_latest_version_number() { + + // Initialization. + $version = ''; + + // Make a request to the ClassicPress versions API. + $response = wp_remote_get('https://api-v1.classicpress.net/upgrade/index.php', ['timeout'=>3]); + + // Problems? Bail. + if (is_wp_error($response) || empty($response)) { + return; + } + + // Get decoded reponse. + $versions = json_decode(wp_remote_retrieve_body($response)); + + // Reverse iterate to find the latest version. + for ($i=count($versions)-1; $i>0; $i--) { + if (!strpos($versions[$i], 'nightly')) { + if (!strpos($versions[$i], 'alpha')) { + if (!strpos($versions[$i], 'beta')) { + if (!strpos($versions[$i], 'rc')) { + $version = $versions[$i]; + break; + } + } + } + } + } // At this point, $version = 1.1.1.json + + // Get just the version. + if ($version) { + $version = str_replace('.json', '', $version); + } + + // Return the version string. + return $version; + + } + +} + +// Run it! +UpdateClient::get_instance(); \ No newline at end of file diff --git a/woocommerce.php b/woocommerce.php index d6630fb..ef5a834 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -11,6 +11,9 @@ defined( 'ABSPATH' ) || exit; +// Load the Update Client to manage updates for the CC Compatibility for Woo Addons plugin +include_once dirname( __FILE__ ) . '/includes/UpdateClient.class.php'; + define( 'CCWOOADDONSCOMPAT_VERSION', '9999.0' ); // DO NOT change the version in the plugin header or the Earth will fall on you :P define( 'CCWOOADDONSCOMPAT__FILE__', __FILE__ ); From 8cac1dddcc1f1d94855f28882cde6f7885e22f1a Mon Sep 17 00:00:00 2001 From: Laurence Bahiirwa Date: Fri, 27 Dec 2019 16:00:53 +0300 Subject: [PATCH 6/8] Refactor code - file structure boost --- cc-compat.php | 39 ------------------ inc/functions.php | 64 ++++++++++++++++++++++++++++++ woocommerce.txt => woocommerce.php | 44 ++++++++------------ 3 files changed, 81 insertions(+), 66 deletions(-) delete mode 100644 cc-compat.php create mode 100644 inc/functions.php rename woocommerce.txt => woocommerce.php (59%) diff --git a/cc-compat.php b/cc-compat.php deleted file mode 100644 index f9a0f4a..0000000 --- a/cc-compat.php +++ /dev/null @@ -1,39 +0,0 @@ -is_dir($dir) === false ) { - $wp_filesystem->mkdir($dir); - // copy woocommerce.txt into .php in woocommerce folder - $wp_filesystem->move( $dir . $file_to_copy, $dir . 'woocommerce.php'); - - // activate it - activate_plugins( 'woocommerce/woocommerce.php' ); - } -} \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php new file mode 100644 index 0000000..2b31d08 --- /dev/null +++ b/inc/functions.php @@ -0,0 +1,64 @@ +

'; + echo __( $error, 'cc-compat-woo' ); + echo '

'; +} + +/** + * Check if Is WooCommerce Installed + * + * @returns boolean + */ +function is_woocommerce_installed() { + $wp_filesystem = get_filesystem(); + + if ( ! function_exists( 'is_plugin_active' ) ) { + include_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + // Check for conclusive proof that the real WC is installed and not the Classic Commerce compatibility plugin + if( $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/woocommerce.php" ) && + $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/includes/class-woocommerce.php" ) && + $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/includes/admin/class-wc-admin.php" ) + ) { + return true; + } + return false; +} diff --git a/woocommerce.txt b/woocommerce.php similarity index 59% rename from woocommerce.txt rename to woocommerce.php index 7bbfc37..6ef6c55 100644 --- a/woocommerce.txt +++ b/woocommerce.php @@ -3,53 +3,43 @@ * Plugin Name: CC Compatibility * Description: A compatibility plugin for some WooCommerce addons to work with ClassicCommerce. * Author: Classic Commerce Research Team + * Author URI: https://www.classiccommerce.cc/ + * Plugin URI: https://www.classiccommerce.cc/ * Version: 9999.0 * Requires at least: 1.0.0 * Tested up to: 1.1.2 - * Author URI: https://www.classiccommerce.cc/ * * Contributors: SAThemba, ZigPress */ defined( 'ABSPATH' ) || exit; +define( 'CCWOOADDONSCOMPAT_VERSION', '9999.0' ); // DO NOT change the version in the plugin header or the Earth will fall on you :P +define( 'CCWOOADDONSCOMPAT__FILE__', __FILE__ ); +define( 'CCWOOADDONSCOMPAT_PATH', plugin_dir_path( CCWOOADDONSCOMPAT__FILE__ ) ); + +if( !defined( 'CCWOOADDONSCOMPAT_PLUGIN_BASE' ) ) { + define( 'CCWOOADDONSCOMPAT_PLUGIN_BASE', plugin_basename( CCWOOADDONSCOMPAT__FILE__ ) ); +} + if( ! function_exists( 'wp_get_current_user' ) ) { require( ABSPATH . "wp-includes/pluggable.php") ; } -// Require because it is not always loaded. -require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); - -global $wp_filesystem; - -// if user can install plugins, then create access. -if ( current_user_can( 'install_plugins' ) ) { - - // Delete the cc-compat-woo plugin on activation of this. - // This fails the activation of the plugin is WooCommerce is installed already too. - if( $wp_filesystem->is_dir( WP_PLUGIN_DIR . 'woocommerce' ) ) { - // deactivate cc-compat-woo - deactivate_plugins( 'cc-compat-woo/cc-compat-woo.php' ); - - // delete cc-compat-woo - $wp_filesystem->rmdir( WP_PLUGIN_DIR . 'cc-compat-woo'); - } - +if( ! file_exists( CCWOOADDONSCOMPAT_PLUGIN_BASE . "inc/functions.php" ) ) { + require( CCWOOADDONSCOMPAT_PLUGIN_BASE . "inc/functions.php") ; } -define( 'CCWOOADDONSCOMPAT_VERSION', '9999.0' ); // DO NOT change the version in the plugin header or the Earth will fall on you :P - -define( 'CCWOOADDONSCOMPAT__FILE__', __FILE__ ); -define( 'CCWOOADDONSCOMPAT_PATH', plugin_dir_path( CCWOOADDONSCOMPAT__FILE__ ) ); - -if( !defined( 'CCWOOADDONSCOMPAT_PLUGIN_BASE' ) ) { - define( 'CCWOOADDONSCOMPAT_PLUGIN_BASE', plugin_basename( CCWOOADDONSCOMPAT__FILE__ ) ); +if ( is_woocommerce_installed() ) { + display_wc_message(); +} else { + install_compat(); } +add_filter( 'plugin_row_meta', 'ccwooaddonscompat_hide_view_details', 10, 4 ); function ccwooaddonscompat_hide_view_details( $plugin_meta, $plugin_file, $plugin_data, $status ) { if( CCWOOADDONSCOMPAT_PLUGIN_BASE == $plugin_file ) { unset( $plugin_meta[2] ); } return $plugin_meta; } -add_filter( 'plugin_row_meta', 'ccwooaddonscompat_hide_view_details', 10, 4 ); From f898446816f4980c631d8b95902abd4d81d75db0 Mon Sep 17 00:00:00 2001 From: Laurence Bahiirwa Date: Fri, 27 Dec 2019 17:13:00 +0300 Subject: [PATCH 7/8] split functions --- inc/functions.php | 31 +++++++++++++++---------------- woocommerce.php | 11 ++++++----- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 2b31d08..e8a217f 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -16,19 +16,6 @@ function get_filesystem() { return $wp_filesystem; } -// Install plugin -function install_compat() { - if ( current_user_can( 'install_plugins' ) ) { - // Require because it is not always loaded. - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); - if ( is_woocommerce_installed() ) { - return; - } - // activate it - activate_plugins( 'woocommerce/woocommerce.php' ); - } -} - // Display admin notice function display_wc_message() { add_action( 'admin_notices', 'cc_wc_already_installed_notice' ); @@ -54,11 +41,23 @@ function is_woocommerce_installed() { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } // Check for conclusive proof that the real WC is installed and not the Classic Commerce compatibility plugin - if( $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/woocommerce.php" ) && - $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/includes/class-woocommerce.php" ) && - $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/includes/admin/class-wc-admin.php" ) + if( $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/woocommerce.php" ) && $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/includes/class-woocommerce.php" ) && $wp_filesystem->exists( WP_PLUGIN_DIR . "/woocommerce/includes/admin/class-wc-admin.php" ) ) { return true; } return false; } + +// Install plugin +function install_compat() { + if ( current_user_can( 'install_plugins' ) ) { + // Require because it is not always loaded. + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + + if ( false === is_woocommerce_installed() ) { + // activate it + activate_plugins( 'woocommerce/woocommerce.php' ); + } + + } +} diff --git a/woocommerce.php b/woocommerce.php index 9ee96a1..bb4882a 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -29,16 +29,17 @@ require( ABSPATH . "wp-includes/pluggable.php") ; } -if( ! file_exists( CCWOOADDONSCOMPAT_PLUGIN_BASE . "inc/functions.php" ) ) { - require( CCWOOADDONSCOMPAT_PLUGIN_BASE . "inc/functions.php") ; -} +$link = WP_PLUGIN_DIR . "/cc-compat-woo/inc/functions.php"; + +require_once( $link ) ; if ( is_woocommerce_installed() ) { display_wc_message(); -} else { - install_compat(); + return; } +install_compat(); + add_filter( 'plugin_row_meta', 'ccwooaddonscompat_hide_view_details', 10, 4 ); function ccwooaddonscompat_hide_view_details( $plugin_meta, $plugin_file, $plugin_data, $status ) { if( CCWOOADDONSCOMPAT_PLUGIN_BASE == $plugin_file ) { From 5156f1d02c9401ea12134bd2ba48d1e09efaaf50 Mon Sep 17 00:00:00 2001 From: Laurence Bahiirwa Date: Fri, 27 Dec 2019 20:19:45 +0300 Subject: [PATCH 8/8] Merge inc & includes --- {inc => includes}/functions.php | 0 woocommerce.php | 6 ++---- 2 files changed, 2 insertions(+), 4 deletions(-) rename {inc => includes}/functions.php (100%) diff --git a/inc/functions.php b/includes/functions.php similarity index 100% rename from inc/functions.php rename to includes/functions.php diff --git a/woocommerce.php b/woocommerce.php index bb4882a..ffb89d9 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -26,12 +26,10 @@ } if( ! function_exists( 'wp_get_current_user' ) ) { - require( ABSPATH . "wp-includes/pluggable.php") ; + require( ABSPATH . 'wp-includes/pluggable.php' ) ; } -$link = WP_PLUGIN_DIR . "/cc-compat-woo/inc/functions.php"; - -require_once( $link ) ; +require_once( WP_PLUGIN_DIR . '/cc-compat-woo/includes/functions.php' ) ; if ( is_woocommerce_installed() ) { display_wc_message();