Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
fix woocommerce check
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 23, 2015
1 parent f962780 commit 80285c4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 82 deletions.
70 changes: 20 additions & 50 deletions includes/class-wc-pos-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,20 @@ static private function add_pos_capability(){
* Check dependencies
*/
public function run_checks() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;

if( ! current_user_can( 'activate_plugins' ) )
return;

$this->version_check();
$this->woocommerce_check();
$this->wc_api_check();
$this->permalink_check();
}

/**
* Check version number, runs every time
* Check version number, runs every admin page load
*/
public function version_check(){
private function version_check(){
// next check the POS version number
$old = get_option( 'woocommerce_pos_db_version' );
if( !$old || version_compare( $old, WC_POS_VERSION, '<' ) ) {
Expand All @@ -146,7 +150,7 @@ public function version_check(){
/**
* Upgrade database
*/
public function db_upgrade( $old, $current ) {
private function db_upgrade( $old, $current ) {
$db_updates = array(
'0.4' => 'updates/update-0.4.php'
);
Expand All @@ -159,57 +163,23 @@ public function db_upgrade( $old, $current ) {
}

/**
* Check if WooCommerce is active
* WooCommerce POS will not load if WooCommerce is not present
*/
private function woocommerce_check() {
if( ! current_user_can( 'activate_plugins' ) )
return;

if ( ! class_exists( 'WooCommerce' ) && current_user_can( 'activate_plugins' ) ) {

// alert the user
$error = array (
'msg_type' => 'error',
'msg' => sprintf( __('<strong>WooCommerce POS</strong> requires <a href="%s">WooCommerce</a>. Please <a href="%s">install and activate WooCommerce</a>', 'woocommerce-pos' ), 'http://wordpress.org/plugins/woocommerce/', admin_url('plugins.php') ) . ' &raquo;'
);
do_action('woocommerce_pos_add_admin_notice', $error);
}

if( ! class_exists( 'WooCommerce' ) )
add_action( 'admin_notices', array( $this, 'woocommerce_alert' ) );
}

/**
* Check if the WC API option is enabled
* Admin message - WooCommerce not activated
*/
private function wc_api_check() {
if( ! current_user_can( 'manage_woocommerce' ) )
return;

if( get_option( 'woocommerce_api_enabled' ) !== 'yes' && ! isset( $_POST['woocommerce_api_enabled'] ) ) {

// alert the user
$error = array (
'msg_type' => 'error',
'msg' => sprintf( __('<strong>WooCommerce POS</strong> requires the WooCommerce REST API. Please <a href="%s">enable the REST API</a>', 'woocommerce-pos' ), admin_url('admin.php?page=wc-settings') ) . ' &raquo;'
);
do_action('woocommerce_pos_add_admin_notice', $error);

}
public function woocommerce_alert(){
echo '<div class="error">
<p>'. sprintf( __('<strong>WooCommerce POS</strong> requires <a href="%s">WooCommerce</a>. Please <a href="%s">install and activate WooCommerce</a>', 'woocommerce-pos' ), 'http://wordpress.org/plugins/woocommerce/', admin_url('plugins.php') ) . ' &raquo;</p>
</div>';
}

/**
* Check if permalinks enabled, WC API needs permalinks
*/
private function permalink_check() {
global $wp_rewrite;
if( $wp_rewrite->permalink_structure == '' ) {

// alert the user
$error = array (
'msg_type' => 'error',
'msg' => sprintf( __('<strong>WooCommerce REST API</strong> requires <em>pretty</em> permalinks to work correctly. Please <a href="%s">enable permalinks</a>.', 'woocommerce-pos'), admin_url('options-permalink.php') ) . ' &raquo;'
);
do_action('woocommerce_pos_add_admin_notice', $error);
}
}
}

}
// self loading
new WC_POS_Activator();
1 change: 1 addition & 0 deletions includes/class-wc-pos-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function dispatch_args($args, $callback){
}

/**
* - this filter was introduced in WC 2.3
* @param $args
* @param $request_args
* @return mixed
Expand Down
27 changes: 7 additions & 20 deletions includes/class-wc-pos-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class WC_POS_Status {

private $wc_min_version = '2.2';
private $wc_min_version = '2.3';

public function output() {
$results = array(
Expand All @@ -21,7 +21,7 @@ public function output() {
}

/**
* Test WC >= 2.2
* Test WC >= $wc_min_version
* @return boolean
*/
private function test_wc_version() {
Expand Down Expand Up @@ -71,14 +71,6 @@ private function test_wc_rest_api() {
));
}

// check curl
if( !function_exists('curl_init') ){
return array_merge( $result, array(
'pass' => false,
'message' => __( 'Unable to test the REST API', 'woocommerce-pos' )
));
}

// check API access
if( $this->http_status( get_woocommerce_api_url('') ) !== 200 ){
return array_merge( $result, array(
Expand Down Expand Up @@ -126,16 +118,11 @@ private function use_restful_http_methods(){
* @return bool|int
*/
private function http_status( $url, $method = 'GET' ){
if( $ch = curl_init($url)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return (int) $status;
} else {
return false;
}
$args = array(
'method' => $method
);
$response = wp_remote_request( $url, $args );
return wp_remote_retrieve_response_code( $response );
}

}
1 change: 0 additions & 1 deletion includes/class-wc-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function init() {

// admin only
if (is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX)) {
new WC_POS_Activator(); // contains sanity checks
new WC_POS_Admin();
}

Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can see a demo of the WooCommerce POS plugin in action by going to [http://d
* Upgrade to Pro: http://woopos.com.au/pro

= REQUIREMENTS =
* WooCommerce >= 2.2.0
* WooCommerce >= 2.3.0
* [A modern browser](http://woopos.com.au/faq/browser-compatibility/)

== Installation ==
Expand Down
23 changes: 14 additions & 9 deletions tests/integration/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

class Integration_Test_WC_POS {

private $wp_root;

public function __construct(){

ini_set( 'display_errors','on' );
error_reporting( E_ALL );

$this->includes();
switch_theme('WooCommerce-POS-Test-Theme', 'WooCommerce-POS-Test-Theme');
update_option('active_plugins', array(
'woocommerce-pos/woocommerce-pos.php',
'woocommerce/woocommerce.php'
));
register_shutdown_function( array( $this, 'after_tests' ) );
$this->setup();
register_shutdown_function( array( $this, 'shutdown' ) );
}

/**
* include wp-load
* include composer autoloader
*/
private function includes(){

public function includes(){
require_once(__DIR__.'/../../../../../../wp-load.php');

if( is_readable( 'vendor/autoload.php' ) ){
Expand All @@ -34,10 +31,18 @@ private function includes(){

}

public function setup(){
switch_theme('WooCommerce-POS-Test-Theme', 'WooCommerce-POS-Test-Theme');
update_option('active_plugins', array(
'woocommerce-pos/woocommerce-pos.php',
'woocommerce/woocommerce.php'
));
}

/**
* runs after all tests are complete
*/
public function after_tests(){
public function shutdown(){
switch_theme('twentyfifteen', 'twentyfifteen');
}

Expand Down
45 changes: 45 additions & 0 deletions tests/integration/php/spec/test-activation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class ActivationTest extends PHPUnit_Framework_TestCase {

protected $client;

public function setUp() {
$this->client = new GuzzleHttp\Client([
'defaults' => ['exceptions' => false],
]);
}

public function test_load() {
$response = $this->client->get( wc_pos_url(), [
'allow_redirects' => false
]);
// expect 302 redirect to login
$this->assertEquals(302, $response->getStatusCode());
}

public function test_load_order() {
// reverse plugin load order
update_option('active_plugins', array(
'woocommerce/woocommerce.php',
'woocommerce-pos/woocommerce-pos.php'
));

$response = $this->client->get( wc_pos_url(), [
'allow_redirects' => false
]);
// expect 302 redirect to login
$this->assertEquals(302, $response->getStatusCode());

// restore plugin load order
update_option('active_plugins', array(
'woocommerce-pos/woocommerce-pos.php',
'woocommerce/woocommerce.php'
));
}

public function tearDown(){

}

}
7 changes: 6 additions & 1 deletion woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
/**
* Begins execution of the plugin.
*/
add_action( 'woocommerce_loaded', 'run_woocommerce_pos' );
function run_woocommerce_pos() {
new WC_POS();
}
global $wp_actions;
if ( isset( $wp_actions['woocommerce_loaded'] ) ) {
run_woocommerce_pos();
} else {
add_action( 'woocommerce_loaded', 'run_woocommerce_pos' );
}

0 comments on commit 80285c4

Please sign in to comment.