Plugin code
This commit is contained in:
parent
8819c70d0e
commit
2fde6edfdc
28 changed files with 6338 additions and 0 deletions
409
admin/partials/class-cklph-wdl-rest-api.php
Normal file
409
admin/partials/class-cklph-wdl-rest-api.php
Normal file
|
@ -0,0 +1,409 @@
|
|||
<?php
|
||||
/**
|
||||
* The list-table class that defines the product details and its add to cart url
|
||||
*
|
||||
* Get the product name, sku, thumbnail, and category of a woocommerce product and its checkout
|
||||
* link.
|
||||
*
|
||||
* @link https://chykalophia.com/woocommerce-direct-links
|
||||
* @since 0.1.0
|
||||
* @package Woocommerce_Direct_Links
|
||||
* @subpackage Woocommerce_Direct_Links/admin/partials/list-table
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit();
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'Cklph_Wdl_Rest' ) ) {
|
||||
|
||||
/**
|
||||
* Rest Api for WDL Plugin.
|
||||
*/
|
||||
class Cklph_Wdl_Rest extends WP_REST_Controller {
|
||||
|
||||
/**
|
||||
* Register rest route of the plugin.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cklph_wdl_rest_route() {
|
||||
|
||||
// rest route for individual shortlink request.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/product/(?P<id>\d+)',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_refresh' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for individual force refresh request for joturl.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/product-joturl/(?P<id>\d+)',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_force_joturl' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for bulk refresh request.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/product/bulk',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_bulk_refresh' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for shortlink provider activate/deactivate.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/shortlink/(?P<shortlink>[a-zA-Z0-9-]+)',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_activate' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for tinyurl setup.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/tinyurl',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_tinyurl_settings' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
'args' => array(
|
||||
'domain' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
'apikey' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for bitly setup.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/bitly',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_bitly_settings' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
'args' => array(
|
||||
'apikey' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for yourls setup.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/yourls',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_yourls_settings' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
'args' => array(
|
||||
'domain' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
'apikey' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// rest route for yourls setup.
|
||||
register_rest_route(
|
||||
'cklph-wdl/v1',
|
||||
'/joturl',
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'cklph_wdl_joturl_settings' ),
|
||||
'permission_callback' => array( $this, 'permission_check' ),
|
||||
'args' => array(
|
||||
'public' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
'private' => array( 'validate_callback' => array( $this, 'validate_string_callback' ) ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if callback is string.
|
||||
*
|
||||
* @param string $param value of args.
|
||||
* @param string $request request.
|
||||
* @param string $key key.
|
||||
* @return string $param
|
||||
*/
|
||||
public function validate_string_callback( $param, $request, $key ) {
|
||||
return is_string( $param );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to data.
|
||||
*
|
||||
* @param mixed $request full data of the request.
|
||||
* @return WP_Error|bool
|
||||
*/
|
||||
public function permission_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save setting setup for bitly.
|
||||
*
|
||||
* @param string $request api key for bitly.
|
||||
* @return $result success || error.
|
||||
*/
|
||||
public function cklph_wdl_bitly_settings( $request ) {
|
||||
$access_token = $request['apikey'];
|
||||
$wc_product = new Cklph_Wdl_Wc_Products();
|
||||
$product = $wc_product->get_single_wc_product_id();
|
||||
|
||||
update_option( 'bitly_access_token_options', $access_token );
|
||||
|
||||
$rest_url = $this->cklph_wdl_check_rest( $product, 'bitly' );
|
||||
|
||||
if ( ! is_numeric( $rest_url ) ) {
|
||||
update_option( 'url_options', 'none' );
|
||||
update_option( 'bitly_access_token_options', '' );
|
||||
return new WP_REST_Response( $rest_url );
|
||||
}
|
||||
|
||||
$bitly_apikey = get_option( 'bitly_access_token_options' );
|
||||
$response = new WP_REST_Response( 200 );
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save setting setup for tinyurl.
|
||||
*
|
||||
* @param string $request type, domain & apikey for tinyurl.
|
||||
* @return $result success || error.
|
||||
*/
|
||||
public function cklph_wdl_tinyurl_settings( $request ) {
|
||||
$tinyurl_domain = $request['domain'];
|
||||
$tinyurl_apikey = $request['apikey'];
|
||||
$wc_product = new Cklph_Wdl_Wc_Products();
|
||||
$product = $wc_product->get_single_wc_product_id();
|
||||
|
||||
update_option( 'tinyurl_access_token_options', $tinyurl_apikey );
|
||||
update_option( 'tinyurl_domain', $tinyurl_domain );
|
||||
|
||||
$rest_url = $this->cklph_wdl_check_rest( $product, 'tinyurl' );
|
||||
|
||||
if ( ! is_numeric( $rest_url ) ) {
|
||||
update_option( 'tinyurl_domain', '' );
|
||||
return new WP_REST_Response( $rest_url );
|
||||
}
|
||||
|
||||
$response = new WP_REST_Response( 200 );
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save setting setup for yourls.
|
||||
*
|
||||
* @param string $request site & api key for yourls.
|
||||
* @return $result success || error.
|
||||
*/
|
||||
public function cklph_wdl_yourls_settings( $request ) {
|
||||
$yourls_domain = $request['domain'];
|
||||
$yourls_apikey = $request['apikey'];
|
||||
$wc_product = new Cklph_Wdl_Wc_Products();
|
||||
$product = $wc_product->get_single_wc_product_id();
|
||||
|
||||
update_option( 'yourls_signature', $yourls_apikey );
|
||||
update_option( 'yourls_domain', $yourls_domain );
|
||||
|
||||
$rest_url = $this->cklph_wdl_check_rest( $product, 'yourls' );
|
||||
|
||||
if ( ! is_numeric( $rest_url ) ) {
|
||||
update_option( 'url_options', 'none' );
|
||||
update_option( 'yourls_domain', '' );
|
||||
update_option( 'yourls_signature', '' );
|
||||
return new WP_REST_Response( $rest_url );
|
||||
}
|
||||
|
||||
$response = new WP_REST_Response( 200 );
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save setting setup for joturl.
|
||||
*
|
||||
* @param string $request site & api key for yourls.
|
||||
* @return $result success || error.
|
||||
*/
|
||||
public function cklph_wdl_joturl_settings( $request ) {
|
||||
$joturl_public = $request['public'];
|
||||
$joturl_private = $request['private'];
|
||||
$wc_product = new Cklph_Wdl_Wc_Products();
|
||||
$product = $wc_product->get_single_wc_product_id();
|
||||
|
||||
update_option( 'joturl_private', $joturl_private );
|
||||
update_option( 'joturl_public', $joturl_public );
|
||||
|
||||
$rest_url = $this->cklph_wdl_check_rest( $product, 'joturl' );
|
||||
|
||||
if ( ! is_numeric( $rest_url ) ) {
|
||||
update_option( 'joturl_public', '' );
|
||||
update_option( 'joturl_private', '' );
|
||||
update_option( 'url_options', 'none' );
|
||||
return new WP_REST_Response( $rest_url );
|
||||
}
|
||||
|
||||
$response = new WP_REST_Response( 200 );
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate/deactivate shortener provider
|
||||
*
|
||||
* @param string $request shortener provider.
|
||||
* @return string $result url_options.
|
||||
*/
|
||||
public function cklph_wdl_activate( $request ) {
|
||||
$option = $request['shortlink'];
|
||||
$url_option = get_option( 'url_options' );
|
||||
$wc_product = new Cklph_Wdl_Wc_Products();
|
||||
$product = $wc_product->get_single_wc_product_id();
|
||||
$rest_url = $this->cklph_wdl_check_rest( $product, $option );
|
||||
|
||||
if ( ! is_numeric( $rest_url ) ) {
|
||||
return new WP_REST_Response( $rest_url );
|
||||
}
|
||||
|
||||
if ( $url_option !== $option ) {
|
||||
update_option( 'url_options', $option );
|
||||
} else {
|
||||
update_option( 'url_options', 'none' );
|
||||
}
|
||||
|
||||
$new_option = get_option( 'url_options' );
|
||||
$response = new WP_REST_Response( $new_option );
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Force refresh joturl shortlink when refresh link is click twice.
|
||||
*
|
||||
* @param int $request item id of product.
|
||||
* @return $response refreshed link || error.
|
||||
*/
|
||||
public function cklph_wdl_force_joturl( $request ) {
|
||||
$item_id = $request['id'];
|
||||
$url_option = get_option( 'url_options' );
|
||||
$home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
|
||||
|
||||
if ( 'joturl' === $url_option ) {
|
||||
$rest_url = $this->cklph_wdl_check_rest( $item_id, 'force-joturl' );
|
||||
$failed = new WP_REST_Response( array( $home_url, $rest_url ) );
|
||||
$get_url = get_post_meta( $item_id, 'joturl_link', true );
|
||||
$success = new WP_REST_Response( array( $get_url, $rest_url ) );
|
||||
$result = is_numeric( $rest_url ) ? $success : $failed;
|
||||
return $result;
|
||||
}
|
||||
return $this->cklph_wdl_refresh( $item_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh shortlink when refresh link is click.
|
||||
*
|
||||
* @param int $request item id of product.
|
||||
* @return $response refreshed link.
|
||||
*/
|
||||
public function cklph_wdl_refresh( $request ) {
|
||||
$item_id = empty( $request['id'] ) ? $request : $request['id'];
|
||||
$url_option = get_option( 'url_options' );
|
||||
$home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
|
||||
$rest_url = $this->cklph_wdl_check_rest( $item_id, $url_option );
|
||||
$failed = new WP_REST_Response( array( $home_url, $rest_url ) );
|
||||
|
||||
switch ( $url_option ) {
|
||||
case 'tinyurl':
|
||||
$get_url = get_post_meta( $item_id, 'tinyurl_link', true );
|
||||
$success = new WP_REST_Response( array( $get_url, $rest_url ) );
|
||||
$result = is_numeric( $rest_url ) ? $success : $failed;
|
||||
return $result;
|
||||
case 'bitly':
|
||||
$get_url = get_post_meta( $item_id, 'bitly_link', true );
|
||||
$success = new WP_REST_Response( array( $get_url, $rest_url ) );
|
||||
$result = is_numeric( $rest_url ) ? $success : $failed;
|
||||
return $result;
|
||||
case 'yourls':
|
||||
$get_url = get_post_meta( $item_id, 'yourls_link', true );
|
||||
$success = new WP_REST_Response( array( $get_url, $rest_url ) );
|
||||
$result = is_numeric( $rest_url ) ? $success : $failed;
|
||||
return $result;
|
||||
case 'joturl':
|
||||
$get_url = get_post_meta( $item_id, 'joturl_link', true );
|
||||
$success = new WP_REST_Response( array( $get_url, $rest_url ) );
|
||||
$result = is_numeric( $rest_url ) ? $success : $failed;
|
||||
return $result;
|
||||
case 'none':
|
||||
$success_response = new WP_REST_Response( array( $home_url, 200 ) );
|
||||
return $success_response;
|
||||
default:
|
||||
$success_response = new WP_REST_Response( array( $home_url, 200 ) );
|
||||
return $success_response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh all shortlink when refresh link is click in settings page.
|
||||
*
|
||||
* @param int $request all ids of products.
|
||||
* @return $response refreshed link || error.
|
||||
*/
|
||||
public function cklph_wdl_bulk_refresh( $request ) {
|
||||
$wc_products = new Cklph_Wdl_Wc_Products();
|
||||
$products = $wc_products->get_all_wc_product_ids();
|
||||
$url_option = get_option( 'url_options' );
|
||||
|
||||
foreach ( $products as $product ) {
|
||||
$rest_url = $this->cklph_wdl_check_rest( $product, $url_option );
|
||||
|
||||
if ( ! is_numeric( $rest_url ) ) {
|
||||
return new WP_REST_Response( $rest_url );
|
||||
}
|
||||
}
|
||||
|
||||
$success_response = new WP_REST_Response( 200 );
|
||||
return $success_response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run shortlink rest api and check for errors.
|
||||
*
|
||||
* @param int $item_id product id.
|
||||
* @param string $url_option url option for shortener.
|
||||
* @return $shortlink error.
|
||||
*/
|
||||
protected function cklph_wdl_check_rest( $item_id, $url_option ) {
|
||||
$shortlink = new Cklph_Wdl_Shortlinks();
|
||||
|
||||
switch ( $url_option ) {
|
||||
case 'tinyurl':
|
||||
return $shortlink->cklph_wdl_check_tinyurl( $item_id );
|
||||
case 'bitly':
|
||||
return $shortlink->cklph_wdl_check_bitly( $item_id );
|
||||
case 'yourls':
|
||||
return $shortlink->cklph_wdl_check_yourls( $item_id );
|
||||
case 'joturl':
|
||||
return $shortlink->cklph_wdl_check_joturl( $item_id );
|
||||
case 'force-joturl':
|
||||
return $shortlink->cklph_wdl_check_force_joturl( $item_id );
|
||||
default:
|
||||
return 'none';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue