Skip to content
Snippets Groups Projects
Commit e5a8505c authored by dyszczo's avatar dyszczo
Browse files

first version

parent 8c79ca7e
No related branches found
No related tags found
No related merge requests found
.git/ export-ignore
tests/ export-ignore
vendor/ export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.gitlab-ci.yml export-ignore
.idea export-ignore
apigen.neon export-ignore
phpcs.xml.dist export-ignore
phpunit-integration.xml export-ignore
phpunit-unit.xml export-ignore
......@@ -2,4 +2,5 @@
.idea
composer.lock
build-coverage
docker-compose.yml
\ No newline at end of file
docker-compose.yml
*.mo
\ No newline at end of file
## [1.0.0] - 2019-11-29
### Added
- First version
\ No newline at end of file
MIT License
Copyright (c) 2018 wpdesk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
[![pipeline status](https://gitlab.com/wpdesk/wp-wpdesk-rating-petition/badges/master/pipeline.svg)](https://gitlab.com/wpdesk/wp-wpdesk-rating-petition/pipelines)
[![coverage report](https://gitlab.com/wpdesk/wp-wpdesk-rating-petition/badges/master/coverage.svg)](https://gitlab.com/wpdesk/wp-wpdesk-rating-petition/commits/master)
[![Latest Stable Version](https://poser.pugx.org/wpdesk/wp-wpdesk-rating-petition/v/stable)](https://packagist.org/packages/wpdesk/wp-wpdesk-rating-petition)
[![Total Downloads](https://poser.pugx.org/wpdesk/wp-wpdesk-rating-petition/downloads)](https://packagist.org/packages/wpdesk/wp-wpdesk-rating-petition)
[![Latest Unstable Version](https://poser.pugx.org/wpdesk/wp-wpdesk-rating-petition/v/unstable)](https://packagist.org/packages/wpdesk/wp-wpdesk-rating-petition)
[![License](https://poser.pugx.org/wpdesk/wp-wpdesk-rating-petition/license)](https://packagist.org/packages/wpdesk/wp-wpdesk-rating-petition)
WordPress Library to ask for good rate in repository.
===================================================
destination: docs
templateConfig: /app/theme-woocommerce/config.neon
extensions: [php]
source:
- src
exclude:
- vendor
- tests
- lang
charset: [UTF-8]
main: Wordpress plugin
title: Plugin template more info
baseUrl: "/"
templateTheme: default
php: false
sourceCode: false
tree: true
deprecated: false
todo: false
download: false
accessLevels:
- public
- private
- protected
\ No newline at end of file
{
"name": "wpdesk/wp-wpdesk-rating-petition",
"authors": [
{
"name": "Krzysiek",
"email": "krzysiek@wpdesk.pl"
}
],
"require": {
"php": ">=5.6",
"wpdesk/wp-notice": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "<7",
"wp-coding-standards/wpcs": "^0.14.1",
"squizlabs/php_codesniffer": "^3.0.2",
"mockery/mockery": "*",
"10up/wp_mock": "*",
"wimg/php-compatibility": "^8"
},
"autoload": {
"psr-4": {
"WPDesk\\RepositoryRating\\": "src/"
}
},
"autoload-dev": {
"classmap": ["tests/"]
},
"extra": {
"text-domain": "wpdesk-rating-petition",
"translations-folder": "lang",
"po-files": {
"pl_PL": "wpdesk-rating-petition-pl_PL.po"
}
},
"scripts": {
"test": "echo composer is alive",
"phpcs": "phpcs",
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage",
"docs": "apigen generate"
}
}
This diff is collapsed.
<phpunit bootstrap="tests/integration/bootstrap.php"
backupGlobals="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
strict="true"
>
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
<php>
<env name="WP_DEVELOP_DIR" value="/tmp/wordpress-develop"/>
<env name="WC_DEVELOP_DIR" value="/tmp/woocommerce"/>
</php>
</phpunit>
\ No newline at end of file
<phpunit bootstrap="tests/unit/bootstrap.php">
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/unit/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
</phpunit>
<?php
/**
* Repository rating.
*
* @package Flexible Shipping Fedex
*/
namespace WPDesk\RepositoryRating;
use WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Can display rating notices based on watcher time time.
*/
class RatingPetitionNotice implements Hookable {
const CLOSE_TEMPORARY_NOTICE = 'close-temporary-notice';
const NOTICES_OFFSET = 1209600; // Two weeks in seconds.
/**
* First notice start time.
*
* @var string
*/
private $first_notice_start_time;
/**
* Second notice start time.
*
* @var string
*/
private $second_notice_start_time;
/**
* Unique namespace for id/option generation
*
* @var string
*/
private $namespace;
/**
* Plugin name in notice content.
*
* @var string
*/
private $plugin_name;
/**
* RatingPetitionNotice constructor.
*
* @param TimeWatcher $method_watcher Watcher to decide when should first notice be displayed.
* @param string $namespace Unique namespace for id/option generation.
* @param string $plugin_name Plugin name in notice content.
*/
public function __construct( TimeWatcher $method_watcher, $namespace, $plugin_name ) {
$this->namespace = $namespace;
$this->plugin_name = $plugin_name;
$this->second_notice_start_time = get_option( $this->prepare_notice_start_time_option_name(), '' );
$this->first_notice_start_time = '';
if ( '' === $this->second_notice_start_time && '' !== $method_watcher->get_creation_time() ) {
$this->first_notice_start_time = gmdate( 'Y-m-d H:i:s', strtotime( $method_watcher->get_creation_time() ) + self::NOTICES_OFFSET );
}
}
/**
* Return option name for second notice timer.
*
* @return string
*/
private function prepare_notice_start_time_option_name() {
return $this->namespace . '_second_notice_time';
}
/**
* Init hooks (actions and filters).
*/
public function hooks() {
add_action( 'admin_notices', array( $this, 'maybe_show_first_notice' ) );
add_action( 'admin_notices', array( $this, 'maybe_show_second_notice' ) );
add_action(
'wpdesk_notice_dismissed_notice',
array( $this, 'maybe_start_second_notice_on_dismiss_first_notice' ),
10,
2
);
}
/**
* Maybe reset counter.
*
* @param string $notice_name .
* @param string $source .
*/
public function maybe_start_second_notice_on_dismiss_first_notice( $notice_name, $source = null ) {
if ( $this->prepare_notice_name( 1 ) === $notice_name && ( empty( $source ) || self::CLOSE_TEMPORARY_NOTICE === $source ) ) {
update_option( $this->prepare_notice_start_time_option_name(), gmdate( 'Y-m-d H:i:s', intval( current_time( 'timestamp' ) ) + self::NOTICES_OFFSET ) );
}
}
/**
* Returns notice name for Notice class.
*
* @param int $notice_number Notice number as there are two notices.
*
* @return string
*/
private function prepare_notice_name( $notice_number ) {
return $this->namespace . '_rating_' . $notice_number;
}
/**
* Maybe show first notice.
*/
public function maybe_show_first_notice() {
if ( $this->should_display_notice() ) {
if ( '' !== $this->first_notice_start_time && current_time( 'mysql' ) > $this->first_notice_start_time ) {
$this->show_notice( $this->prepare_notice_name( 1 ) );
}
}
}
/**
* Should display notice.
*
* @return bool
*/
private function should_display_notice() {
$current_screen = get_current_screen();
$display_on_screens = [ 'shop_order', 'edit-shop_order', 'woocommerce_page_wc-settings' ];
if ( ! empty( $current_screen ) && in_array( $current_screen->id, $display_on_screens, true ) ) {
return true;
}
return false;
}
/**
* Show notice.
*
* @param string $notice_name .
*/
private function show_notice( $notice_name ) {
new \WPDesk\Notice\PermanentDismissibleNotice(
$this->get_notice_content(),
$notice_name,
\WPDesk\Notice\Notice::NOTICE_TYPE_INFO
);
}
/**
* Get notice content.
*
* @return string
*/
private function get_notice_content() {
// Translators: plugin name.
$content = sprintf( __( 'Awesome, you\'ve been using %s for more than 2 weeks. Could you please do me a BIG favor and give it a 5-star rating on WordPress? ~ Peter', 'wpdesk-rating-petition' ), $this->plugin_name );
$content .= '<br/>';
$content .= implode( ' | ', $this->action_links() );
return $content;
}
/**
* Action links
*
* @return array
*/
protected function action_links() {
$actions[] = sprintf( // phpcs:ignore
// Translators: link.
__( '%1$sOk, you deserved it%2$s', 'wpdesk-rating-petition' ),
'<a class="fs-response-deserved" target="_blank" href="' . esc_url( 'https://wordpress.org/plugins/wpdesk-rating-petition/#reviews' ) . '">',
'</a>'
);
$actions[] = sprintf(
// Translators: link.
__( '%1$sNope, maybe later%2$s', 'wpdesk-rating-petition' ),
'<a class="fs-response-close-temporary-notice notice-dismiss-link" data-source="' . self::CLOSE_TEMPORARY_NOTICE . '" href="#">',
'</a>'
);
$actions[] = sprintf(
// Translators: link.
__( '%1$sI already did%2$s', 'wpdesk-rating-petition' ),
'<a class="fs-response-already-did notice-dismiss-link" data-source="already-did" href="#">',
'</a>'
);
return $actions;
}
/**
* Maybe show second notice.
*/
public function maybe_show_second_notice() {
if ( $this->should_display_notice() ) {
if ( ( '' !== $this->second_notice_start_time ) && ( current_time( 'mysql' ) > $this->second_notice_start_time ) ) {
$this->show_notice( $this->prepare_notice_name( 2 ) );
}
}
}
}
<?php
/**
* TimeWatcher interface.
*
* @package WPDesk\RepositoryRating
*/
namespace WPDesk\RepositoryRating;
/**
* Simple interface for tracking creation time of certaing things.
*
* @package WPDesk\RepositoryRating
*/
interface TimeWatcher {
/**
* Return creation time of watched item.
*
* @return string Returns date string. Can also return '' when no creation time is found.
*/
public function get_creation_time();
}
<?php
/**
* Shipping method watcher.
*
* @package Flexible Shipping Fedex
*/
namespace WPDesk\RepositoryRating\TimeWatcher;
use WPDesk\PluginBuilder\Plugin\Hookable;
use WPDesk\RepositoryRating\TimeWatcher;
/**
* Can watch shipping method creation.
*/
class ShippingMethodGlobalSettingsWatcher implements Hookable, TimeWatcher {
const NO_CREATION_TIME = '';
/**
* Unique id of shipping method used in WC_Shipping_Method::id.
*
* @var string
*/
private $shipping_method_unique_id;
/**
* ShippingMethodGlobalSettingsWatcher constructor.
*
* @param string $shipping_method_unique_id Unique id of shipping method used in WC_Shipping_Method::id.
*/
public function __construct( $shipping_method_unique_id ) {
$this->shipping_method_unique_id = $shipping_method_unique_id;
}
/**
* Init hooks (actions and filters).
*/
public function hooks() {
add_action(
"woocommerce_update_options_shipping_{$this->shipping_method_unique_id}", function () {
$this->watch_saved_settings();
}
);
}
/**
* Watch saved settings and save time when first saved.
*/
private function watch_saved_settings() {
if ( ! $this->is_creation_time_saved() ) {
$this->update_creation_time();
}
}
/**
* Is creation time saved or it's first time?.
*
* @return bool
*/
private function is_creation_time_saved() {
return $this->get_creation_time() !== self::NO_CREATION_TIME;
}
/**
* Set ccreation time to now.
*/
private function update_creation_time() {
update_option( $this->prepare_creation_time_option_name(), current_time( 'mysql' ) );
}
/**
* Option name to save time in db.
*
* @return string
*/
private function prepare_creation_time_option_name() {
return $this->shipping_method_unique_id . '_repository_creation_time';
}
/**
* Get first method creation time.
*
* @return string
*/
public function get_creation_time() {
return get_option( $this->prepare_creation_time_option_name(), self::NO_CREATION_TIME );
}
}
<?php
/**
* Shipping method watcher.
*
* @package Flexible Shipping Fedex
*/
namespace WPDesk\RepositoryRating\TimeWatcher;
use WPDesk\PluginBuilder\Plugin\Hookable;
use WPDesk\RepositoryRating\TimeWatcher;
/**
* Can watch shipping method creation.
*
* @todo This class is not tested yet. Should be used in UPS.
*/
class ShippingMethodInstanceWatcher implements Hookable, TimeWatcher {
/**
* First method added time.
*
* @var string
*/
private $first_method_creation_time = '';
/**
* First method watching.
*
* @var int
*/
private $first_method_watching = 0;
/**
* Unique namespace for id/option generation
*
* @var string
*/
private $namespace;
/**
* Name of shipping method ::class
*
* @var string
*/
private $shipping_method_name;
/**
* Name of WP option with plugin activation time
*
* @var string
*/
private $plugin_activation_time_option_name;
/**
* Date in string when functionality started and we can start watching
*
* @var string
*/
private $zero_date;
/**
* ShippingMethodInstanceWatcher constructor.
*
* @param string $namespace Unique namespace for id/option generation.
* @param string $plugin_activation_time_option_name Where in options is plugin activation time.
* @param string $zero_date When functionality started and we can start watching.
* @param string $shipping_method_name What method name is watched.
*/
public function __construct( $namespace, $plugin_activation_time_option_name, $zero_date, $shipping_method_name ) {
$this->namespace = $namespace;
$this->plugin_activation_time_option_name = $plugin_activation_time_option_name;
$this->zero_date = $zero_date;
$this->shipping_method_name = $shipping_method_name;
}
/**
* Init hooks (actions and filters).
*/
public function hooks() {
add_action( 'admin_init', array( $this, 'maybe_init_watching' ), 10, 3 );
add_action( 'woocommerce_shipping_zone_method_added', array( $this, 'watch_added_shipping_method' ), 10, 3 );
}
/**
* Init watching.
*/
public function maybe_init_watching() {
$this->first_method_watching = intval( get_option( $this->prepare_option_name_watching(), 0 ) );
if ( 0 === $this->first_method_watching ) {
$ups_free_activation_time = get_option( $this->plugin_activation_time_option_name, current_time( 'mysql' ) );
if ( strtotime( $ups_free_activation_time ) < strtotime( $this->zero_date ) ) {
$this->init_watching_from_existing_shipping_methods();
}
update_option( $this->prepare_option_name_watching(), 1 );
$this->first_method_watching = 1;
}
}
/**
* Returns options name for no idea what.
*
* @return string
*/
private function prepare_option_name_watching() {
return $this->namespace . '_method_watching';
}
/**
* First time init watching.
* Sets first time to current time when Fedex shipping method already exists.
*/
private function init_watching_from_existing_shipping_methods() {
$shipping_zones = \WC_Shipping_Zones::get_zones();
/** @var \WC_Shipping_Zone $shipping_zone */ // phpcs:ignore
foreach ( $shipping_zones as $shipping_zone_data ) {
$shipping_zone = \WC_Shipping_Zones::get_zone( $shipping_zone_data['id'] );
if ( $shipping_zone && $shipping_zone instanceof \WC_Shipping_Zone ) {
$shipping_methods = $shipping_zone->get_shipping_methods();
foreach ( $shipping_methods as $shipping_method ) {
if ( $shipping_method instanceof $this->shipping_method_name ) {
update_option( $this->prepare_option_name_method_created(), current_time( 'mysql' ) );
}
}
}
}
}
/**
* Returns options name for info when shipping method was created.
*
* @return string
*/
private function prepare_option_name_method_created() {
return $this->namespace . '_method_created';
}
/**
* Watch added shipping method.
* Set first time to current time when first UPS shipping method created.
*
* @param int $instance_id .
* @param string $type .
* @param int $zone_id .
*/
public function watch_added_shipping_method( $instance_id, $type, $zone_id ) {
$this->first_method_creation_time = get_option( $this->prepare_option_name_method_created(), '' );
if ( $this->namespace === $type ) {
if ( '' === $this->first_method_creation_time ) {
$this->first_method_creation_time = (string) current_time( 'mysql' );
update_option( $this->prepare_option_name_method_created(), $this->first_method_creation_time );
}
}
}
/**
* Get first method creation time.
*
* @return string
*/
public function get_creation_time() {
$this->first_method_creation_time = get_option( $this->prepare_option_name_method_created(), '' );
return $this->first_method_creation_time;
}
/**
* Returns date when functionality started and we can start watching.
*
* @return string
*/
private function get_watcher_zero_date() {
return $this->zero_date;
}
}
<?php
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
require_once __DIR__ . '/../../vendor/autoload.php';
// disable xdebug backtrace
if ( function_exists( 'xdebug_disable' ) ) {
xdebug_disable();
}
if ( getenv( 'PLUGIN_PATH' ) !== false ) {
define( 'PLUGIN_PATH', getenv( 'PLUGIN_PATH' ) );
} else {
define( 'PLUGIN_PATH', __DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR );
}
require_once( getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit/includes/functions.php' );
tests_add_filter( 'muplugins_loaded', function () {
}, 100 );
putenv('WP_TESTS_DIR=' . getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit');
require_once( getenv( 'WC_DEVELOP_DIR' ) . '/tests/bootstrap.php' );
\ No newline at end of file
<?php
/**
* PHPUnit bootstrap file
*/
require_once __DIR__ . '/../../vendor/autoload.php';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment