Skip to content

Commit

Permalink
Merge pull request #15 from nomasi/master
Browse files Browse the repository at this point in the history
Options prefixed in database
  • Loading branch information
nomasi authored Apr 28, 2020
2 parents e480e23 + b24702d commit a35b94e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 25 deletions.
5 changes: 3 additions & 2 deletions classes/class-wc-settings-sizeme-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class WC_Settings_SizeMe_for_WooCommerce extends WC_Settings_Page {
*/
const ADD_TOGGLER_YES = 'yes';


/**
* Class constructor.
*
Expand Down Expand Up @@ -141,6 +140,7 @@ public function get_settings() {
self::SERVICE_STATUS_ON => 'On',
self::SERVICE_STATUS_OFF => 'Off',
),
'default' => get_option( WC_SizeMe_for_WooCommerce::SERVICE_STATUS_ID, self::SERVICE_STATUS_OFF ),
'id' => WC_SizeMe_for_WooCommerce::SERVICE_STATUS_ID,
),
array(
Expand All @@ -164,7 +164,7 @@ public function get_settings() {
'type' => 'multiselect',
'options' => self::load_size_attribute_options(),
'css' => 'width: 150px; height: 150px;',
'id' => 'size_attributes',
'id' => WC_SizeMe_for_WooCommerce::SIZE_ATTRIBUTES_KEY,
),
array(
'type' => 'sectionend',
Expand Down Expand Up @@ -212,6 +212,7 @@ public function get_settings() {
self::ADD_TOGGLER_NO => 'No',
self::ADD_TOGGLER_YES => 'Yes',
),
'default' => get_option( WC_SizeMe_for_WooCommerce::ADD_TOGGLER, self::ADD_TOGGLER_NO ),
'id' => WC_SizeMe_for_WooCommerce::ADD_TOGGLER,
),
array(
Expand Down
9 changes: 7 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== SizeMe for WooCommerce ===
Contributors: sizeme
Tags: sizeme, measurements, size guide, size recommendations
Tags: sizeme, measurements, sizeguide, size guide, size recommendations
Requires at least: 3.8
Tested up to: 5.4
Stable tag: 2.1.1
Stable tag: 2.1.2
Requires PHP: 5.2.4
WC requires at least: 2.5
WC tested up to: 4.0.1
Expand Down Expand Up @@ -82,6 +82,11 @@ To install and take into use the SizeMe for WooCommerce plugin, follow the instr
4. Visualize the fit using the SizeMe Sizing Bar™ and easy-to-understand illustrations

== Changelog ==
= 2.2.0 =
* Options (finally) prefixed to avoid plugin conflicts and some significant code cleanup too.

= 2.1.2 =
* Added support for the clear size selection button.

= 2.1.1 =
* Fixed tracking of ajax-based add to cart events. Also improved SKU handling with simple products.
Expand Down
97 changes: 78 additions & 19 deletions sizeme-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
* @wordpress-plugin
* Plugin Name: SizeMe for WooCommerce
* Description: SizeMe is a web store plugin that enables your consumers to input their measurements and get personalised fit recommendations based on actual product data.
* Version: 2.1.1
* Version: 2.2.0
* Author: SizeMe Ltd
* Author URI: https://www.sizeme.com/
* Text Domain: sizeme
* License: GPLv2 or later
*
* WC requires at least: 2.5
* WC tested up to: 4.0.1
*
* SizeMe for WooCommerce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
Expand Down Expand Up @@ -50,7 +53,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string VERSION The plugin version.
*/
const VERSION = '2.1.1';
const VERSION = '2.2.0';

/**
* Minimum WordPress version this plugin works with, used for dependency checks.
Expand Down Expand Up @@ -116,14 +119,23 @@ class WC_SizeMe_for_WooCommerce {
*/
protected static $attributes = array();

/**
* Plugin version option key.
*
* @since 2.2.0
*
* @var string SERVICE_STATUS_ID The key for the service status.
*/
const PLUGIN_VERSION_KEY = 'sizeme_version';

/**
* Service status option key, used when saving settings and retrieving them.
*
* @since 1.0.0
*
* @var string SERVICE_STATUS_ID The key for the service status.
*/
const SERVICE_STATUS_ID = 'service_status';
const SERVICE_STATUS_ID = 'sizeme_service_status';

/**
* UI option, API key, used in conversations with the SizeMe Shop API
Expand All @@ -132,7 +144,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string API_KEY The key for the Key!
*/
const API_KEY = 'api_key';
const API_KEY = 'sizeme_api_key';

/**
* UI option, append content to element, used in settings.
Expand All @@ -141,7 +153,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string APPEND_CONTENT_TO The key for UI option.
*/
const APPEND_CONTENT_TO = 'append_content_to';
const APPEND_CONTENT_TO = 'sizeme_append_content_to';

/**
* UI option, invoke element, used in settings.
Expand All @@ -150,7 +162,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string INVOKE_ELEMENT The key for UI option.
*/
const INVOKE_ELEMENT = 'invoke_element';
const INVOKE_ELEMENT = 'sizeme_invoke_element';

/**
* UI option, size selector type, used in settings.
Expand All @@ -159,7 +171,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string SIZE_SELECTION_TYPE The key for UI option.
*/
const SIZE_SELECTION_TYPE = 'size_selection_type';
const SIZE_SELECTION_TYPE = 'sizeme_size_selection_type';

/**
* UI option, add to cart element, used in settings.
Expand All @@ -168,7 +180,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string ADD_TO_CART_ELEMENT The key for UI option.
*/
const ADD_TO_CART_ELEMENT = 'add_to_cart_element';
const ADD_TO_CART_ELEMENT = 'sizeme_add_to_cart_element';

/**
* UI option, add to cart event, used in settings.
Expand All @@ -177,7 +189,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string ADD_TO_CART_EVENT The key for UI option.
*/
const ADD_TO_CART_EVENT = 'add_to_cart_event';
const ADD_TO_CART_EVENT = 'sizeme_add_to_cart_event';

/**
* UI option, add toggler
Expand All @@ -186,7 +198,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var boolean ADD_TOGGLER The key for UI option.
*/
const ADD_TOGGLER = 'add_toggler';
const ADD_TOGGLER = 'sizeme_add_toggler';

/**
* UI option, lang override, used in settings.
Expand All @@ -195,7 +207,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string LANG_OVERRIDE The key for UI option.
*/
const LANG_OVERRIDE = 'lang_override';
const LANG_OVERRIDE = 'sizeme_lang_override';

/**
* UI option, custom css, used in settings.
Expand All @@ -204,7 +216,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string CUSTOM_CSS The key for UI option.
*/
const CUSTOM_CSS = 'custom_css';
const CUSTOM_CSS = 'sizeme_custom_css';

/**
* UI option, additional translations, used in settings.
Expand All @@ -213,7 +225,16 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string ADDITIONAL_TRANSLATIONS The key for UI option.
*/
const ADDITIONAL_TRANSLATIONS = 'additional_translations';
const ADDITIONAL_TRANSLATIONS = 'sizeme_additional_translations';

/**
* UI option, max recommendation distance, used in settings.
*
* @since 2.1.0
*
* @var string MAX_RECOMMENDATION_DISTANCE The key for UI option.
*/
const MAX_RECOMMENDATION_DISTANCE = 'sizeme_max_recommendation_distance';

/**
* UI option, max recommendation distance, used in settings.
Expand All @@ -222,7 +243,7 @@ class WC_SizeMe_for_WooCommerce {
*
* @var string MAX_RECOMMENDATION_DISTANCE The key for UI option.
*/
const MAX_RECOMMENDATION_DISTANCE = 'max_recommendation_distance';
const SIZE_ATTRIBUTES_KEY = 'sizeme_size_attributes';

/**
* Info related to SizeMe API requests
Expand Down Expand Up @@ -330,7 +351,6 @@ public function get_service_status() {
return get_option( self::SERVICE_STATUS_ID );
}


/**
* Get the the client key which is a hash of the super-secret api key
*
Expand Down Expand Up @@ -515,7 +535,7 @@ protected function load_skus( WC_Product_Variable $product ) {
* @return array|string If parameter $one is true, returns a string of attribute name, otherwise an array of names.
*/
protected function get_size_attribute( WC_Product_Variable $product, $one = true ) {
$size_attributes = get_option( 'size_attributes', array() );
$size_attributes = get_option( self::SIZE_ATTRIBUTES_KEY, array() );
$product_attributes = $product->get_attributes();
$attribute_names = array();
foreach ( $product_attributes as $attribute_name => $attribute_data ) {
Expand Down Expand Up @@ -759,7 +779,6 @@ public function get_plugin_name() {
*/
public function add_setting_page( $settings ) {
$settings[] = require_once( 'classes/class-wc-settings-sizeme-for-woocommerce.php' );

return $settings;
}

Expand Down Expand Up @@ -820,10 +839,12 @@ protected function init_frontend() {
*/
protected function init_admin() {
add_filter( 'woocommerce_get_settings_pages', array( $this, 'add_setting_page' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
// ajax based add to carts come this way
if ( wp_doing_ajax() ) {
add_action( 'woocommerce_add_to_cart', array( $this, 'send_add_to_cart_info' ), 10, 6 );
} else {
// Migrate dumb old option system to new if applicable
$this->migrate_possible_options();
}
}

Expand All @@ -835,7 +856,7 @@ protected function init_admin() {
* @since 1.0.0
*/
public function admin_notices() {
$size_attributes = get_option( 'size_attributes', array() );
$size_attributes = get_option( self::SIZE_ATTRIBUTES_KEY, array() );
if ( empty( $size_attributes ) ) {
$this->render( 'admin-notice' );
}
Expand Down Expand Up @@ -911,6 +932,44 @@ protected function check_dependencies() {
return true;
}

/**
* Migrate old options.
*
* Versions before 2.2.0 used non-prefixed names for options and that is very bad.
*
* @since 2.2.0
*
* @return bool True always
*/
protected function migrate_possible_options() {
// Check if the sizeme_version option has been set. If it's not present, previous (non-prefixed) option keys are likely to been set.
if ( get_option('sizeme_version', NULL) === NULL ) {
// Check if there actually are old options present
if ( get_option('service_status', NULL) !== NULL ) {
require_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/settings/class-wc-settings-page.php' );
require_once( 'classes/class-wc-settings-sizeme-for-woocommerce.php' );
$setting_page = New WC_Settings_SizeMe_for_WooCommerce;
$settings = $setting_page->get_settings();
foreach( $settings as $s ) {
// check if old style option exists and update if so
if ( isset( $s['id'] ) ) {
$current_id = $s['id'];
if ( strpos( $current_id, 'sizeme_' ) !== false ) {
$old_id = str_replace( 'sizeme_','',$current_id );
if ( get_option($old_id, NULL) !== NULL ) {
update_option( $current_id, get_option( $old_id ) );
delete_option( $old_id );
}
}
}
}
}
add_option( 'sizeme_version', self::VERSION );
}

return true;
}

/**
* Override the locate_template function.
*
Expand Down
12 changes: 10 additions & 2 deletions templates/sizeme-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<script type="text/javascript">
//<![CDATA[
var sizeme_options = {
service_status: "<?php echo esc_js( $sizeme->get_service_status() ); ?>",
pluginVersion: "WC-<?php echo WC_VERSION; ?>",
serviceStatus: "<?php echo esc_js( $sizeme->get_service_status() ); ?>",
pluginVersion: "WC-<?php echo WC_SizeMe_for_WooCommerce::VERSION; ?>",
shopType: "woocommerce",
clientKey: "<?php echo esc_js( $sizeme->get_client_key() ); ?>",
uiOptions: {}
Expand Down Expand Up @@ -103,6 +103,14 @@
$el = '.variations select';
if ( $sizeme->get_ui_option( WC_SizeMe_for_WooCommerce::INVOKE_ELEMENT, '' ) ) $el = $sizeme->get_ui_option( WC_SizeMe_for_WooCommerce::INVOKE_ELEMENT, '' );
echo 'if (window.jQuery) { jQuery(function() { if (document.querySelector("'.$el.'")) document.querySelector("'.$el.'").addEventListener("sizemeChange", function(e) { jQuery("'.$el.'").trigger("change"); }) } ); }';

//
// Also add support for possible clear size selection button
// Includes a timeout to let Woo do it's thing (clear the selection)
//
// @since 2.1.2
//
echo 'if (window.jQuery) jQuery(function() { jQuery(".reset_variations").click(function() { if (document.querySelector("'.$el.'")) { setTimeout(function() { document.querySelector("'.$el.'").dispatchEvent(new Event("change")); }, 200); } } ); } );';
?>
//]]>
</script>
Expand Down

0 comments on commit a35b94e

Please sign in to comment.