diff --git a/CHANGELOG.md b/CHANGELOG.md index d78b275c73934ee73f07eadf0400744d73fbcc1a..d564d5f9a93faa58153d678ddf3393f9a1f18213 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 0000000000000000000000000000000000000000..3a35acbae01e4aa9c77e5246f8cbfbb87a555905 --- /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 12d200576a9572b9246434e223fb244611403551..bb88d1d30e97b0ab780050363bbe90bb41f7643d 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 0000000000000000000000000000000000000000..7d40d228ae550a66217f7697bbb63f2f14edd510 --- /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 0000000000000000000000000000000000000000..2198eec0c396589ae7eb685fb3d4f804dce7b907 --- /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 0000000000000000000000000000000000000000..a48fc439d5c1b4c7e244651185bec3e23af94512 --- /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 0000000000000000000000000000000000000000..2a12fe6759569388ae9c53bec31138589061c233 --- /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 0000000000000000000000000000000000000000..8026d6c24b50d69a8cb449c5bc12dfe5e480d990 --- /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 0000000000000000000000000000000000000000..9a206aa22b57892f127d92e1e6dd58d9cf719ad9 --- /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 0000000000000000000000000000000000000000..258f3aa4c422e54c065cad76d65dbb1c8619d6e4 --- /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 0000000000000000000000000000000000000000..b798059bd9fe8185e2a1f7363567bbfa752806c2 --- /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">♥</span>', + $plugin_author, + '<span class="plugin-title">' . $plugin_title . '</span>', + '<a href="' . $rating_url . '" target="_blank">', + '<span class="star">★★★★★</span>', + '</a>' + ) + ); ?> +</div> \ No newline at end of file