Plugin code

This commit is contained in:
janine 2024-03-01 19:04:22 +05:30
parent 8819c70d0e
commit 2fde6edfdc
28 changed files with 6338 additions and 0 deletions

View file

@ -0,0 +1,33 @@
<?php
/**
* Provide a admin page view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link https://chykalophia.com/woocommerce-direct-links
* @since 0.1.0
*
* @package Woocommerce_Direct_Links
* @subpackage Woocommerce_Direct_Links/admin/partials
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
?>
<!-- Display Page for the WordPress Direct Link Admin Page -->
<div class="wrap">
<h1>
<?php
echo esc_html( get_admin_page_title() );
?>
</h1>
<input type="hidden" id="cklph-wdl-hidden">
<div id="cklph-wdl-reply" class=""></div>
<?php
$wdl_table = new Cklph_Wdl_List_Table();
$wdl_table->prepare_items();
$wdl_table->search_box( __( 'Search Products', 'woocommerce-direct-links' ), 'search' );
$wdl_table->display();

View file

@ -0,0 +1,105 @@
<?php
/**
* Provide a settings page view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link https://chykalophia.com/woocommerce-direct-links
* @since 0.1.0
*
* @package Woocommerce_Direct_Links
* @subpackage Woocommerce_Direct_Links/admin/partials
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
?>
<div class="wrap">
<h1>
<?php
settings_errors( 'urlshortener_messages' );
echo esc_html( get_admin_page_title() );
?>
</h1>
<div id="cklph-wdl-reply" class=""></div>
<div class="tab-content">
<!-- Bulk Refresh Button -->
<button class="button button-primary cklph-wdl-bulk" type="button" id="cklph-wdl-bulk-btn" onclick="cklph_wdl_bulk_rest()" >
<span class="cklph-wdl-btn-text"><?php esc_html_e( 'Refresh All Links', 'woocommerce-direct-links' ); ?></span>
</button>
<?php
// setting field.
settings_fields( 'cklph-wdl-option-group' );
// output settings section.
do_settings_sections( 'cklph-wdl-settings' );
?>
<div id="tab-tinyurl" class="tab-pane">
<?php
// tinyurl setting field.
settings_fields( 'cklph-wdl-tinyurl-group' );
// output tinyurl settings section.
do_settings_sections( 'cklph-wdl-tinyurl-settings' );
?>
<button class="button button-primary" type="button" id="tinyurl-save-btn" onclick="cklph_wdl_setup_tinyurl()" >
<span class="cklph-wdl-btn-text">
<?php esc_html_e( 'Save Settings', 'woocommerce-direct-links' ); ?>
</span>
</button>
</div>
<div id="tab-bitly" class="tab-pane ">
<?php
// setting field.
settings_fields( 'cklph-wdl-bitly-group' );
// output settings section.
do_settings_sections( 'cklph-wdl-bitly-settings' );
?>
<button class="button button-primary" type="button" id="bitly-save-btn" onclick="cklph_wdl_setup_bitly()" >
<span class="cklph-wdl-btn-text">
<?php esc_html_e( 'Save Settings', 'woocommerce-direct-links' ); ?>
</span>
</button>
</div>
<div id="tab-yourls" class="tab-pane">
<?php
// setting field.
settings_fields( 'cklph-wdl-yourls-group' );
// output settings section.
do_settings_sections( 'cklph-wdl-yourls-settings' );
?>
<button class="button button-primary" type="button" id="yourls-save-btn" onclick="cklph_wdl_setup_yourls()" >
<span class="cklph-wdl-btn-text">
<?php esc_html_e( 'Save Settings', 'woocommerce-direct-links' ); ?>
</span>
</button>
</div>
<div id="tab-joturl" class="tab-pane">
<?php
// setting field.
settings_fields( 'cklph-wdl-joturl-group' );
// output settings section.
do_settings_sections( 'cklph-wdl-joturl-settings' );
?>
<button class="button button-primary" type="button" id="joturl-save-btn" onclick="cklph_wdl_setup_joturl()" >
<span class="cklph-wdl-btn-text">
<?php esc_html_e( 'Save Settings', 'woocommerce-direct-links' ); ?>
</span>
</button>
</div>
</div>
</div>

View 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';
}
}
}
}

View file

@ -0,0 +1,616 @@
<?php
/**
* A class that generates the shortlinks
*
* Defines the api calls of the url-shortener used in the plugin
*
* @link https://chykalophia.com/woocommerce-direct-links
* @since 0.1.0
*
* @package Woocommerce_Direct_Links
* @subpackage Woocommerce_Direct_Links/admin/partials
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
/**
* Check if Cklph_Wdl_shortlinks exists.
*/
if ( ! class_exists( 'Cklph_Wdl_Shortlinks' ) ) {
/**
* Class for shortlink API
*/
class Cklph_Wdl_Shortlinks {
/**
* Converts a url into tinyurl link.
*
* @param int $item_id item_id of product.
* @return $response if error occur.
*/
protected function cklph_wdl_get_tinyurl( $item_id ) {
$tinyurl_site = get_option( 'tinyurl_domain' );
$domain = empty( $tinyurl_site ) ? 'tiny.one' : $tinyurl_site;
$access_token = get_option( 'tinyurl_access_token_options' );
$get_url = get_post_meta( $item_id, 'tinyurl_link', true );
$home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
// If user has API key run this function.
if ( ! empty( $access_token ) ) {
$headers = array(
'Authorization' => 'Bearer ' . $access_token,
'Content-Type' => 'application/json',
);
$tinyurl_payload = array(
'url' => $home_url,
'domain' => $domain,
);
$tinyurl_payload_encode = wp_json_encode( $tinyurl_payload );
$tinyurl_request = wp_remote_post(
'https://api.tinyurl.com/create',
array(
'method' => 'POST',
'headers' => $headers,
'body' => $tinyurl_payload_encode,
)
);
$response_code = wp_remote_retrieve_response_code( $tinyurl_request );
$tinyurl_body = wp_remote_retrieve_body( $tinyurl_request );
$tinyurl_decode = json_decode( $tinyurl_body );
if ( 200 === (int) $response_code ) {
$tinyurl = $tinyurl_decode->data->tiny_url;
if ( empty( $get_url ) || $tinyurl !== $get_url ) {
update_post_meta( $item_id, 'tinyurl_link', $tinyurl );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
}
$response = array(
'response_code' => $response_code,
'message' => 'Exists',
);
return $response;
} elseif ( 400 === (int) $response_code ) {
$response = array(
'response_code' => $response_code,
'message' => 'Bad Request',
);
return $response;
} elseif ( 403 === (int) $response_code ) {
$response = array(
'response_code' => $response_code,
'message' => 'FORBIDDEN',
);
return $response;
} elseif ( 401 === (int) $response_code ) {
update_option( 'tinyurl_access_token_options', '' );
$response = array(
'response_code' => $response_code,
'message' => $tinyurl_decode->errors[0],
);
return $response;
} else {
$response = array(
'response_code' => $response_code,
'message' => $tinyurl_decode->errors[0],
);
return $response;
}
} else {
$tinyurl_request = wp_remote_post( 'https://tinyurl.com/api-create.php?url=' . $home_url );
$response_code = wp_remote_retrieve_response_code( $tinyurl_request );
$tinyurl = wp_remote_retrieve_body( $tinyurl_request );
if ( 200 === (int) $response_code ) {
if ( empty( $get_url ) || $tinyurl !== $get_url ) {
update_post_meta( $item_id, 'tinyurl_link', $tinyurl );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
}
$response = array(
'response_code' => $response_code,
'message' => 'Exists',
);
return $response;
} else {
$response = array(
'response_code' => $response_code,
'message' => $tinyurl,
);
return $response;
}
}
}
/**
* Validates tinyurl.
*
* @param int $item item_id of product.
* @return string
*/
public function cklph_wdl_check_tinyurl( $item ) {
$tinyurl_domain = get_option( 'tinyurl_domain' );
$access_token = get_option( 'tinyurl_access_token_options' );
if ( ! empty( $tinyurl_domain ) && empty( $access_token ) ) {
return '401-tinyurl';
}
$tinyurl_response = $this->cklph_wdl_get_tinyurl( $item );
$tinyurl_response_code = $tinyurl_response['response_code'];
$tinyurl_response_body = $tinyurl_response['message'];
if ( 422 === (int) $tinyurl_response_code ) {
return '422-tinyurl';
}
switch ( $tinyurl_response_body ) {
case 'OK':
return 201;
case 'Exists':
return 200;
default:
$i18n_code = empty( $tinyurl_response_code ) ? '0' : $tinyurl_response_code;
$i18n_body = empty( $tinyurl_response_body ) ? 'Unknown Error.' : $tinyurl_response_body;
/*
* Translators: %1$d: tinyurl response code.
* Translators: %2$s: tinyurl error message.
*/
return sprintf( __( 'An Error has occured. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) );
}
}
/**
* Returns the headers based from the parameters
*
* @param string $host The host API.
* @param string $access_token The authorization token from the API.
* @param string $content_type Specifiy the media tpye of the resource.
* @return array
*/
protected function cklph_wdl_get_api_headers( $host, $access_token, $content_type ) {
return array(
'Host' => $host,
'Authorization' => 'Bearer ' . $access_token,
'Content-Type' => $content_type,
);
}
/**
* Http request GET the group_guid for bitly url.
*
* @return @group_guid | @guid_response
*/
protected function cklph_wdl_group_guid() {
$access_token = get_option( 'bitly_access_token_options' );
$headers = $this->cklph_wdl_get_api_headers( 'api-ssl.bitly.com', $access_token, 'application/json' );
$guid_request = wp_remote_get(
'https://api-ssl.bitly.com/v4/groups',
array(
'timeout' => 0,
'headers' => $headers,
)
);
$guid_response_code = wp_remote_retrieve_response_code( $guid_request );
$guid_body = wp_remote_retrieve_body( $guid_request );
$guid_json = json_decode( $guid_body );
if ( 200 !== (int) $guid_response_code ) {
$guid_response = array(
'response_code' => $guid_response_code,
'message' => $guid_json->message,
);
return $guid_response;
}
$guid_response = array(
'response_code' => $guid_response_code,
'message' => $guid_json->groups[0]->guid,
);
return $guid_response;
}
/**
* Converts a url into a bitly link
*
* @param int $item_id item_id_id of product.
* @return @response return the error response and message if error occured.
*/
protected function cklph_wdl_get_bitly( $item_id ) {
$access_token = get_option( 'bitly_access_token_options' );
$headers = $this->cklph_wdl_get_api_headers( 'api-ssl.bitly.com', $access_token, 'application/json' );
$long_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
$bitly_payload = array(
'long_url' => $long_url,
'group_guid' => $this->cklph_wdl_group_guid()['message'],
);
$bitly_payload_encode = wp_json_encode( $bitly_payload );
$bitly_request = wp_remote_post(
'https://api-ssl.bitly.com/v4/shorten',
array(
'method' => 'POST',
'headers' => $headers,
'body' => $bitly_payload_encode,
)
);
$response_code = wp_remote_retrieve_response_code( $bitly_request );
$bitly_body = wp_remote_retrieve_body( $bitly_request );
$bitly_decode = json_decode( $bitly_body );
if ( 201 < (int) $response_code ) {
$response = array(
'response_code' => $response_code,
'message' => $bitly_decode->message,
);
return $response;
}
if ( 200 === (int) $response_code || 201 === (int) $response_code ) {
$get_url = get_post_meta( $item_id, 'bitly_link', true );
if ( empty( $get_url ) || $get_url !== $bitly_decode->link ) {
update_post_meta( $item_id, 'bitly_link', $bitly_decode->link );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
}
$response = array(
'response_code' => $response_code,
'message' => 'Exists',
);
return $response;
}
}
/**
* Validates the bitly API Key.
*
* @param int $item item_id of product.
* @return string
*/
public function cklph_wdl_check_bitly( $item ) {
$access_token = get_option( 'bitly_access_token_options' );
if ( empty( $access_token ) ) {
return '401-bitly';
}
$group_guid_response = $this->cklph_wdl_group_guid();
$guid_response_code = $group_guid_response['response_code'];
$guid_response_body = $group_guid_response['message'];
if ( 200 !== (int) $guid_response_code ) {
$i18n_code = empty( $guid_response_code ) ? '0' : $guid_response_code;
$i18n_body = empty( $guid_response_body ) ? 'Unknown error.' : $guid_response_body;
/*
* Translators: %1$d: bitly response code.
* Translators: %2$s: bitly error message.
*/
return sprintf( __( 'An Error has occured while validating API key for Bitly. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) );
}
$bitly_response = $this->cklph_wdl_get_bitly( $item );
$bitly_response_code = $bitly_response['response_code'];
$bitly_response_body = $bitly_response['message'];
switch ( $bitly_response_body ) {
case 'OK':
return 201;
case 'Exists':
return 200;
default:
$i18n_code = empty( $bitly_response_code ) ? '0' : $bitly_response_code;
$i18n_body = empty( $bitly_response_body ) ? 'Unknown Error.' : $bitly_response_body;
/*
* Translators: %1$d: bitly response code.
* Translators: %2$s: bitly error message.
*/
return sprintf( __( 'An Error has occured while validating API key for Bitly. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) );
}
}
/**
* Converts a url into joturl.
*
* @param int $item_id item_id of product.
* @return $response if error occur.
*/
protected function cklph_wdl_get_joturl( $item_id ) {
$get_url = get_post_meta( $item_id, 'joturl_link', true );
if ( empty( $get_url ) ) {
$joturl_private = get_option( 'joturl_private' );
$joturl_public = get_option( 'joturl_public' );
$home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
$joturl_request = wp_remote_post( 'https://api.joturl.com/a/v1/shorten?login=' . $joturl_public . '&key=' . $joturl_private . '&url=' . $home_url . '' );
$response_code = wp_remote_retrieve_response_code( $joturl_request );
$joturl_body = wp_remote_retrieve_body( $joturl_request );
$joturl_decode = json_decode( $joturl_body );
if ( 200 === (int) $response_code ) {
$joturl = $joturl_decode->result->short_url;
update_post_meta( $item_id, 'joturl_link', $joturl );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
} else {
$response = array(
'response_code' => $response_code,
'message' => $joturl_decode->status->text,
);
return $response;
}
}
$response = array(
'response_code' => 409,
'message' => 'Exists',
);
return $response;
}
/**
* Validates the JotURL API Keys.
*
* @param int $item_id item_id of product.
* @return string
*/
public function cklph_wdl_check_joturl( $item_id ) {
$joturl_private_key = get_option( 'joturl_private' );
$joturl_public_key = get_option( 'joturl_public' );
if ( empty( $joturl_private_key ) || empty( $joturl_public_key ) ) {
return '401-joturl';
}
$joturl_response = $this->cklph_wdl_get_joturl( $item_id );
$joturl_response_code = $joturl_response['response_code'];
$joturl_response_body = $joturl_response['message'];
switch ( $joturl_response_body ) {
case 'OK':
return 201;
case 'Exists':
return 200;
default:
$i18n_code = empty( $joturl_response_code ) ? '0' : $joturl_response_code;
$i18n_body = empty( $joturl_response_body ) ? 'Unknown Error.' : $joturl_response_body;
/*
* Translators: %1$d: joturl response code.
* Translators: %2$s: joturl error message.
*/
return sprintf( __( 'An Error has occured while validating API keys for JotURL. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) );
}
}
/**
* Force convert a url into joturl.
*
* @param int $item_id item_id of product.
* @return $response if error occur.
*/
protected function cklph_wdl_force_get_joturl( $item_id ) {
$joturl_private = get_option( 'joturl_private' );
$joturl_public = get_option( 'joturl_public' );
$home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
$joturl_request = wp_remote_post( 'https://api.joturl.com/a/v1/shorten?login=' . $joturl_public . '&key=' . $joturl_private . '&url=' . $home_url . '' );
$response_code = wp_remote_retrieve_response_code( $joturl_request );
$joturl_body = wp_remote_retrieve_body( $joturl_request );
$joturl_decode = json_decode( $joturl_body );
if ( 200 === (int) $response_code ) {
$joturl = $joturl_decode->result->short_url;
update_post_meta( $item_id, 'joturl_link', $joturl );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
} else {
$response = array(
'response_code' => $response_code,
'message' => $joturl_decode->status->text,
);
return $response;
}
}
/**
* Validates the JotURL API Keys when JotURL is forced to refresh link.
*
* @param int $item_id item_id of product.
* @return string
*/
public function cklph_wdl_check_force_joturl( $item_id ) {
$joturl_private_key = get_option( 'joturl_private' );
$joturl_public_key = get_option( 'joturl_public' );
if ( empty( $joturl_private_key ) || empty( $joturl_public_key ) ) {
return '401-joturl';
}
$joturl_response = $this->cklph_wdl_force_get_joturl( $item_id );
$joturl_response_code = $joturl_response['response_code'];
$joturl_response_body = $joturl_response['message'];
switch ( $joturl_response_body ) {
case 'OK':
return 201;
case 'Exists':
return 200;
default:
$i18n_code = empty( $joturl_response_code ) ? '0' : $joturl_response_code;
$i18n_body = empty( $joturl_response_body ) ? 'Unknown Error.' : $joturl_response_body;
/*
* Translators: %1$d: joturl response code.
* Translators: %2$s: joturl error message.
*/
return sprintf( __( 'An Error has occured while validating API keys for JotURL. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) );
}
}
/**
* Converts checkout links to YoURLS.
*
* @param int $item_id id of product.
* @return @response if error occur.
*/
protected function cklph_wdl_get_yourls( $item_id ) {
$get_url = get_post_meta( $item_id, 'yourls_link', true );
$home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' );
$signature = get_option( 'yourls_signature' );
$domain = get_option( 'yourls_domain' );
$yourls_request = wp_remote_post( 'http://' . $domain . '/yourls-api.php?format=json&action=shorturl&signature=' . $signature . '&url=' . $home_url . '' );
$response_code = wp_remote_retrieve_response_code( $yourls_request );
$yourls_body = wp_remote_retrieve_body( $yourls_request );
$yourls_decode = json_decode( $yourls_body );
switch ( $response_code ) {
case 200:
if ( empty( $get_url ) || $yourls_decode->shorturl !== $get_url ) {
update_post_meta( $item_id, 'yourls_link', $yourls_decode->shorturl );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
}
$response = array(
'response_code' => $response_code,
'message' => 'Exists',
);
return $response;
case 400:
if ( empty( $get_url ) || $yourls_decode->shorturl !== $get_url ) {
update_post_meta( $item_id, 'yourls_link', $yourls_decode->shorturl );
$response = array(
'response_code' => $response_code,
'message' => 'OK',
);
return $response;
}
$response = array(
'response_code' => $response_code,
'message' => 'Exists',
);
return $response;
case 0:
$response = array(
'response_code' => 404,
'message' => 'Site not found',
);
return $response;
case 403:
$response = array(
'response_code' => 403,
'message' => 'Please use a valid YoURLS Signature',
);
return $response;
default:
$response = array(
'response_code' => $response_code,
'message' => $yourls_decode->message,
);
return $response;
}
}
/**
* Validates the YoURLS Signature.
*
* @param int $item item_id of product.
* @return string
*/
public function cklph_wdl_check_yourls( $item ) {
$yourls_signature = get_option( 'yourls_signature' );
$yourls_domain = get_option( 'yourls_domain' );
if ( empty( $yourls_signature ) || empty( $yourls_domain ) ) {
return '401-yourls';
}
$yourls_response = $this->cklph_wdl_get_yourls( $item );
$yourls_response_code = $yourls_response['response_code'];
$yourls_response_body = $yourls_response['message'];
switch ( $yourls_response_body ) {
case 'OK':
return 201;
case 'Exists':
return 200;
default:
$i18n_code = empty( $yourls_response_code ) ? '0' : $yourls_response_code;
$i18n_body = empty( $yourls_response_body ) ? 'Unknown Error.' : $yourls_response_body;
/*
* Translators: %1$d: YoURLS response code.
* Translators: %2$s: YoURLS error message.
*/
return sprintf( __( 'An Error has occured while validating YoURLS credentials. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) );
}
}
/**
* Get shortlink to use in list tables.
*
* @param int $item_id ID of product.
* @return @turl shortlink
*/
public function cklph_wdl_get_link( $item_id ) {
$url_option = get_option( 'url_options' );
$home_url = esc_url( home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' ) );
switch ( $url_option ) {
case 'tinyurl':
$get_url = get_post_meta( $item_id, 'tinyurl_link', true );
$url = empty( $get_url ) ? $home_url : $get_url;
break;
case 'bitly':
$get_url = get_post_meta( $item_id, 'bitly_link', true );
$url = empty( $get_url ) ? $home_url : $get_url;
break;
case 'yourls':
$get_url = get_post_meta( $item_id, 'yourls_link', true );
$url = empty( $get_url ) ? $home_url : $get_url;
break;
case 'joturl':
$get_url = get_post_meta( $item_id, 'joturl_link', true );
$url = empty( $get_url ) ? $home_url : $get_url;
break;
case 'none':
default:
$url = $home_url;
}
return $url;
}
}
}

View file

@ -0,0 +1,179 @@
<?php
/**
* A class that gets data from woocommerce products.
*
* Defines the product queries for the plugin
*
* @link https://chykalophia.com/woocommerce-direct-links
* @since 0.1.0
*
* @package Woocommerce_Direct_Links
* @subpackage Woocommerce_Direct_Links/admin/partials
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
if ( ! class_exists( 'Cklph_Wdl_Wc_Products' ) ) {
/**
* Handles all queries concerning woocommerce products.
*/
class Cklph_Wdl_Wc_Products {
/**
* Gets all product ids to create shortlinks.
*
* @return @product_lists array of IDs
*/
public function get_all_wc_product_ids() {
$product_lists = array();
$args = $this->get_wc_query_args( 'publish', -1, array( 'simple', 'variable', 'subscription', 'variable-subscription' ), 'ids' );
$product_ids = wc_get_products( $args );
foreach ( $product_ids as $product_id ) {
$prod_var = new WC_Product_Variable( $product_id );
$variations = $prod_var->get_available_variations( 'objects' );
if ( ! empty( $variations ) ) {
foreach ( $variations as $variation ) {
$product_lists[] = $variation->get_id();
}
} else {
$product_lists[] = $product_id;
}
}
return $product_lists;
}
/**
* Gets the necessarry product data to display in list table.
*
* @param array $args Query arguments for wc_get_products.
* @return array
*/
public function get_all_wc_products_data( $args ) {
$product_list = array();
$products = wc_get_products( $args );
foreach ( $products as $product ) {
// Get product details.
$prod_id = $product->get_id();
$prod_link = $product->get_permalink();
$prod_thumbnail = $product->get_image( array( 50, 50 ) );
$prod_title = $product->get_title();
$category = wc_get_product_category_list( $prod_id );
$prod_sku = $product->get_sku();
$prod_var = new WC_Product_Variable( $prod_id );
$variations = $prod_var->get_available_variations( 'objects' );
if ( count( $variations ) > 1 ) {
foreach ( $variations as $vars ) {
// Get specific variation details.
$var_id = $vars->get_id();
$var_sku = $vars->get_sku();
$var_thumbnail = $vars->get_image( array( 50, 50 ) );
$var_attributes = wc_get_formatted_variation( $vars, 'true' );
$product_list[] = array(
'ID' => esc_attr( $prod_id ),
'variation_id' => esc_attr( $var_id ),
'post_title' => esc_attr( $product->get_formatted_name() ),
'sku' => ! empty( $var_sku ) ? esc_attr( $var_sku ) : '<span class="na">&ndash;</span>',
'attrib' => esc_attr( $var_attributes ),
'thumb' => $var_thumbnail,
'category' => $category,
'guid' => esc_attr( $prod_link ),
);
}
} else {
$product_list[] = array(
'ID' => esc_attr( $prod_id ),
'post_title' => esc_attr( $prod_title ),
'sku' => ! empty( esc_attr( $prod_sku ) ) ? esc_attr( $prod_sku ) : '<span class="na">&ndash;</span>',
'attrib' => '<span class="na">&ndash;</span>',
'thumb' => $prod_thumbnail,
'category' => $category,
'guid' => esc_attr( $prod_link ),
);
}
}
return $product_list;
}
/**
* Gets the necessarry product data to display on product meta box.
*
* @return array
*/
public function get_metabox_product_data() {
$product_list = array();
$product = wc_get_product( get_the_ID() );
$product_variable = new WC_Product_Variable( $product );
$variations = $product_variable->get_available_variations( 'objects' );
if ( ! empty( $variations ) ) {
foreach ( $variations as $vars ) {
$product_list[] = array(
'ID' => esc_attr( $vars->get_id() ),
'post_title' => esc_attr( $product->get_formatted_name() ),
'sku' => esc_attr( $vars->get_sku() ),
'attrib' => esc_attr( wc_get_formatted_variation( $vars, 'true' ) ),
'thumb' => $vars->get_image( array( 50, 50 ) ),
'guid' => esc_attr( $product->get_permalink() ),
);
}
} else {
$product_list[] = array(
'ID' => esc_attr( get_the_ID() ),
'post_title' => esc_attr( get_the_title() ),
'sku' => esc_attr( $product->get_sku() ),
'attrib' => '<span class="na">&ndash;</span>',
'thumb' => $product->get_image( array( 100, 100 ) ),
'guid' => esc_attr( $product->get_permalink() ),
);
}
return $product_list;
}
/**
* Returns the ID of the most recent product.
*
* @return string Product ID.
*/
public function get_single_wc_product_id() {
$args = $this->get_wc_query_args( 'publish', 1, array( 'simple', 'variable', 'subscription', 'variable-subscription' ), 'ids' );
$product = wc_get_products( $args );
return $product;
}
/**
* Returns the query args for woocommerce products.
*
* @param string $status The post status.
* @param string $limit The number of products to be returned.
* @param array $type The type of products to be returned.
* @param string $return The return type.
* @return array
*/
public function get_wc_query_args( $status, $limit, $type = array(), $return = 'objects' ) {
return array(
'status' => $status,
'limit' => $limit,
'type' => $type,
'return' => $return,
);
}
}
}

View file

@ -0,0 +1,462 @@
<?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_WP_List_Table' ) ) {
require_once 'class-wp-list-table.php';
}
if ( ! class_exists( 'Cklph_Wdl_List_Table' ) ) {
/**
* Edited list table class
*/
class Cklph_Wdl_List_Table extends Cklph_WP_List_Table {
/**
* Product_List_Table constructor.
*/
public function __construct() {
// Set parent defaults.
parent::__construct(
array(
'singular' => 'product',
'plural' => 'products',
'ajax' => false,
)
);
}
/**
* Defines the columns to use in your listing table
*
* @return array
*/
public function manage_columns() {
// Columns for admin list table.
$columns = array(
'thumb' => '<span class="wc-image tips" data-tip="' . esc_attr__( 'Image', 'woocommerce-direct-links' ) . '">' . __( 'Image', 'woocommerce-direct-links' ) . '</span>',
'post_title' => __( 'Product Name', 'woocommerce-direct-links' ),
'sku' => __( 'SKU', 'woocommerce-direct-links' ),
'attrib' => __( 'Attributes', 'woocommerce-direct-links' ),
'category' => __( 'Category', 'woocomerce-direct-links' ),
'guid' => __( 'Add to Cart Link', 'woocommerce-direct-links' ),
'copy' => __( 'Copy Link', 'woocommerce-direct-links' ),
'refresh' => __( 'Refresh Link', 'woocommerce-direct-links' ),
);
// remove category column for metabox list table.
if ( 'product' === $this->screen->id ) {
array_splice( $columns, 4, 1 );
}
return $columns;
}
/**
* Override the parent columns method for screen options columns.
*
* @return Array
*/
public function get_columns() {
// Columns for screen options.
$columns = array(
'thumb' => __( 'Thumbnail', 'woocommerce-direct-links' ),
'sku' => __( 'SKU', 'woocommerce-direct-links' ),
'attrib' => __( 'Attributes', 'woocommerce-direct-links' ),
'category' => __( 'Category', 'woocomerce-direct-links' ),
);
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns() {
return array();
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data.
* @param String $column_name Current column name.
*
* @return Mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'sku':
case 'attrib':
case 'thumb':
case 'category':
return $item[ $column_name ];
default:
return print_r( $item, true );
}
}
/**
* Prepare the items for the table to process
*
* @return void
*/
public function prepare_items() {
$search = '';
$category = '';
$primary = 'post_title';
$columns = $this->manage_columns();
$sortable = $this->get_sortable_columns();
$get_hidden_columns = $this->get_hidden_columns();
$get_hidden_options = get_user_option( 'manage' . $this->screen->id . 'columnshidden' );
$hidden = is_array( $get_hidden_options ) ? $get_hidden_options : $get_hidden_columns;
if ( isset( $_GET['s'] ) ) {
$search = sanitize_text_field( wp_unslash( $_GET['s'] ) );
}
if ( isset( $_GET['product_cat'] ) ) {
$category = sanitize_text_field( wp_unslash( $_GET['product_cat'] ) );
}
$data = $this->get_data( $search, $category );
usort( $data, array( &$this, 'sort_data' ) );
$per_page = get_user_meta( get_current_user_id(), 'cklph_wdl_per_page', true );
if ( empty( $per_page ) ) {
$per_page = $this->screen->get_option( 'per_page', 'default' );
}
$current_page = $this->get_pagenum();
$total_items = count( $data );
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
)
);
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->_column_headers = array( $columns, $hidden, $sortable, $primary );
if ( 'product' === $this->screen->id ) {
$this->_column_headers = array( $columns, $hidden, $sortable, $primary );
}
$this->items = $data;
}
/**
* Retrieves product data. Inlcudes search and filter functions
*
* @param string $search search filter.
* @param string $category category filter.
* @return Array
*/
public function get_data( $search = '', $category = '' ) {
$wc_products = new Cklph_Wdl_Wc_Products();
$productlist = array();
if ( isset( $search ) ) {
$args = array(
'status' => 'publish',
'limit' => -1,
'orderby' => 'title',
'order' => 'ASC',
'type' => array( 'simple', 'variable', 'subscription', 'variable-subscription' ),
's' => $search,
'meta_query' => array(
array(
'key' => '_sku',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => '_title',
'value' => $search,
'compare' => 'LIKE',
),
),
);
} else {
$args = array(
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'limit' => -1,
'type' => array( 'simple', 'variable', 'subscription', 'variable-subscription' ),
);
}
if ( isset( $category ) ) {
$args['category'] = $category;
}
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
$productlist = $wc_products->get_all_wc_products_data( $args );
} else {
$productlist = $wc_products->get_metabox_product_data();
}
return $productlist;
}
/**
* Custom row display for Post title
*
* @param int $item item data.
* @return String
*/
protected function column_post_title( $item ) {
$item_id = $item['ID'];
$item_guid = $item['guid'];
$item_title = $item['post_title'];
// Only shows the id / variation id as actions in meta box.
if ( 'product' === $this->screen->id ) {
$actions = array(
'ID' => sprintf(
'<b>%s: %s</b>',
__( 'ID', 'woocommerce-direct-links' ),
esc_attr( $item_id )
),
);
if ( isset( $item['variation_id'] ) ) {
array_splice(
$actions,
1,
0,
array(
'variation_id' => sprintf(
'%s: %s',
__( 'Variation ID', 'woocommerce-direct-links' ),
esc_attr( $item['variation_id'] )
),
)
);
}
} else {
$actions = array(
'ID' => sprintf(
'<b>%s: %s</b>',
__( 'ID', 'woocommerce-direct-links' ),
esc_attr( $item_id )
),
'edit' => sprintf(
'<a href="%s">%s</a>',
esc_attr( get_edit_post_link( $item_id ) ),
__( 'Edit', 'woocommerce-direct-links' )
),
'view' => sprintf(
'<a href="%s">%s</a>',
esc_attr( $item_guid ),
__( 'View', 'woocommerce-direct-links' )
),
);
if ( isset( $item['variation_id'] ) ) {
array_splice(
$actions,
1,
0,
array(
'variation_id' => sprintf(
'%s: %s',
__( 'Variation ID', 'woocommerce-direct-links' ),
esc_attr( $item['variation_id'] )
),
)
);
}
}
return sprintf(
'<a href="%1$s"><strong>%2$s</strong></a>%3$s',
esc_attr( $item_guid ),
esc_attr( $item_title ),
$this->row_actions( $actions )
);
}
/**
* Custom row display for Copy column. Prints the copy button
*
* @param int $item item data.
* @return String
*/
protected function column_copy( $item ) {
$id = array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : $item['ID'];
return sprintf(
'<div class="tooltip">
<button class="button-primary" type="button" onclick="cklph_wdl_copyurl(%1$d)" onmouseout="cklph_wdl_out(%1$d)">
<span class="tooltiptext" id="myTooltip-%1$d">Copy</span>
%2$s
</button>
</div>',
esc_attr( $id ),
__( 'Copy Link', 'woocommerce-direct-links' ),
);
}
/**
* Custom row display for Refresh Link Button.
*
* @param array $item Item data.
* @return String
*/
protected function column_refresh( $item ) {
$id = array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : $item['ID'];
return sprintf(
'<button class="button-primary cklph-wdl-refresh" id="cklph-wdl-btn-%1$d" type="button" onclick=cklph_wdl_rest(%1$d)>
<span class="cklph-wdl-btn-text">%2$s</span>
</button>',
esc_attr( $id ),
__( 'Refresh Link', 'woocommerce-direct-links' ),
);
}
/**
* Custom row display for the add-to-cart link
*
* @param int $item item data.
* @return String
*/
protected function column_guid( $item ) {
$id = array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : $item['ID'];
$shortlink = new Cklph_Wdl_Shortlinks();
$turl = $shortlink->cklph_wdl_get_link( $id );
return sprintf(
'<input id="cklph-wdl-url-%1$s" class="cklph-wdl-input" type="text" size=30 value="%2$s" readonly>',
esc_attr( $id ),
esc_url( $turl )
);
}
/**
* Adds custom filters at the top.
*
* @param string $which identify if content is top or bottom.
*/
public function extra_tablenav( $which ) {
?>
<div class="alignleft actions">
<?php
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
if ( 'top' === $which ) {
_e( '<form method="GET"><input type="hidden" name="page" value="' . esc_attr( $_REQUEST['page'] ) . '">' );
$args = array();
if ( isset( $_GET['product_cat'] ) ) {
$args['selected'] = sanitize_text_field( $_GET['product_cat'] );
}
wc_product_dropdown_categories( $args );
$output = ob_get_clean();
if ( ! empty( $output ) ) {
echo $output;
submit_button( __( 'Filter', 'woocommerce-direct-links' ), '', 'filter_action', false, array( 'id' => 'site-query-submit' ) );
_e( '</form>' );
}
}
}
?>
</div>
<?php
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns() {
return array(
'post_title' => array( 'post_title', false ),
'category' => array( 'category', false ),
);
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data( $a, $b ) {
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
// Set defaults.
$orderby = 'post_title';
$order = 'asc';
// If orderby is set, use this as the sort column.
if ( ! empty( $_GET['orderby'] ) ) {
$orderby = $_GET['orderby'];
}
// If order is set use this as the order.
if ( ! empty( $_GET['order'] ) ) {
$order = $_GET['order'];
}
$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
if ( 'asc' === $order ) {
return $result;
}
return -$result;
}
}
/**
* Prints the search box of the List Table
*
* @param string $text search query.
* @param int $input_id input id.
*/
public function search_box( $text, $input_id ) {
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
_e( '<form method="GET"><input type="hidden" name="page" value="'.esc_attr( $_REQUEST['page'] ).'">' );
parent::search_box( $text, $input_id );
_e( '</form>' );
}
}
}
}

File diff suppressed because it is too large Load diff