From 6b5cd597450634b38ac29b112e69bed149d00a31 Mon Sep 17 00:00:00 2001
From: Grzegorz Rola <grola@seostudio.pl>
Date: Sun, 23 Jan 2022 12:13:49 +0100
Subject: [PATCH] feature(petition): added text petition

---
 src/DisplayStrategy/DisplayDecision.php       |  2 +-
 .../ShippingMethodDisplayDecision.php         | 60 ++++++++++++++++
 src/TextPetition.php                          | 26 ++++++-
 src/views/html-text-petition.php              | 69 +++++++++++++++----
 4 files changed, 141 insertions(+), 16 deletions(-)
 create mode 100644 src/DisplayStrategy/ShippingMethodDisplayDecision.php

diff --git a/src/DisplayStrategy/DisplayDecision.php b/src/DisplayStrategy/DisplayDecision.php
index c12d45f..2198eec 100644
--- a/src/DisplayStrategy/DisplayDecision.php
+++ b/src/DisplayStrategy/DisplayDecision.php
@@ -12,6 +12,6 @@ interface DisplayDecision {
 	 *
 	 * @return bool
 	 */
-	public function should_display();
+	public function should_display(): bool;
 
 }
\ 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/TextPetition.php b/src/TextPetition.php
index 1904729..fdda3fc 100644
--- a/src/TextPetition.php
+++ b/src/TextPetition.php
@@ -20,6 +20,27 @@ class TextPetition implements Hookable {
 	 */
 	private $display_decision;
 
+
+	/**
+	 * @var string
+	 */
+	private $text_align;
+
+	/**
+	 * @var string
+	 */
+	private $plugin_author;
+
+	/**
+	 * @var string
+	 */
+	private $plugin_title;
+
+	/**
+	 * @var string
+	 */
+    private $rating_url;
+
 	/**
 	 * @param string $display_on_action
 	 * @param DisplayDecision $display_decision
@@ -29,15 +50,14 @@ class TextPetition implements Hookable {
 		$this->display_decision  = $display_decision;
 	}
 
-
 	public function hooks() {
 		add_action( $this->display_on_action, [ $this, 'display_petition_if_should' ] );
 	}
 
 	public function display_petition_if_should() {
 		if ( $this->display_decision->should_display() ) {
-
+			include __DIR__ . '/views/html-text-petition.php';
 		}
 	}
 
-}
\ No newline at end of file
+}
diff --git a/src/views/html-text-petition.php b/src/views/html-text-petition.php
index 18e89ed..d4b54f7 100644
--- a/src/views/html-text-petition.php
+++ b/src/views/html-text-petition.php
@@ -1,15 +1,60 @@
 <?php
+/**
+ * @var string $text_align
+ * @var string $plugin_author
+ * @var string $plugin_title
+ * @var string $rating_url
+ */
+?><style>
+    .wpdesk-rating-petition {
+        font-size: 15px;
+        text-align: <?php echo esc_attr( $text_align ); ?>;
+        width: 100%;
+    }
 
-?><div class="aligncenter">
-	Created with
-	<span class="fcfSettings__footerIcon fcfSettings__footerIcon--heart"></span>
-	by Rangers from <a href="https://wpde.sk/fcf-settings-footer-wpdesk-link-pl" target="_blank">WP Desk</a>
-	- if you like FCF
-	<a href="https://wpde.sk/fcf-settings-footer-review-link-pl" target="_blank">rate us
-		<span class="fcfSettings__footerIcon fcfSettings__footerIcon--star"></span>
-		<span class="fcfSettings__footerIcon fcfSettings__footerIcon--star"></span>
-		<span class="fcfSettings__footerIcon fcfSettings__footerIcon--star"></span>
-		<span class="fcfSettings__footerIcon fcfSettings__footerIcon--star"></span>
-		<span class="fcfSettings__footerIcon fcfSettings__footerIcon--star"></span>
-	</a>
+    .wpdesk-rating-petition a {
+        text-decoration: none;
+        color: inherit;
+    }
+
+    .wpdesk-rating-petition span.heart:before {
+        speak: none;
+        -webkit-font-smoothing: antialiased;
+        -moz-osx-font-smoothing: grayscale;
+        color: #dc3232;
+        font-size: 16px;
+        font-family: "dashicons";
+        content: "\f487";
+        font-style: normal;
+        font-variant: normal;
+        font-weight: 400;
+        text-transform: none;
+    }
+    .wpdesk-rating-petition span.star:before {
+        speak: none;
+        -webkit-font-smoothing: antialiased;
+        -moz-osx-font-smoothing: grayscale;
+        color: #ffb900;
+        font-size: 16px;
+        font-family: "dashicons";
+        content: "\f155";
+        font-style: normal;
+        font-variant: normal;
+        font-weight: 400;
+        text-transform: none;
+        text-decoration: none;
+    }
+</style>
+<div class="wpdesk-rating-petition">
+	<?php echo wp_kses_post(
+        sprintf(
+            __( 'Created with %1$s by %2$s - If you like %3$s %4$srate us %5$s%6$s', 'wp-wpdesk-rating-petition' ),
+            '<span class="heart"></span>',
+            $plugin_author,
+            $plugin_title,
+            '<a href="' . $rating_url . '" target="_blank">',
+            '<span class="star"></span><span class="star"></span><span class="star"></span><span class="star"></span><span class="star"></span>',
+            '</a>'
+        )
+    ); ?>
 </div>
\ No newline at end of file
-- 
GitLab