From c484c8ec95d2e7ebe6b4f712465294e335b51295 Mon Sep 17 00:00:00 2001
From: Grzegorz Rola <grola@seostudio.pl>
Date: Mon, 24 Jan 2022 10:50:16 +0000
Subject: [PATCH] feature(petition): added text petition

---
 CHANGELOG.md                                  |  4 ++
 assets/css/style.css                          | 39 +++++++++++
 composer.json                                 |  5 +-
 .../AlwaysDisplayDisplayDecision.php          | 20 ++++++
 src/DisplayStrategy/DisplayDecision.php       | 17 +++++
 .../GetParametersDisplayDecision.php          | 46 +++++++++++++
 .../ShippingMethodDisplayDecision.php         | 60 +++++++++++++++++
 src/PetitionText.php                          | 17 +++++
 src/RepositoryRatingPetitionText.php          | 53 +++++++++++++++
 src/TextPetitionDisplayer.php                 | 64 +++++++++++++++++++
 src/views/html-text-petition.php              | 20 ++++++
 11 files changed, 343 insertions(+), 2 deletions(-)
 create mode 100644 assets/css/style.css
 create mode 100644 src/DisplayStrategy/AlwaysDisplayDisplayDecision.php
 create mode 100644 src/DisplayStrategy/DisplayDecision.php
 create mode 100644 src/DisplayStrategy/GetParametersDisplayDecision.php
 create mode 100644 src/DisplayStrategy/ShippingMethodDisplayDecision.php
 create mode 100644 src/PetitionText.php
 create mode 100644 src/RepositoryRatingPetitionText.php
 create mode 100644 src/TextPetitionDisplayer.php
 create mode 100644 src/views/html-text-petition.php

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d78b275..d564d5f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## [1.2.0] - 2022-01-24
+### Changes
+- Added TextPetition
+
 ## [1.1.0] - 2019-12-02
 ### Changes
 - RatingPetitionNotice can get url to redirect user when wants to rate
diff --git a/assets/css/style.css b/assets/css/style.css
new file mode 100644
index 0000000..3a35acb
--- /dev/null
+++ b/assets/css/style.css
@@ -0,0 +1,39 @@
+.wpdesk-rating-petition {
+    font-size: 15px;
+    width: 100%;
+}
+
+.wpdesk-rating-petition a {
+    text-decoration: none;
+    color: inherit;
+    font-weight: 600;
+}
+
+.wpdesk-rating-petition span.plugin-title {
+    font-weight: 600;
+}
+
+.wpdesk-rating-petition span.heart {
+    speak: none;
+    -webkit-font-smoothing: antialiased;
+    -moz-osx-font-smoothing: grayscale;
+    color: #dc3232;
+    font-size: 16px;
+    font-style: normal;
+    font-variant: normal;
+    font-weight: 400;
+    text-transform: none;
+}
+
+.wpdesk-rating-petition span.star {
+    speak: none;
+    -webkit-font-smoothing: antialiased;
+    -moz-osx-font-smoothing: grayscale;
+    color: #ffb900;
+    font-size: 16px;
+    font-style: normal;
+    font-variant: normal;
+    font-weight: 400;
+    text-transform: none;
+    text-decoration: none;
+}
diff --git a/composer.json b/composer.json
index 12d2005..bb88d1d 100644
--- a/composer.json
+++ b/composer.json
@@ -7,8 +7,9 @@
         }
     ],
     "require": {
-        "php": ">=5.6",
-        "wpdesk/wp-notice": "^3.1"
+        "php": ">=7.0",
+        "wpdesk/wp-notice": "^3.1",
+        "wpdesk/wp-plugin-flow": "^3.1"
     },
     "require-dev": {
         "phpunit/phpunit": "<7",
diff --git a/src/DisplayStrategy/AlwaysDisplayDisplayDecision.php b/src/DisplayStrategy/AlwaysDisplayDisplayDecision.php
new file mode 100644
index 0000000..7d40d22
--- /dev/null
+++ b/src/DisplayStrategy/AlwaysDisplayDisplayDecision.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace WPDesk\RepositoryRating\DisplayStrategy;
+
+/**
+ * DisplayDecision - always display.
+ */
+class AlwaysDisplayDisplayDecision implements DisplayDecision {
+
+	/**
+	 * Should display?
+	 *
+	 * @return bool
+	 */
+	public function should_display(): bool {
+
+		return true;
+	}
+
+}
\ No newline at end of file
diff --git a/src/DisplayStrategy/DisplayDecision.php b/src/DisplayStrategy/DisplayDecision.php
new file mode 100644
index 0000000..2198eec
--- /dev/null
+++ b/src/DisplayStrategy/DisplayDecision.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace WPDesk\RepositoryRating\DisplayStrategy;
+
+/**
+ * ShouldDisplay interface.
+ */
+interface DisplayDecision {
+
+	/**
+	 * Returns true when element should be displayed.
+	 *
+	 * @return bool
+	 */
+	public function should_display(): bool;
+
+}
\ No newline at end of file
diff --git a/src/DisplayStrategy/GetParametersDisplayDecision.php b/src/DisplayStrategy/GetParametersDisplayDecision.php
new file mode 100644
index 0000000..a48fc43
--- /dev/null
+++ b/src/DisplayStrategy/GetParametersDisplayDecision.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace WPDesk\RepositoryRating\DisplayStrategy;
+
+/**
+ * DisplayDecision based on GET parameters.
+ */
+class GetParametersDisplayDecision implements DisplayDecision {
+
+	/**
+	 * Whether to show beacon on the page or not. Array of arrays with condition for _GET.
+	 * Inner arrays mean AND, outer arrays mean OR conditions.
+	 *
+	 * ie. [ [ ... and ... and ... ] or [ ... and ... and ... ] or ... ]
+	 *
+	 * @var array
+	 */
+	private $conditions;
+
+	public function __construct( array $conditions ) {
+		$this->conditions = $conditions;
+	}
+
+	/**
+	 * Should display?
+	 *
+	 * @return bool
+	 */
+	public function should_display(): bool {
+		foreach ( $this->conditions as $or_conditions ) {
+			$display = true;
+			foreach ( $or_conditions as $parameter => $value ) {
+				if ( ! isset( $_GET[ $parameter ] ) || $_GET[ $parameter ] !== $value ) {
+					$display = false;
+				}
+			}
+			if ( $display ) {
+
+				return $display;
+			}
+		}
+
+		return false;
+	}
+
+}
\ No newline at end of file
diff --git a/src/DisplayStrategy/ShippingMethodDisplayDecision.php b/src/DisplayStrategy/ShippingMethodDisplayDecision.php
new file mode 100644
index 0000000..2a12fe6
--- /dev/null
+++ b/src/DisplayStrategy/ShippingMethodDisplayDecision.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace WPDesk\RepositoryRating\DisplayStrategy;
+
+class ShippingMethodDisplayDecision implements DisplayDecision {
+
+	/**
+	 * @var \WC_Shipping_Zones
+	 */
+	private $shipping_zones;
+
+	/**
+	 * @var string
+	 */
+	private $shipping_method_id;
+
+	/**
+	 * @param \WC_Shipping_Zones $shipping_zones
+	 * @param string $shipping_method_id
+	 */
+	public function __construct( \WC_Shipping_Zones $shipping_zones, string $shipping_method_id ) {
+		$this->shipping_zones     = $shipping_zones;
+		$this->shipping_method_id = $shipping_method_id;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	public function should_display(): bool {
+		if ( $this->is_in_shipping_settings() ) {
+			if ( $this->is_get_parameter_with_value( 'section', $this->shipping_method_id ) ) {
+
+				return true;
+			}
+
+			if ( isset( $_GET['instance_id'] ) ) {
+				$shipping_method = $this->shipping_zones::get_shipping_method( sanitize_key( $_GET['instance_id'] ) );
+				if ( $shipping_method instanceof \WC_Shipping_Method ) {
+
+					return $shipping_method->id === $this->shipping_method_id;
+				}
+			}
+		}
+
+		return false;
+	}
+
+	private function is_in_shipping_settings(): bool {
+		if ( $this->is_get_parameter_with_value( 'page', 'wc-settings' ) && $this->is_get_parameter_with_value( 'tab', 'shipping' ) ) {
+			return true;
+		}
+
+		return false;
+	}
+
+	private function is_get_parameter_with_value( string $parameter, string $value ): bool {
+		return isset( $_GET[ $parameter ] ) && $_GET[ $parameter ] === $value;
+	}
+
+}
\ No newline at end of file
diff --git a/src/PetitionText.php b/src/PetitionText.php
new file mode 100644
index 0000000..8026d6c
--- /dev/null
+++ b/src/PetitionText.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace WPDesk\RepositoryRating;
+
+/**
+ * Petition text generator.
+ */
+interface PetitionText {
+
+	/**
+	 * Returns petition text.
+	 *
+	 * @return string
+	 */
+	public function get_petition_text(): string;
+
+}
\ No newline at end of file
diff --git a/src/RepositoryRatingPetitionText.php b/src/RepositoryRatingPetitionText.php
new file mode 100644
index 0000000..9a206aa
--- /dev/null
+++ b/src/RepositoryRatingPetitionText.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace WPDesk\RepositoryRating;
+
+class RepositoryRatingPetitionText implements PetitionText {
+
+	/**
+	 * @var string
+	 */
+	private $plugin_author;
+
+	/**
+	 * @var string
+	 */
+	private $plugin_title;
+
+	/**
+	 * @var string
+	 */
+	private $rating_url;
+
+	/**
+	 * @var string
+	 */
+	private $text_align;
+
+	/**
+	 * @param string $plugin_author
+	 * @param string $plugin_title
+	 * @param string $rating_url
+	 * @param string $text_align
+	 */
+	public function __construct( string $plugin_author, string $plugin_title, string $rating_url, string $text_align ) {
+		$this->plugin_author = $plugin_author;
+		$this->plugin_title  = $plugin_title;
+		$this->rating_url    = $rating_url;
+		$this->text_align    = $text_align;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	public function get_petition_text(): string {
+		ob_start();
+		$plugin_author = $this->plugin_author;
+		$plugin_title = $this->plugin_title;
+		$rating_url = $this->rating_url;
+		$text_align = $this->text_align;
+		include __DIR__ . '/views/html-text-petition.php';
+
+		return ob_get_clean();
+	}
+}
\ No newline at end of file
diff --git a/src/TextPetitionDisplayer.php b/src/TextPetitionDisplayer.php
new file mode 100644
index 0000000..258f3aa
--- /dev/null
+++ b/src/TextPetitionDisplayer.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace WPDesk\RepositoryRating;
+
+use WPDesk\PluginBuilder\Plugin\Hookable;
+use WPDesk\RepositoryRating\DisplayStrategy\DisplayDecision;
+
+/**
+ * Can display text petition.
+ */
+class TextPetitionDisplayer implements Hookable {
+
+	const SCRIPTS_VERSION = '2';
+
+	/**
+	 * @var string
+	 */
+	private $display_on_action;
+
+	/**
+	 * @var DisplayDecision
+	 */
+	private $display_decision;
+
+	/**
+	 * @var PetitionText
+	 */
+	private $petition_text_generator;
+
+	/**
+	 * @param string $display_on_action
+	 * @param DisplayDecision $display_decision
+	 * @param PetitionText $petition_text_generator
+	 */
+	public function __construct( string $display_on_action, DisplayDecision $display_decision, PetitionText $petition_text_generator ) {
+		$this->display_on_action       = $display_on_action;
+		$this->display_decision        = $display_decision;
+		$this->petition_text_generator = $petition_text_generator;
+	}
+
+	public function hooks() {
+		add_action( $this->display_on_action, [ $this, 'display_petition_if_should' ] );
+		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_css_if_should' ] );
+	}
+
+	/**
+	 * @internal
+	 */
+	public function enqueue_css_if_should() {
+		if ( $this->display_decision->should_display() ) {
+			wp_enqueue_style( 'wpdesk-rating-petition', plugin_dir_url( __DIR__ . '/../assets/css/style.css' ) . 'style.css', array(), self::SCRIPTS_VERSION );
+		}
+	}
+
+	/**
+	 * @internal
+	 */
+	public function display_petition_if_should() {
+		if ( $this->display_decision->should_display() ) {
+			echo wp_kses_post( $this->petition_text_generator->get_petition_text() );
+		}
+	}
+
+}
diff --git a/src/views/html-text-petition.php b/src/views/html-text-petition.php
new file mode 100644
index 0000000..b798059
--- /dev/null
+++ b/src/views/html-text-petition.php
@@ -0,0 +1,20 @@
+<?php
+/**
+ * @var string $text_align
+ * @var string $plugin_author
+ * @var string $plugin_title
+ * @var string $rating_url
+ */
+?><div class="wpdesk-rating-petition" style="text-align: <?php echo esc_attr( $text_align ); ?>;">
+	<?php echo wp_kses_post(
+        sprintf(
+            __( 'Created with %1$s by %2$s - If you like %3$s you can %4$srate us %5$s in plugins repository%6$s', 'wp-wpdesk-rating-petition' ),
+            '<span class="heart">&hearts;</span>',
+            $plugin_author,
+            '<span class="plugin-title">' . $plugin_title . '</span>',
+            '<a href="' . $rating_url . '" target="_blank">',
+            '<span class="star">&#9733;&#9733;&#9733;&#9733;&#9733;</span>',
+            '</a>'
+        )
+    ); ?>
+</div>
\ No newline at end of file
-- 
GitLab