diff --git a/readme.txt b/readme.txt index a27abb3..6cf43ac 100644 --- a/readme.txt +++ b/readme.txt @@ -2,9 +2,9 @@ Contributors: hxii Tags: yotpo, shortcode, shortcodes, yotpo add-on, yotpo shortcodes, shortcodes for yotpo Requires at least: 4.6 -Requires PHP: 5.6 +Requires PHP: 7.0 Tested up to: 5.3 -Stable tag: 1.1.6 +Stable tag: 1.2.1 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html @@ -47,6 +47,12 @@ Get in touch, and I'll see what can be done. == Screenshots == 1. Example usage of the shortcodes. == Changelog == += 1.2.1 = +* Code cleanup. +* Added review highlights widget `[yotpo_highlights]` with optional `product_id` argument. +* Added Q&A widget `[yotpo_questions]` with optional `product_id` argument. += 1.2.0 = +* Now using Yotpo Reviews for WooCommerce (YRFW) as requirement. = 1.1.6 = * Showing notice to warn users prior to updating to 1.2.X versions. * Checked with latest version of WP. diff --git a/wc_yotpo_shortcodes.php b/wc_yotpo_shortcodes.php index 88ebbf6..fb83b34 100644 --- a/wc_yotpo_shortcodes.php +++ b/wc_yotpo_shortcodes.php @@ -2,7 +2,7 @@ /* * Plugin Name: Shortcodes for Yotpo * Description: This plugin adds the ability to use shortcodes to control the placement of Yotpo widgets. -* Version: 1.1.6 +* Version: 1.2.1 * Author: Paul Glushak * Author URI: http://paulglushak.com/ * Plugin URI: http://paulglushak.com/shortcodes-for-yotpo/ @@ -12,81 +12,113 @@ /* * This plugin allows using shortcodes to display Yotpo widgets inside and oustide (applicable widgets only) of product pages e.g. page builders, sidebars, widgets etc. - * See example usage at the bottom. */ -// if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly -defined( 'ABSPATH' ) or die(); +defined( 'ABSPATH' ) || die(); /** * Shortcodes! */ -class Yotpo_Shortcodes -{ +class Yotpo_Shortcodes { - function __construct() - { - $this->init_shortcodes(); + /** + * Run the init function on construction via action + * + * @since 1.2.0 + */ + public function __construct() { + add_action( 'plugins_loaded', array( $this, 'init_shortcodes' ) ); } - function init_shortcodes() { - add_shortcode( 'yotpo_widget', array( $this, 'yotpo_widget' ) ); - add_shortcode( 'yotpo_bottomline', array( $this, 'yotpo_bottomline' ) ); - add_shortcode( 'yotpo_product_gallery', array( $this, 'yotpo_product_gallery' ) ); - add_shortcode( 'yotpo_product_reviews_carousel', array( $this, 'yotpo_product_reviews_carousel' ) ); - add_shortcode( 'yotpo_badge', array( $this, 'yotpo_badge' ) ); - add_shortcode( 'yotpo_testimonials', array( $this, 'yotpo_testimonials' ) ); - add_action( 'admin_notices', array( $this, 'show_upgrade_message' ) ); + /** + * Set short transient for thank-you message + * + * @return void + * @since 1.2.1 + */ + public function activate() { + set_transient( 'yotpo_shortcodes_message', true, 5 ); } + /** - * Basic dependency check, to be replaced with class requirement + * Registers all the shortcodes in WP * * @return void - **/ - private function basic_check() { - if ( !class_exists( 'woocommerce' ) ) { - return; - } elseif ( !function_exists( 'wc_yotpo_get_product_data' ) ) { - $plugins = get_option( 'active_plugins', array() ); - if ( array_search('wc_yotpo.php', $plugins) ) { - require_once( ABSPATH . 'wp-content/plugins/yotpo-social-reviews-for-woocommerce/wc_yotpo.php' ); - } else { - die(); + * @since 1.2.0 + */ + public function init_shortcodes() { + if ( class_exists( 'YRFW_Reviews' ) ) { + if ( get_transient( 'yotpo_shortcodes_message' ) ) { + new YRFW_Messages( 'Thank you for installing Shortcodes for Yotpo! Please consider leaving a review, it really helps!', 'success', true, true ); } + add_shortcode( 'yotpo_widget', array( $this, 'yotpo_widget' ) ); + add_shortcode( 'yotpo_bottomline', array( $this, 'yotpo_bottomline' ) ); + add_shortcode( 'yotpo_questions', array( $this, 'yotpo_questions' ) ); + add_shortcode( 'yotpo_product_gallery', array( $this, 'yotpo_product_gallery' ) ); + add_shortcode( 'yotpo_product_reviews_carousel', array( $this, 'yotpo_product_reviews_carousel' ) ); + add_shortcode( 'yotpo_badge', array( $this, 'yotpo_badge' ) ); + add_shortcode( 'yotpo_testimonials', array( $this, 'yotpo_testimonials' ) ); + add_shortcode( 'yotpo_highlights', array( $this, 'yotpo_highlights' ) ); + } else { + add_action( + 'admin_notices', + function() { + ?> +
+

Please install and/or activate Yotpo Reviews for WooCommerce in order for Shortcodes for Yotpo to work.

+
+ -
-

Shortcodes for Yotpo - Please note that the next update of Shortcodes for Yotpo (version 1.2.1) will require Yotpo Reviews for WooCommerce (YRFW) to function. -
Please make sure you install and configure YRFW when updating Shortcodes for Yotpo. For more information, please see here.

-
- basic_check(); - if ( isset( $args['product_id'] ) ) { $product_id = $args['product_id']; } elseif ( is_product() ) { global $product; $product_id = $product->get_id(); } else { return; } - $_product = wc_get_product( $product_id ); - if ( is_null( $_product ) || !$_product ) { return; } - $widget_product_data = wc_yotpo_get_product_data( $_product ); - $html = "
"; + if ( isset( $args['product_id'] ) ) { + $product_id = $args['product_id']; + } elseif ( is_product() ) { + global $product; + $product_id = $product->get_id(); + } else { + return; + } + $product_handler = YRFW_Product_Cache::get_instance(); + $widget_product_data = $product_handler->get_cached_product( $product_id ); + $html = "
"; return $html; } + /** + * Show star rating widget + * + * @param array $args product_id, noempty arguments. + * @return string + * @since 1.2.0 + */ public function yotpo_bottomline( $args ) { - $this->basic_check(); - if ( !class_exists( 'Yotpo' ) ) { require_once( ABSPATH . 'wp-content/plugins/yotpo-social-reviews-for-woocommerce/lib/yotpo-api/Yotpo.php' ); } - $yotpo_settings = get_option( 'yotpo_settings' ); + if ( ! class_exists( 'YRFW_API_Wrapper' ) ) { + require_once YRFW_PLUGIN_PATH . 'inc/Helpers/class-yrfw-api-wrapper.php'; + } + if ( ! array_key_exists( 'settings_instance', $GLOBALS ) ) { + $settings_instance = ( YRFW_Settings_File::get_instance() )->get_settings(); + } else { + global $settings_instance; + } if ( isset( $args['product_id'] ) ) { $product_id = $args['product_id']; } elseif ( is_product() ) { @@ -95,29 +127,55 @@ public function yotpo_bottomline( $args ) { } else { return; } - $data = array(); - $data['product_id'] = $product_id; - $data['app_key'] = $yotpo_settings['app_key']; - $_product = wc_get_product( $product_id ); - if ( is_null( $_product ) || !$_product ) { return; } - $yotpo = new Yotpo( $yotpo_settings['app_key'], $yotpo_settings['secret'] ); - $response = $yotpo->get_product_bottom_line( $data ); - if ( !empty( $response ) ) { - if ( $response['status']['code'] && $response['status']['code'] === 200 && $response['response']['bottomline']['total_reviews'] > 0 ) { - $widget_product_data = wc_yotpo_get_product_data( wc_get_product( $product_id ) ); - $html = "
-
"; - } elseif ( !isset( $args['0'] ) || $args['0'] != "noempty" ) { + $curl = YRFW_API_Wrapper::get_instance(); + $curl->init( $settings_instance['app_key'], $settings_instance['secret'] ); + $response = json_decode( $curl->get_product_bottomline( $product_id ) ); + if ( ! empty( $response ) ) { + if ( $response->response->bottomline->total_reviews > 0 ) { + $product_handler = YRFW_Product_Cache::get_instance(); + $widget_product_data = $product_handler->get_cached_product( $product_id ); + $html = "
+
"; + } elseif ( ! isset( $args['0'] ) || 'noempty' !== $args['0'] ) { return $this->show_empty_bottomline(); - } else { return; } + } else { + return; + } } return $html; } - public function show_empty_bottomline() { + /** + * Show questions widget + * + * @param array $args product_id argument. + * @return void | string + * @since 1.2.1 + */ + public function yotpo_questions( $args ) { + $product_id = ''; + if ( isset( $args['product_id'] ) ) { + $product_id = $args['product_id']; + } elseif ( is_product() ) { + global $product; + $product_id = $product->get_id(); + } else { + return; + } + $html = "
"; + return $html; + } + + /** + * Show empty star rating code + * + * @return string + * @since 1.2.0 + */ + private function show_empty_bottomline() { return "
@@ -132,31 +190,49 @@ public function show_empty_bottomline() {
"; } + /** + * Show gallery widget + * + * @param array $args arguments accepts 'gallery_id', 'product_id' and 'noproduct'. + * @return string + * @since 1.2.0 + */ public function yotpo_product_gallery( $args ) { - $this->basic_check(); if ( empty( $args['gallery_id'] ) ) { return 'Error - no gallery ID specified'; } $html = "
basic_check(); - extract( shortcode_atts( array( - 'background_color' => 'transparent', // transparent or #color - 'mode' => 'top_rated', // top_rated or most_recent - 'type' => 'per_product', // per_product, product, both or site - 'count' => '9', // 3-9 - 'show_bottomline' => '1', - 'autoplay_enabled' => '1', - 'autoplay_speed' => '3000', - 'show_navigation' => '1'), $args ) ); + extract( + shortcode_atts( + array( + 'background_color' => 'transparent', // transparent or #color + 'mode' => 'top_rated', // top_rated or most_recent + 'type' => 'per_product', // per_product, product, both or site + 'count' => '9', // 3-9 + 'show_bottomline' => '1', + 'autoplay_enabled' => '1', + 'autoplay_speed' => '3000', + 'show_navigation' => '1', + ), + $args + ) + ); $html = "