Skip to content

Commit

Permalink
fix: Update appsero SDK (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
sourovroy authored and sabbir1991 committed May 6, 2019
1 parent 787dafa commit 6418187
Show file tree
Hide file tree
Showing 6 changed files with 1,036 additions and 192 deletions.
19 changes: 12 additions & 7 deletions classes/tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Dokan_Tracker {
* @return void
*/
public function __construct() {
add_action( 'init', array( $this, 'appsero_init_tracker_dokan' ) );

$this->appsero_init_tracker_dokan();
}

/**
Expand All @@ -29,17 +30,20 @@ public function __construct() {
* @return void
*/
public function appsero_init_tracker_dokan() {
if ( ! class_exists( 'AppSero\Insights' ) ) {
require_once DOKAN_LIB_DIR . '/appsero/src/insights.php';

if ( ! class_exists( 'Appsero\Client' ) ) {
require_once DOKAN_LIB_DIR . '/appsero/Client.php';
}

$this->insights = new AppSero\Insights( '559bcc0d-21b4-4b34-8317-3e072badf46d', 'Dokan Multivendor Marketplace', DOKAN_FILE );
$client = new Appsero\Client( '559bcc0d-21b4-4b34-8317-3e072badf46d', 'Dokan Multivendor Marketplace', DOKAN_FILE );

$this->insights->add_extra(array(
$this->insights = $client->insights();

$this->insights->add_extra( [
'products' => $this->insights->get_post_count( 'product' ),
'orders' => $this->get_order_count(),
'is_pro' => dokan()->is_pro_exists() ? 'Yes' : 'No',
));
'is_pro' => class_exists( 'Dokan_Pro' ) ? 'Yes' : 'No',
] );

$this->insights->init_plugin();
}
Expand All @@ -54,4 +58,5 @@ protected function get_order_count() {

return (int) $wpdb->get_var( "SELECT count(ID) FROM $wpdb->posts WHERE post_type = 'shop_order' and post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-refunded');");
}

}
28 changes: 22 additions & 6 deletions dokan.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ final class WeDevs_Dokan {
*/
public $version = '2.9.14';

/**
* Instance of self
*
* @var WeDevs_Dokan
*/
private static $instance = null;

/**
* Minimum PHP version required
*
Expand All @@ -102,13 +109,15 @@ final class WeDevs_Dokan {
* Sets up all the appropriate hooks and actions
* within our plugin.
*/
public function __construct() {
private function __construct() {
$this->define_constants();

register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );

add_action( 'woocommerce_loaded', array( $this, 'init_plugin' ) );

$this->init_appsero_tracker();
}

/**
Expand All @@ -118,13 +127,12 @@ public function __construct() {
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;

if ( ! $instance ) {
$instance = new WeDevs_Dokan();
if ( self::$instance === null ) {
self::$instance = new self();
}

return $instance;
return self::$instance;
}

/**
Expand Down Expand Up @@ -362,7 +370,6 @@ function init_classes() {

$this->container['pageview'] = new Dokan_Pageviews();
$this->container['rewrite'] = new Dokan_Rewrites();
$this->container['tracker'] = new Dokan_Tracker();
$this->container['seller_wizard'] = new Dokan_Seller_Setup_Wizard();
$this->container['core'] = new Dokan_Core();
$this->container['scripts'] = new Dokan_Assets();
Expand Down Expand Up @@ -473,6 +480,15 @@ function plugin_action_links( $links ) {
return $links;
}

/**
* Initialize Appsero Tracker
*
* @return void
*/
public function init_appsero_tracker() {
$this->container['tracker'] = new Dokan_Tracker();
}

} // WeDevs_Dokan

/**
Expand Down
202 changes: 202 additions & 0 deletions lib/appsero/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
namespace Appsero;

/**
* AppSero Client
*
* This class is necessary to set project data
*/
class Client {

/**
* The client version
*
* @var string
*/
public $version = '1.0.1';

/**
* Hash identifier of the plugin
*
* @var string
*/
public $hash;

/**
* Name of the plugin
*
* @var string
*/
public $name;

/**
* The plugin/theme file path
* @example .../wp-content/plugins/test-slug/test-slug.php
*
* @var string
*/
public $file;

/**
* Main plugin file
* @example test-slug/test-slug.php
*
* @var string
*/
public $basename;

/**
* Slug of the plugin
* @example test-slug
*
* @var string
*/
public $slug;

/**
* The project version
*
* @var string
*/
public $project_version;

/**
* The project type
*
* @var string
*/
public $type;

/**
* Initialize the class
*
* @param string $hash hash of the plugin
* @param string $name readable name of the plugin
* @param string $file main plugin file path
*/
public function __construct( $hash, $name, $file ) {
$this->hash = $hash;
$this->name = $name;
$this->file = $file;

$this->set_basename_and_slug();
}

/**
* Initialize insights class
*
* @return Appsero\Insights
*/
public function insights() {

if ( ! class_exists( __NAMESPACE__ . '\Insights') ) {
require_once __DIR__ . '/Insights.php';
}

return new Insights( $this );
}

/**
* Initialize plugin/theme updater
*
* @return Appsero\Updater
*/
public function updater() {

if ( ! class_exists( __NAMESPACE__ . '\Updater') ) {
require_once __DIR__ . '/Updater.php';
}

return new Updater( $this );
}

/**
* Initialize license checker
*
* @return Appsero\License
*/
public function license() {

if ( ! class_exists( __NAMESPACE__ . '\License') ) {
require_once __DIR__ . '/License.php';
}

return new License( $this );
}

/**
* API Endpoint
*
* @return string
*/
public function endpoint() {
$endpoint = apply_filters( 'appsero_endpoint', 'https://api.appsero.com' );

return trailingslashit( $endpoint );
}

/**
* Set project basename, slug and version
*
* @return void
*/
protected function set_basename_and_slug() {

if ( strpos( $this->file, WP_CONTENT_DIR . '/themes/' ) === false ) {

$this->basename = plugin_basename( $this->file );

list( $this->slug, $mainfile) = explode( '/', $this->basename );

require_once ABSPATH . 'wp-admin/includes/plugin.php';

$plugin_data = get_plugin_data( $this->file );

$this->project_version = $plugin_data['Version'];
$this->type = 'plugin';

} else {

$this->basename = str_replace( WP_CONTENT_DIR . '/themes/', '', $this->file );

list( $this->slug, $mainfile) = explode( '/', $this->basename );

$theme = wp_get_theme( $this->slug );

$this->project_version = $theme->version;
$this->type = 'theme';

}
}

/**
* Send request to remote endpoint
*
* @param array $params
* @param string $route
*
* @return array|WP_Error Array of results including HTTP headers or WP_Error if the request failed.
*/
public function send_request( $params, $route, $blocking = false ) {
$url = $this->endpoint() . $route;

$headers = array(
'user-agent' => 'Appsero/' . md5( esc_url( home_url() ) ) . ';',
'Accept' => 'application/json',
);

$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => $blocking,
'headers' => $headers,
'body' => array_merge( $params, array( 'client' => $this->version ) ),
'cookies' => array()
) );

return $response;
}

}
Loading

0 comments on commit 6418187

Please sign in to comment.