diff --git a/.gitignore b/.gitignore index 5e55e37cdaf8d535a3ded8dabacb847b3daa74db..42e837a184d13a9f374155ec15ba7c8f14e64cae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ .idea -composer.lock \ No newline at end of file +composer.lock +build-coverage \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 97023aa7cc1943a9985c3f2b6e9842974fe72759..2d2d987ece12ce7c2b9add7b54428497da9394b7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,5 @@ variables: - WPDESK_CI_VERSION: 1.10.19 + WPDESK_CI_VERSION: 1.10.19-library MYSQL_ROOT_PASSWORD: mysql MYSQL_DATABASE: wptest MYSQL_USER: mysql @@ -13,8 +13,6 @@ variables: stages: - tools - tests - - pre-deploy - - deploy .template: &job-test-template stage: tests @@ -78,18 +76,6 @@ stages: - tags only: -.template: &job-deploy-template - image: wpdesknet/amazon-svn-deploy - stage: deploy - dependencies: - - build to deploy - - unit test lastest coverage - - integration test lastest coverage - retry: 2 - when: manual - only: - - tags - before_script: - cd ${CI_PROJECT_DIR} @@ -119,16 +105,7 @@ churn metrics: - echo ${WPDESK_CI_VERSION} - composer require bmitch/churn-php - composer update --no-progress - - vendor/bin/churn run classes inc - -#code style test: -# stage: tests -# image: wpdesknet/phpunit-woocommerce:0-0 -# allow_failure: true -# script: -# - echo ${WPDESK_CI_VERSION} -# - composer update --no-progress -# - vendor/bin/phpcs + - vendor/bin/churn run src unit test lastest: <<: *job-test-unit-template-fast @@ -175,136 +152,3 @@ integration test current woocommerce: - rm -rf woocommerce - git clone https://github.com/woocommerce/woocommerce.git - cd ${CI_PROJECT_DIR} - -acceptance test: - image: wpdesknet/node - variables: - CI_DEBUG_SERVICES: trace - services: - - name: mysql:5.6 - alias: mysqltests - - name: wpdesknet/apache-woocommerce:latest - alias: wootests - - name: selenium/standalone-chrome - alias: selenium - artifacts: - when: always - expire_in: 1 day - name: "error logs" - paths: - - ${CI_PROJECT_DIR}/acceptance - stage: tests - allow_failure: true - script: - - cd ${CI_PROJECT_DIR} - - composer update --no-progress --no-dev - - if [[ -f ${CI_PROJECT_DIR}/tests/acceptance/prepare.sh ]]; then sh ${CI_PROJECT_DIR}/tests/acceptance/prepare.sh; fi - - - export MYSQL_IP=$(awk '/^[[:space:]]*($|#)/{next} /mysqltests/{print $1; exit}' /etc/hosts) - - export WOOTESTS_IP=$(awk '/^[[:space:]]*($|#)/{next} /wootests/{print $1; exit}' /etc/hosts) - - echo "http://wootests/wpdesk_init.php?mysql_ip=${MYSQL_IP}&wootests_ip=${WOOTESTS_IP}" - - wget -O /tmp/wpdesk_init.txt "http://wootests/wpdesk_init.php?mysql_ip=${MYSQL_IP}&wootests_ip=${WOOTESTS_IP}" - - tail -50 /tmp/wpdesk_init.txt - - - sh /tmp/clone.sh git@gitlab.com:wpdesk/plugins-tests.git /tmp/tests headless - - cd /tmp/tests - - npm install - - npm install -g grunt-cli - - cd node_modules/.bin - - ./webdriver-manager update - - cd ../ - - mkdir -p ${CI_PROJECT_DIR}/acceptance/reports/html/screenshot - - grunt chrome-${CI_PROJECT_NAME} - - -apigen docs: - image: - name: wpdesknet/apigen - stage: pre-deploy - artifacts: - expire_in: 1 day - name: "docs" - paths: - - docs/ - script: - - echo ${WPDESK_CI_VERSION} - - ls -l - - /app/vendor/bin/apigen generate - - php /app/hooks-docs.php ${CI_PROJECT_DIR} - only: - - tags - -pages: - stage: deploy - dependencies: - - apigen docs - script: - - rm -rf public - - mv docs/ public/ - - 'curl -X POST --data-urlencode "payload={\"text\": \"Dokumentacja projektu ${CI_PROJECT_NAME} w wersji ${CI_COMMIT_REF_NAME} umieszczona w <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}/pages|pages> \", }" https://hooks.slack.com/services/${SLACK_AUTH}' - artifacts: - expire_in: 1 day - paths: - - public - only: - - tags - -build to deploy: - image: wpdesknet/phpunit-woocommerce:4-0 - stage: pre-deploy - artifacts: - expire_in: 1 month - name: "production release" - paths: - - release - - release.zip - script: - - echo ${WPDESK_CI_VERSION} - - php --version - - ls -l - - /tmp/set_version.sh ${CI_COMMIT_REF_NAME} - - rm -rf ${CI_PROJECT_DIR}/release ${CI_PROJECT_DIR}/release.zip /tmp/release - - mkdir /tmp/release - - mkdir -p ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} - - cp -rf ${CI_PROJECT_DIR}/* /tmp/release - - cp -rf /tmp/release/* ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} - - cd ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} - - composer install --no-dev --no-progress --optimize-autoloader - - rm -rf build-coverage release tests docs .git .editorconfig .gitignore .gitlab-ci.yml apigen.neon phpunit.xml acceptance test_soap.php .gitlab - - rm -rf composer.json composer.lock phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar - - cd ../ - - zip -r -q ../release.zip ./ - only: - - tags - -deploy to shop: - <<: *job-deploy-template - script: - - echo ${WPDESK_CI_VERSION} - - /tmp/deploy_shop.sh ${CI_PROJECT_NAME} release.zip ${CI_PROJECT_NAME}.zip - - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do sklepu w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}' - environment: - name: wpdesk shop - url: https://wpdeskplugin.s3.amazonaws.com/${CI_PROJECT_NAME}.zip - -deploy to demo: - <<: *job-deploy-template - script: - - echo ${WPDESK_CI_VERSION} - - /tmp/deploy_demo.sh release/${CI_PROJECT_NAME} ${CI_PROJECT_NAME} - - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do demo w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}' - environment: - name: wpdesk demo - url: https://demo.wpdesk.org - -deploy to repository: - <<: *job-deploy-template - script: - - echo ${WPDESK_CI_VERSION} - - rm -rf /tmp/svn-repository - - mkdir /tmp/svn-repository - - /tmp/deploy_repository.sh ${CI_PROJECT_NAME} ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} /tmp/svn-repository - - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do repozytorium WP w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}' - environment: - name: wordpress repository - url: https://downloads.wordpress.org/plugin/${CI_PROJECT_NAME}.${CI_COMMIT_REF_NAME}.zip diff --git a/.gitlab/issue_templates/Bug.md b/.gitlab/issue_templates/Bug.md deleted file mode 100644 index fb98b9efbe4725333c759257cb4692a543cccee8..0000000000000000000000000000000000000000 --- a/.gitlab/issue_templates/Bug.md +++ /dev/null @@ -1,32 +0,0 @@ -### Select WooCommerce version e.g: [x] 2.6 -<!--- Select the WooCommerce version on which the error occurred --> - -- [ ] [2.6](http://woo26.tests.wpdesk.org) -- [ ] [2.7](http://woo27.tests.wpdesk.org) -- [ ] [3.0](http://woo30.tests.wpdesk.org) -- [ ] [3.1](http://woo31.tests.wpdesk.org) -- [ ] [3.2](http://woo32.tests.wpdesk.org) -- [ ] [3.3](http://woo33.tests.wpdesk.org) -- [ ] [3.4](http://woo34.tests.wpdesk.org) -- [ ] [Stable](https://stable.wpdesk.org/) - -### Select PHP version e.g: [x] 5.6 -<!--- Select the PHP version on which the error occurred --> - -- [ ] 5.3 -- [ ] 5.5 -- [ ] 5.6 -- [ ] 7.1 -- [ ] 7.2 - -## Steps to Reproduce -<!--- Provide a link to a live example, or an unambiguous set of steps to --> - -1. -2. - - -## Expected Behavior -<!--- Tell what should happen --> - -1. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..761c900fe87d9ae90b88901ca0e684c6439b85dc --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +Wp-basic-requirements is a simple library for WordPress plugins to check if the environment meets the requirements +and if the requirements are not met shows proper notice to user. + +Library have to be compatible with PHP 5.2.x as it's the lowest possible version for WordPress. + +Requirements may be: +- Minimal PHP version +- Minimal WordPress version +- Minimal WooCommerce version +- Required PHP module +- Required PHP setting +- OpenSSL version \ No newline at end of file diff --git a/src/Basic_Requirement_Checker.php b/src/Basic_Requirement_Checker.php index 538599d71897ea39686e23ddfff8d98720bb2a12..89ae599f125432ffbc8297e4293872ecbeccdef2 100644 --- a/src/Basic_Requirement_Checker.php +++ b/src/Basic_Requirement_Checker.php @@ -1,21 +1,21 @@ <?php -if ( ! interface_exists( 'WPDesk_Translable' ) ) { - require_once 'Translable.php'; +if ( ! interface_exists( 'WPDesk_Translatable' ) ) { + require_once 'Translatable.php'; } /** * Checks requirements for plugin * have to be compatible with PHP 5.2.x */ -class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { +class WPDesk_Basic_Requirement_Checker implements WPDesk_Translatable { const EXTENSION_NAME_OPENSSL = 'openssl'; const HOOK_ADMIN_NOTICES_ACTION = 'admin_notices'; /** @var string */ - private $plugin_name = ''; + private $plugin_name; /** @var string */ - private $plugin_file = ''; + private $plugin_file; /** @var string */ private $min_php_version; /** @var string */ @@ -107,7 +107,7 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { * @return $this */ public function add_plugin_require( $plugin_name, $nice_plugin_name = null ) { - if ( is_null( $nice_plugin_name ) ) { + if ( $nice_plugin_name === null ) { $this->plugin_require[ $plugin_name ] = $plugin_name; } else { $this->plugin_require[ $plugin_name ] = $nice_plugin_name; @@ -123,7 +123,7 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { * @return $this */ public function add_php_module_require( $module_name, $nice_name = null ) { - if ( is_null( $nice_name ) ) { + if ( $nice_name === null ) { $this->module_require[ $module_name ] = $module_name; } else { $this->module_require[ $module_name ] = $nice_name; @@ -158,19 +158,19 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { */ private function prepare_requirement_notices() { $notices = array(); - if ( ! $this->is_php_at_least( $this->min_php_version ) ) { + if ( ! self::is_php_at_least( $this->min_php_version ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on PHP versions older than %s. Please contact your host and ask them to upgrade.', $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_php_version ) ); } - if ( ! $this->is_wp_at_least( $this->min_wp_version ) ) { + if ( ! self::is_wp_at_least( $this->min_wp_version ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WordPress versions older than %s. Please update WordPress.', $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wp_version ) ); } - if ( ! is_null( $this->min_wc_version ) && $this->can_check_plugin_version() && ! $this->is_wc_at_least( $this->min_wc_version ) ) { + if ( $this->min_wc_version !== null && $this->can_check_plugin_version() && ! self::is_wc_at_least( $this->min_wc_version ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WooCommerce versions older than %s. Please update WooCommerce.', $this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wc_version ) ); } - if ( ! is_null( $this->min_openssl_version ) && ! $this->is_open_ssl_at_least( $this->min_openssl_version ) ) { + if ( $this->min_openssl_version !== null && ! self::is_open_ssl_at_least( $this->min_openssl_version ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without OpenSSL module version at least %s. Please update OpenSSL module.', $this->get_text_domain() ), esc_html( $this->plugin_name ), '0x' . dechex( $this->min_openssl_version ) ) ); @@ -189,7 +189,7 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { * @return mixed */ public static function is_php_at_least( $min_version ) { - return version_compare( phpversion(), $min_version, '>=' ); + return version_compare( PHP_VERSION, $min_version, '>=' ); } /** @@ -258,7 +258,7 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { private function append_plugin_require_notices( $notices ) { if ( count( $this->plugin_require ) > 0 ) { foreach ( $this->plugin_require as $plugin_name => $nice_plugin_name ) { - if ( ! $this->is_wp_plugin_active( $plugin_name ) ) { + if ( ! self::is_wp_plugin_active( $plugin_name ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s active. Please install and activate %s plugin.', $this->get_text_domain() ), esc_html( $this->plugin_name ), esc_html( basename( $nice_plugin_name ) ), esc_html( basename( $nice_plugin_name ) ) ) ); @@ -294,7 +294,7 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { private function append_module_require_notices( $notices ) { if ( count( $this->module_require ) > 0 ) { foreach ( $this->module_require as $module_name => $nice_module_name ) { - if ( ! $this->is_module_active( $module_name ) ) { + if ( ! self::is_module_active( $module_name ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s php module installed. Please contact your host and ask them to install %s.', $this->get_text_domain() ), esc_html( $this->plugin_name ), esc_html( basename( $nice_module_name ) ), esc_html( basename( $nice_module_name ) ) ) ); @@ -322,7 +322,7 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { private function append_settings_require_notices( $notices ) { if ( count( $this->setting_require ) > 0 ) { foreach ( $this->setting_require as $setting => $value ) { - if ( ! $this->is_setting_set( $setting, $value ) ) { + if ( ! self::is_setting_set( $setting, $value ) ) { $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without %s php setting set to %s. Please contact your host and ask them to set %s.', $this->get_text_domain() ), esc_html( $this->plugin_name ), esc_html( basename( $setting ) ), esc_html( basename( $value ) ), esc_html( basename( $setting ) ) ) ); @@ -340,19 +340,52 @@ class WPDesk_Basic_Requirement_Checker implements WPDesk_Translable { * @return bool */ public static function is_setting_set( $name, $value ) { - return ini_get( $name ) === strval( $value ); + return ini_get( $name ) === (string) $value; } /** * @return void + * + * @deprecated use render_notices or disable_plugin */ public function disable_plugin_render_notice() { add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'render_notices_action' ) ); } + /** + * Renders requirement notices in admin panel + * + * @return void + */ + public function render_notices() { + add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'render_notices_action' ) ); + } + + /** + * Renders requirement notices in admin panel + * + * @return void + */ + public function disable_plugin() { + add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'deactivate_action' ) ); + } + + /** + * @internal Do not use as public. Public only for wp action. + * + * @return void + */ + public function deactivate_action() { + if ( isset( $this->plugin_file ) ) { + deactivate_plugins( plugin_basename( $this->plugin_file ) ); + } + } + /** - * Shoud be called as WordPress action + * Should be called as WordPress action * + * @internal Do not use as public. Public only for wp action. + * * @return void */ public function render_notices_action() { diff --git a/src/Plugin/Has_Plugin_Info.php b/src/Plugin/Has_Plugin_Info.php index 18a0171603c912674664a1231fbcda5009d31fe5..5ecf98d525474f04c37ff04a64ec6926e653e5c4 100644 --- a/src/Plugin/Has_Plugin_Info.php +++ b/src/Plugin/Has_Plugin_Info.php @@ -1,7 +1,7 @@ <?php -if ( ! interface_exists( 'WPDesk_Translable' ) ) { - require_once dirname(__FILE__) . '/../Translable.php'; +if ( ! interface_exists( 'WPDesk_Translatable' ) ) { + require_once __DIR__ . '/../Translatable.php'; } @@ -10,7 +10,7 @@ if ( ! interface_exists( 'WPDesk_Translable' ) ) { * * have to be compatible with PHP 5.2.x */ -interface WPDesk_Has_Plugin_Info extends WPDesk_Translable { +interface WPDesk_Has_Plugin_Info extends WPDesk_Translatable { /** * @return string */ diff --git a/src/Plugin/Plugin_Info.php b/src/Plugin/Plugin_Info.php index b74bf0bd6070f80d8190377d2a17e77ad59d1d3d..0645ca58f7de56eb44c5e1120876e4a98949f9bc 100644 --- a/src/Plugin/Plugin_Info.php +++ b/src/Plugin/Plugin_Info.php @@ -1,10 +1,10 @@ <?php -if ( ! interface_exists( 'WPDesk_Translable' ) ) { - require_once dirname(__FILE__) . '/../Translable.php'; +if ( ! interface_exists( 'WPDesk_Translatable' ) ) { + require_once __DIR__ . '/../Translatable.php'; } if ( ! class_exists( 'WPDesk_Buildable' ) ) { - require_once dirname(__FILE__) . '/../Buildable.php'; + require_once __DIR__ . '/../Buildable.php'; } if ( ! class_exists( 'WPDesk_Has_Plugin_Info' ) ) { require_once 'Has_Plugin_Info.php'; @@ -15,7 +15,7 @@ if ( ! class_exists( 'WPDesk_Has_Plugin_Info' ) ) { * * have to be compatible with PHP 5.2.x */ -class WPDesk_Plugin_Info implements WPDesk_Translable, WPDesk_Buildable, WPDesk_Has_Plugin_Info { +class WPDesk_Plugin_Info implements WPDesk_Translatable, WPDesk_Buildable, WPDesk_Has_Plugin_Info { /** @var string */ private $plugin_file_name; diff --git a/src/Translable.php b/src/Translatable.php similarity index 83% rename from src/Translable.php rename to src/Translatable.php index 32075f503d06cd35636bada1420cc2c751109c03..cc19eddbbfd9fcfde294189cae167ee94736c07c 100644 --- a/src/Translable.php +++ b/src/Translatable.php @@ -5,7 +5,7 @@ * * have to be compatible with PHP 5.2.x */ -interface WPDesk_Translable { +interface WPDesk_Translatable { /** @return string */ public function get_text_domain(); } \ No newline at end of file diff --git a/tests/unit/Test_Basic_Requirement_Checker.php b/tests/unit/Test_Basic_Requirement_Checker.php index f7c2be96650ea921ece4cf39d9b9a398302bc544..e68e24d5e82e428bad51c0c6cd46fec47ae5560d 100644 --- a/tests/unit/Test_Basic_Requirement_Checker.php +++ b/tests/unit/Test_Basic_Requirement_Checker.php @@ -154,23 +154,14 @@ class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_NOT_VALID_PHP_VERSION, self::ALWAYS_VALID_WP_VERSION ); - WP_Mock::expectActionAdded( WPDesk_Basic_Requirement_Checker::HOOK_ADMIN_NOTICES_ACTION, - [ $requirements, 'deactivate_action' ] ); WP_Mock::expectActionAdded( WPDesk_Basic_Requirement_Checker::HOOK_ADMIN_NOTICES_ACTION, [ $requirements, 'render_notices_action' ] ); $this->assertFalse( $requirements->are_requirements_met() ); - $requirements->disable_plugin_render_notice(); - - WP_Mock::wpFunction( 'deactivate_plugins' ) - ->once(); - - WP_Mock::wpFunction( 'plugin_basename' ) - ->once() - ->andReturn( 'whatever' ); + $requirements->disable_plugin(); + $requirements->render_notices(); $this->expectOutputRegex( '/cannot run on PHP/' ); - $requirements->deactivate_action(); $requirements->render_notices_action(); } }