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,884 @@
<?php
/**
* The admin-specific functionality of WDL.
*
* Defines the plugin name, version, enqueues the necessarry css and js files for the admin page,
* defines the admin page, submenu page, and the settings page.
*
* @package Woocommerce_Direct_Links
* @subpackage Woocommerce_Direct_Links/includes
* @author Joel Sanguenza <joel@chykalophia.com>,
* Lord Francis Navarro <francis@chykalophia.com>
* @link https://chykalophia.com/woocommerce-direct-links
* @since 0.1.0
*/
if ( ! class_exists( 'Cklph_Wdl_Admin' ) ) {
/**
* Plugin admin/settings class
*/
class Cklph_Wdl_Admin {
/**
* The ID of wdl.
*
* @since 0.1.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of wdl.
*
* @since 0.1.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 0.1.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
/**
* Register the stylesheets for the admin area.
*
* @since 0.1.0
*/
public function cklph_wdl_enqueue_styles() {
$screen = get_current_screen();
if ( 'toplevel_page_cklph-wdl-admin' === $screen->id ) {
wp_enqueue_style( $this->plugin_name, CKLPH_WDL_URL . 'admin/css/cklph-wdl-admin.css', array(), $this->version, 'all' );
}
if ( 'product' === $screen->id ) {
wp_enqueue_style( $this->plugin_name, CKLPH_WDL_URL . 'admin/css/cklph-wdl-metabox.css', array(), $this->version, 'all' );
}
if ( 'woocommerce-direct-links_page_cklph-wdl-settings' === $screen->id ) {
wp_enqueue_style( $this->plugin_name, CKLPH_WDL_URL . 'admin/css/cklph-wdl-settings.css', array(), $this->version, 'all' );
}
}
/**
* Register the JavaScript for the admin area.
*
* @since 0.1.0
*/
public function cklph_wdl_enqueue_scripts() {
$screen = get_current_screen();
if ( 'toplevel_page_cklph-wdl-admin' === $screen->id || 'product' === $screen->id ) {
wp_enqueue_script( $this->plugin_name, CKLPH_WDL_URL . 'admin/js/cklph-wdl-admin.js', array( 'jquery' ), $this->version, false ); // Load js scripts for admin and product pages.
// Localized scripts for wp rest api nonce for admin and product pages.
wp_localize_script(
$this->plugin_name,
'cklph_wdl',
array(
'product-joturl' => home_url( '/wp-json/cklph-wdl/v1/product-joturl/' ),
'product' => home_url( '/wp-json/cklph-wdl/v1/product/' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
),
);
}
if ( 'woocommerce-direct-links_page_cklph-wdl-settings' === $screen->id ) {
wp_enqueue_script( $this->plugin_name, CKLPH_WDL_URL . 'admin/js/cklph-wdl-settings.js', array( 'jquery' ), $this->version, false ); // Load js scripts for settings page.
// Localized scripts for wp rest api nonce for settings page.
wp_localize_script(
$this->plugin_name,
'cklph_wdl',
array(
'shortlink' => home_url( '/wp-json/cklph-wdl/v1/shortlink/' ),
'product' => home_url( '/wp-json/cklph-wdl/v1/product/' ),
'tinyurl' => home_url( '/wp-json/cklph-wdl/v1/tinyurl/' ),
'yourls' => home_url( '/wp-json/cklph-wdl/v1/yourls/' ),
'joturl' => home_url( '/wp-json/cklph-wdl/v1/joturl/' ),
'bitly' => home_url( '/wp-json/cklph-wdl/v1/bitly/' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
),
);
}
}
/**
* Validate if woocommerce is active
*
* @since 0.1.0
*/
public function cklph_wdl_has_woocommerce() {
if ( is_admin() && current_user_can( 'activate_plugins' ) && ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
add_action( 'admin_notices', array( $this, 'cklph_wdl_no_woocommerce_error' ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
deactivate_plugins( CKLPH_WDL_BASE );
}
}
/**
* Error message if woocommerce is not installed and activated
*
* @since 0.1.0
*/
public function cklph_wdl_no_woocommerce_error() {
echo '<div class="notice notice-error settings-error is-dismissible">
<p><strong>' . esc_html__( 'Cannot activate WooCommerce Direct Links without WooCommerce installed and activated.', 'woocommerce-direct-links' ) . ' </strong></p>
</div>';
}
/**
* Adds the admin and settings pages
*
* @since 0.1.0
* @return void for admin page
*/
public function cklph_wdl_add_admin_pages() {
// If WooCommerce is activated run admin menu and submenu page.
if ( class_exists( 'woocommerce' ) ) {
$admin_page = add_menu_page(
__( 'Woocommerce Direct Links', 'woocommerce-direct-links' ),
__( 'Woocommerce Direct Links', 'woocommerce-direct-links' ),
'manage_options',
'cklph-wdl-admin',
array( $this, 'cklph_wdl_admin_page' ),
'dashicons-store',
110
);
$with_subpage = add_submenu_page(
'cklph-wdl-admin',
__( 'Woocommerce Direct Links', 'woocommerce-direct-links' ),
__( 'WDL Table', 'woocommerce-direct-links' ),
'manage_options',
'cklph-wdl-admin',
array( $this, 'cklph_wdl_admin_page' ),
);
add_submenu_page(
'cklph-wdl-admin',
__( 'Woocommerce Direct Links', 'woocommerce-direct-links' ),
__( 'Settings', 'woocommerce-direct-links' ),
'manage_options',
'cklph-wdl-settings',
array( $this, 'cklph_wdl_settings_page' )
);
add_action( "load-{$admin_page}", array( $this, 'cklph_wdl_screen_options' ) );
array_merge( array( $with_subpage, $admin_page ) );
}
}
/**
* Callback for admin page
*
* @since 0.1.0
* @return void callback path
*/
public function cklph_wdl_admin_page() {
include_once CKLPH_WDL_PATH . 'admin/partials/cklph-wdl-admin-page.php';
}
/**
* Callback for settings page
*
* @since 0.1.0
* @return void callback path
*/
public function cklph_wdl_settings_page() {
include_once CKLPH_WDL_PATH . 'admin/partials/cklph-wdl-settings-page.php';
}
/**
* Add meta box in WooCommerce products page
*
* @since 0.1.0
*/
public function cklph_wdl_add_meta_box() {
$product = wc_get_product( get_the_ID() );
$screen = get_current_screen();
if ( 'product' === $screen->id ) {
if ( $product->is_type( array( 'simple', 'variable', 'subscription' ) ) && 'publish' === $product->get_status() ) {
add_meta_box(
'cklph-wdl-product-meta-box',
__( 'WooCommerce Direct Links', 'woocommerce-direct-links' ),
array( $this, 'cklph_wdl_admin_page' ),
'product'
);
}
}
}
/**
* Screen options for admin page.
*
* @since 0.1.0
* @return void
*/
public function cklph_wdl_screen_options() {
$screen = get_current_screen();
if ( 'toplevel_page_cklph-wdl-admin' !== $screen->id || ! is_object( $screen ) ) {
return;
}
// Array for per page in screen-options.
$per_page_args = array(
'default' => 5,
'option' => 'cklph_wdl_per_page',
);
add_screen_option( 'per_page', $per_page_args );
new Cklph_Wdl_List_Table();
}
/**
* Rest Api initialize.
*
* @return void
*/
public function cklph_wdl_rest_api() {
if ( class_exists( 'Cklph_Wdl_Rest' ) ) {
$controller = new Cklph_Wdl_Rest();
$controller->cklph_wdl_rest_route();
}
}
/**
* Screen options for pagination on list table.
*
* @since 0.1.0
* @param bool|int $status Screen option value. Default false to skip.
* @param string $option The option name.
* @param int $value The number of rows to use.
* @return $value
*/
public function cklph_wdl_set_pagination_option( $status, $option, $value ) {
if ( 'cklph_wdl_per_page' === $option ) {
return $value;
}
}
/**
* Adding plugin links
*
* @since 0.1.0
* @param array $actions action_links array.
* @return $actions action links in plugin page.
*/
public function cklph_wdl_add_action_links( $actions ) {
$settings = array( '<a href="' . esc_url( admin_url( 'admin.php?page=cklph-wdl-settings' ) ) . '">Settings</a>' );
if ( class_exists( 'woocommerce' ) ) {
$actions = array_merge( $settings, $actions );
}
return $actions;
}
/**
* Initialize settings
*
* @since 0.1.0
* @return void for settings
*/
public function cklph_wdl_settings_init() {
// setup settings section.
add_settings_section(
'settings_section', // section id.
__( 'Settings', 'woocommerce-direct-link' ), // section title.
array( $this, 'cklph_wdl_settings' ), // callback.
'cklph-wdl-settings' // page.
);
// register radio field for URL Shortener.
register_setting(
'cklph-wdl-option-group',
'url_options',
array(
'type' => 'string',
'default' => 'none',
'sanitize_callback' => 'sanitize_text_field',
)
);
// add radio field for URL Shortener.
add_settings_field(
'url_options',
__( 'URL Shortener', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_url_shortener' ),
'cklph-wdl-settings',
'settings_section',
array(
'label_for' => 'url_options',
'class' => 'plugin_row',
)
);
$this->cklph_wdl_bitly_option();
$this->cklph_wdl_yourls_option();
$this->cklph_wdl_joturl_option();
$this->cklph_wdl_tinyurl_option();
}
/**
* Add & register settings section & field for Tinyurl.
*
* @return void
*/
public function cklph_wdl_tinyurl_option() {
// setup tinyurl settings section.
add_settings_section(
'tinyurl_section', // section id.
__( 'TinyURL', 'woocommerce-direct-link' ), // section title.
array( $this, 'cklph_wdl_settings_tinyurl' ), // callback.
'cklph-wdl-tinyurl-settings' // page.
);
// register field for tinyurl domain.
register_setting(
'cklph-wdl-tinyurl-group',
'tinyurl_domain',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// register field for tinyurl access token.
register_setting(
'cklph-wdl-tinyurl-group',
'tinyurl_access_token_options',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// add input field for tinyurl domain.
add_settings_field(
'tinyurl_domain',
__( 'TinyURL Domain', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_tinyurl_domain' ),
'cklph-wdl-tinyurl-settings',
'tinyurl_section',
array(
'label_for' => 'tinyurl_domain',
'class' => 'plugin_row',
)
);
// add input field for tinyurl access token.
add_settings_field(
'tinyurl_access_token_options',
__( 'TinyURL Access Token', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_tinyurl_api_key' ),
'cklph-wdl-tinyurl-settings',
'tinyurl_section',
array(
'label_for' => 'tinyurl_access_token_options',
'class' => 'plugin_row',
)
);
}
/**
* Add & register settings section & field for bitly.
*
* @return void
*/
public function cklph_wdl_bitly_option() {
// setup bitly settings section.
add_settings_section(
'bitly_section', // section id.
__( 'Bitly', 'woocommerce-direct-link' ), // section title.
array( $this, 'cklph_wdl_settings_bitly' ), // callback.
'cklph-wdl-bitly-settings' // page.
);
// register field for Bitly access token.
register_setting(
'cklph-wdl-bitly-group',
'bitly_access_token_options',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// add input field for Bitly access token.
add_settings_field(
'bitly_access_token_options',
__( 'Bitly Access Token', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_bitly_api_key' ),
'cklph-wdl-bitly-settings',
'bitly_section',
array(
'label_for' => 'bitly_access_token_options',
'class' => 'plugin_row',
)
);
}
/**
* Add & register settings section & field for yourls.
*
* @return void
*/
public function cklph_wdl_yourls_option() {
// setup yourls settings section.
add_settings_section(
'yourls_section', // section id.
__( 'YoURLS', 'woocommerce-direct-link' ), // section title.
array( $this, 'cklph_wdl_settings_yourls' ), // callback.
'cklph-wdl-yourls-settings' // page.
);
// register field for YoURLS signature.
register_setting(
'cklph-wdl-yourls-group',
'yourls_signature',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// register field for YoURLS domain.
register_setting(
'cklph-wdl-yourls-group',
'yourls_domain',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// add input field for YoURLS domain.
add_settings_field(
'yourls_domain',
__( 'YoURLS Site', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_yourls_domain' ),
'cklph-wdl-yourls-settings',
'yourls_section',
array(
'label_for' => 'yourls_domain',
'class' => 'plugin_row',
)
);
// add input field for YoURLS Signature.
add_settings_field(
'yourls_signature',
__( 'YoURLS Signature', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_yourls_signature' ),
'cklph-wdl-yourls-settings',
'yourls_section',
array(
'label_for' => 'yourls_signature',
'class' => 'plugin_row',
)
);
}
/**
* Add & register settings section & field for JotURL.
*
* @return void
*/
public function cklph_wdl_joturl_option() {
// setup tinyurl settings section.
add_settings_section(
'joturl_section', // section id.
__( 'JotURL', 'woocommerce-direct-link' ), // section title.
array( $this, 'cklph_wdl_settings_joturl' ), // callback.
'cklph-wdl-joturl-settings' // page.
);
// register field for joturl domain.
register_setting(
'cklph-wdl-joturl-group',
'joturl_public',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// register field for joturl access token.
register_setting(
'cklph-wdl-joturl-group',
'joturl_private',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
// add input field for joturl domain.
add_settings_field(
'joturl_public',
__( 'JotURL Public Key', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_joturl_public' ),
'cklph-wdl-joturl-settings',
'joturl_section',
array(
'label_for' => 'joturl_public',
'class' => 'plugin_row',
)
);
// add input field for joturl access token.
add_settings_field(
'joturl_private',
__( 'JotURL Private Key', 'woocommerce-direct-link' ),
array( $this, 'cklph_wdl_joturl_private' ),
'cklph-wdl-joturl-settings',
'joturl_section',
array(
'label_for' => 'joturl_private',
'class' => 'plugin_row',
)
);
}
/**
* Settings general callback
*
* @since 0.1.0
* @param int $args settings callback id.
*/
public function cklph_wdl_settings( $args ) {
return sprintf(
'<p id="$1$s">%2$s</p>',
esc_attr( $args['id'] ),
esc_html_e( 'Choose URL Shortener.', 'woocommerce-direct-link' )
);
}
/**
* Settings tinyurl callback
*
* @since 0.1.0
* @param int $args settings callback id.
*/
public function cklph_wdl_settings_tinyurl( $args ) {
return sprintf(
'<p id="$1$s">%2$s</p>',
esc_attr( $args['id'] ),
esc_html_e( 'Please enter a valid TinyURL API Key or use automatic tinyurl shortener.', 'woocommerce-direct-link' )
);
}
/**
* Settings bitly callback
*
* @since 0.1.0
* @param int $args settings callback id.
*/
public function cklph_wdl_settings_bitly( $args ) {
return sprintf(
'<p id="$1$s">%2$s</p>',
esc_attr( $args['id'] ),
esc_html_e( 'Please enter a valid Bitly API Key.', 'woocommerce-direct-link' )
);
}
/**
* Settings yourls callback
*
* @since 0.1.0
* @param int $args settings callback id.
*/
public function cklph_wdl_settings_yourls( $args ) {
return sprintf(
'<p id="$1$s">%2$s</p>',
esc_attr( $args['id'] ),
esc_html_e( 'Please enter a valid YoURLS Site & Signature.', 'woocommerce-direct-link' )
);
}
/**
* Settings joturl callback
*
* @since 0.1.0
* @param int $args settings callback id.
*/
public function cklph_wdl_settings_joturl( $args ) {
return sprintf(
'<p id="$1$s">%2$s</p>',
esc_attr( $args['id'] ),
esc_html_e( 'Please enter a valid JotURL Public Key & Private Key.', 'woocommerce-direct-link' )
);
}
/**
* Tinyurl access token callback
*
* @return void
*/
public function cklph_wdl_tinyurl_api_key() {
$access_token = get_option( 'tinyurl_access_token_options' );
$api_value = isset( $access_token ) ? esc_attr( $access_token ) : '';
echo sprintf(
'<input type="text" placeholder="TinyURL API Here" id="tinyurl_access_token_options" name="tinyurl_access_token_options" value="%1$s" />',
esc_attr( $api_value )
);
}
/**
* Tinyurl Domain callback.
*
* @return void
*/
public function cklph_wdl_tinyurl_domain() {
$tinyurl_domain = get_option( 'tinyurl_domain' );
$domain_value = isset( $tinyurl_domain ) ? esc_attr( $tinyurl_domain ) : '';
echo sprintf(
'<input type="text" placeholder="e.g tiny.one" id="tinyurl_domain" name="tinyurl_domain" value="%1$s" />',
esc_attr( $domain_value )
);
}
/**
* Bitly access token callback
*
* @since 0.1.0
*/
public function cklph_wdl_bitly_api_key() {
$access_token = get_option( 'bitly_access_token_options' );
$api_value = isset( $access_token ) ? esc_attr( $access_token ) : '';
echo sprintf(
'<input type="text" placeholder="Bitly API Key Here" id="bitly_access_token_options" name="bitly_access_token_options" value="%1$s" />',
esc_attr( $api_value )
);
}
/**
* YoURLS signature callback.
*
* @return void
*/
public function cklph_wdl_yourls_signature() {
$yourls_signature = get_option( 'yourls_signature' );
$signature_value = isset( $yourls_signature ) ? esc_attr( $yourls_signature ) : '';
echo sprintf(
'<input type="text" placeholder="YoURLS API Key Here" id="yourls_signature" name="yourls_signature" value="%1$s" />',
esc_attr( $signature_value )
);
}
/**
* YoURLS domain callback
*
* @return void
*/
public function cklph_wdl_yourls_domain() {
$yourls_domain = get_option( 'yourls_domain' );
$domain_value = isset( $yourls_domain ) ? esc_attr( $yourls_domain ) : '';
echo sprintf(
'<input type="text" placeholder="e.g yourls.org" id="yourls_domain" name="yourls_domain" value="%1$s" />',
esc_attr( $domain_value )
);
}
/**
* JotURL public key callback.
*
* @return void
*/
public function cklph_wdl_joturl_public() {
$joturl_public_key = get_option( 'joturl_public' );
$public_key_value = isset( $joturl_public_key ) ? esc_attr( $joturl_public_key ) : '';
echo sprintf(
'<input type="text" placeholder="JotURL Public API Key" id="joturl_public_key" name="joturl_public_key" value="%1$s" />',
esc_attr( $public_key_value )
);
}
/**
* JotURL private key callback.
*
* @return void
*/
public function cklph_wdl_joturl_private() {
$joturl_private_key = get_option( 'joturl_private' );
$private_key_value = isset( $joturl_private_key ) ? esc_attr( $joturl_private_key ) : '';
echo sprintf(
'<input type="text" placeholder="JotURL Private API Key" id="joturl_private_key" name="joturl_private_key" value="%1$s" />',
esc_attr( $private_key_value )
);
}
/**
* Url shortener option template in settings page.
*
* @since 0.1.0
* @return void html elements in settings page.
*/
public function cklph_wdl_url_shortener() {
$this->cklph_wdl_tinyurl_shortener();
$this->cklph_wdl_bitly_shortener();
$this->cklph_wdl_yourls_shortener();
$this->cklph_wdl_joturl_shortener();
}
/**
* Tinyurl shortener option template in settings page.
*
* @since 0.1.0
* @return void html elements for tinyurl in settings page.
*/
public function cklph_wdl_tinyurl_shortener() {
$url_option = get_option( 'url_options' );
$setup_text = esc_html__( 'Setup', 'woocommerce-direct-links' );
$tinyurl_img = CKLPH_WDL_URL . 'admin/img/tinyurl.svg';
$tinyurl_text = esc_html__( 'TinyURL', 'woocommerce-direct-link' );
$active_tinyurl = ( 'tinyurl' === $url_option ) ? esc_html__( 'Deactivate', 'woocommerce-direct-links' ) : esc_html__( 'Activate', 'woocommerce-direct-links' );
// Echo html tags for tinyurl.
echo sprintf(
'<div class="shortener">
<img class="logo" src="%1$s" alt="tinyurl">
<h3>%2$s</h3>
<div class="box">
<button class="button button-primary" name="setup_btn" type="button" id="tinyurl_setup_btn" onclick="cklph_wdl_shortlink_setup(\'tinyurl\')" >%3$s</button>
</div>
<div class="box">
<button class="button button-primary" name="activate_btn" type="button" id="tinyurl_activate_btn" onclick="cklph_wdl_activate_tinyurl()" ><span id="tinyurl-span" class="cklph-wdl-btn-text">%4$s</span></button>
</div>
</div>',
esc_attr( $tinyurl_img ),
esc_attr( $tinyurl_text ),
esc_attr( $setup_text ),
esc_attr( $active_tinyurl ),
);
}
/**
* Bitly shortener option template in settings page.
*
* @since 0.1.0
* @return void html elements for Bitly in settings page.
*/
public function cklph_wdl_bitly_shortener() {
$setup_text = esc_html__( 'Setup', 'woocommerce-direct-links' );
$url_option = get_option( 'url_options' );
$bitly_img = CKLPH_WDL_URL . 'admin/img/bitly.svg';
$bitly_text = esc_html__( 'Bitly', 'woocommerce-direct-link' );
$active_bitly = ( 'bitly' === $url_option ) ? esc_html__( 'Deactivate', 'woocommerce-direct-links' ) : esc_html__( 'Activate', 'woocommerce-direct-links' );
// Echo html tags for bitly.
echo sprintf(
'<div class="shortener">
<img class="logo" src="%1$s" alt="bitly">
<h3>%2$s</h3>
<div class="box">
<button class="button button-primary" name="setup_btn" type="button" id="bitly_setup_btn" onclick="cklph_wdl_shortlink_setup(\'bitly\')" >%3$s</button>
</div>
<div class="box">
<button class="button button-primary" name="activate_btn" type="button" id="bitly_activate_btn" onclick="cklph_wdl_activate_bitly()" ><span id="bitly-span" class="cklph-wdl-btn-text">%4$s</span></button>
</div>
</div>',
esc_attr( $bitly_img ),
esc_attr( $bitly_text ),
esc_attr( $setup_text ),
esc_attr( $active_bitly ),
);
}
/**
* Yourls shortener option template in settings page.
*
* @since 0.1.0
* @return void html elements for yourls in settings page.
*/
public function cklph_wdl_yourls_shortener() {
$setup_text = esc_html__( 'Setup', 'woocommerce-direct-links' );
$url_option = get_option( 'url_options' );
$yourls_img = CKLPH_WDL_URL . 'admin/img/yourls.svg';
$yourls_text = esc_html__( 'YoURLS', 'woocommerce-direct-link' );
$active_yourls = ( 'yourls' === $url_option ) ? esc_html__( 'Deactivate', 'woocommerce-direct-links' ) : esc_html__( 'Activate', 'woocommerce-direct-links' );
// Echo html tags for yourls.
echo sprintf(
'<div class="shortener">
<img class="logo" src="%1$s" alt="yourls">
<h3>%2$s</h3>
<div class="box">
<button class="button button-primary" name="setup_btn" type="button" id="yourls_setup_btn" onclick="cklph_wdl_shortlink_setup(\'yourls\')" >%3$s</button>
</div>
<div class="box">
<button class="button button-primary" name="activate_btn" type="button" id="yourls_activate_btn" onclick="cklph_wdl_activate_yourls()" ><span id="yourls-span" class="cklph-wdl-btn-text">%4$s</span></button>
</div>
</div>',
esc_attr( $yourls_img ),
esc_attr( $yourls_text ),
esc_attr( $setup_text ),
esc_attr( $active_yourls ),
);
}
/**
* Joturl shortener option template in settings page.
*
* @since 0.1.0
* @return void html elements for joturl in settings page.
*/
public function cklph_wdl_joturl_shortener() {
$setup_text = esc_html__( 'Setup', 'woocommerce-direct-links' );
$url_option = get_option( 'url_options' );
$joturl_img = CKLPH_WDL_URL . 'admin/img/joturl.png';
$joturl_text = esc_html__( 'JotURL', 'woocommerce-direct-link' );
$active_joturl = ( 'joturl' === $url_option ) ? esc_html__( 'Deactivate', 'woocommerce-direct-links' ) : esc_html__( 'Activate', 'woocommerce-direct-links' );
// Echo html tags for joturl.
echo sprintf(
'<div class="shortener">
<img class="logo" src="%1$s" alt="joturl">
<h3>%2$s</h3>
<div class="box">
<button class="button button-primary" name="setup_btn" type="button" id="yourls_setup_btn" onclick="cklph_wdl_shortlink_setup(\'joturl\')" >%3$s</button>
</div>
<div class="box">
<button class="button button-primary" name="activate_btn" type="button" id="joturl_activate_btn" onclick="cklph_wdl_activate_joturl()" ><span id="joturl-span" class="cklph-wdl-btn-text">%4$s</span></button>
</div>
</div>',
esc_attr( $joturl_img ),
esc_attr( $joturl_text ),
esc_attr( $setup_text ),
esc_attr( $active_joturl ),
);
}
}
}

View file

@ -0,0 +1,177 @@
/**
* All of the CSS for your admin-specific functionality should be
* included in this file.
*/
/* tooltip position for copy button */
.tooltip {
position: relative;
display: inline-block;
}
/* tooltip text for copy button */
.tooltip .tooltiptext {
visibility: hidden;
width: 140px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -75px;
opacity: 0;
transition: opacity 0.3s;
}
/* tooltip text for copy button "Link has been copied" */
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* tooltip text for copy button "Copy" */
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
/* for IE & MS Edge */
#id:hover {
cursor: pointer;
}
/* class for span in refresh button */
.cklph-wdl-btn-text {
transition: all 0.2s;
}
/* class for button in refresh button */
.button-primary {
position: relative;
}
/* class for loading when refresh button unused. */
.cklph-wdl-loading .cklph-wdl-btn-text {
visibility: hidden;
opacity: 0;
}
/* class for loading when refresh button is click. */
.cklph-wdl-loading::after {
content: "";
position: absolute;
width: 10px;
height: 10px;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
border: 4px solid transparent;
border-top-color: #fff;
border-radius: 50%;
animation: cklph-wdl-btn-loading 1s ease infinite;
}
.cklph-wdl-input {
width: 90%;
}
/* loading animation */
@keyframes cklph-wdl-btn-loading {
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}
/* image for list table */
.wc-image {
display: block;
position: relative;
text-indent: -9999px;
margin: 0 auto;
height: 1em;
width: 1em;
}
/* image for list table show icon */
table.wp-list-table span.wc-image::before {
font-family: Dashicons;
position: absolute;
font-weight: 400;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
margin: 0;
text-indent: 0;
top: 0;
left: 0;
width: 100%;
height: 100%;
content: "\f128";
}
/* admin list table css */
.wp-list-table .column-thumb { width: 5%; text-align: center; }
.wp-list-table .column-post_title { width: 25%; }
.wp-list-table .column-sku { width: 8%; }
.wp-list-table .column-attrib { width: 8%; }
.wp-list-table .column-category { width: 8%; }
.wp-list-table .column-guid { width: 25%; }
.wp-list-table .column-copy { width: 8%; }
.wp-list-table .column-refresh { width: 8%; }
@media screen and (max-width: 1480px) {
/* admin list table css */
.wp-list-table .column-thumb { width: 2%; text-align: center; }
.wp-list-table .column-post_title { width: 12%; }
.wp-list-table .column-sku { width: 5%; }
.wp-list-table .column-attrib { width: 5%; }
.wp-list-table .column-category { width: 5%; }
.wp-list-table .column-guid { width: 12%; }
.wp-list-table .column-copy { width: 6%; }
.wp-list-table .column-refresh { width: 7%; }
}
@media screen and (max-width: 1024px) {
/* admin list table css */
.wp-list-table .column-post_title { width: 10%; }
.wp-list-table .column-guid { width: 10%; }
.wp-list-table .column-copy { width: 8%; }
.wp-list-table .column-refresh { width: 9%; }
}
@media screen and (max-width: 782px) {
.wp-list-table td.column-primary {
padding-right: 1%;
width: 100%;
}
.wp-list-table .column-thumb {
display: none;
text-align: left;
}
.wp-list-table .column-thumb::before {
visibility: hidden;
}
}

View file

@ -0,0 +1,128 @@
/**
* All of the CSS for metabox in product page functionality should be
* included in this file.
*/
/* tooltip position for copy button */
.tooltip {
position: relative;
display: inline-block;
}
/* tooltip text for copy button */
.tooltip .tooltiptext {
visibility: hidden;
width: 140px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -75px;
opacity: 0;
transition: opacity 0.3s;
}
/* tooltip text for copy button "Link has been copied" */
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* tooltip text for copy button "Copy" */
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
/* for IE & MS Edge */
#id:hover {
cursor: pointer;
}
/* class for span in refresh button */
.cklph-wdl-btn-text {
transition: all 0.2s;
}
/* class for button in refresh button */
.button-primary {
position: relative;
}
/* class for loading when refresh button unused. */
.cklph-wdl-loading .cklph-wdl-btn-text {
visibility: hidden;
opacity: 0;
}
/* class for loading when refresh button is click. */
.cklph-wdl-loading::after {
content: "";
position: absolute;
width: 10px;
height: 10px;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
border: 4px solid transparent;
border-top-color: #fff;
border-radius: 50%;
animation: cklph-wdl-btn-loading 1s ease infinite;
}
/* loading animation */
@keyframes cklph-wdl-btn-loading {
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}
.cklph-wdl-input {
width: 90%;
}
/* metabox list table css */
.wp-list-table .column-thumb { width: 3% !important; }
.wp-list-table .column-post_title { width: 22%; }
.wp-list-table .column-sku { width: 10%; }
.wp-list-table .column-attrib { width: 10%; }
.wp-list-table .column-guid {width: 20%; }
.wp-list-table .column-copy { width: 8%; }
.wp-list-table .column-refresh { width: 13%; }
@media screen and (max-width: 1300px) {
.wp-list-table .column-thumb { display: none; }
.wp-list-table .column-post_title { width: 10%; }
.wp-list-table .column-sku { display: none; }
.wp-list-table .column-attrib { display: none; }
.wp-list-table .column-guid {width: 9%; }
.wp-list-table .column-copy { width: 5%; }
.wp-list-table .column-refresh { width: 6%; }
}
@media screen and (max-width: 782px) {
span.ID {
padding: 8px;
}
.cklph-wdl-input {
width: 90%;
}
}

View file

@ -0,0 +1,113 @@
.tab-content {
position: relative;
}
.tab-content > .tab-pane {
float: left;
width: 100%;
display: none;
}
.tab-content > .tab-pane.active {
display: block;
padding: 10px;
}
.shortener {
width: 20%;
display: inline-block;
border: 1px solid;
border-radius: 25px;
padding: 10px;
margin: 10px;
text-align: center;
align-items: center;
}
.box {
margin: 5px;
display: inline-block;
}
.logo {
margin: 10px;
height: 50px;
width: auto;
}
.cklph-wdl-btn-text {
transition: all 0.2s;
}
.button-primary {
position: relative;
}
.cklph-wdl-loading .cklph-wdl-btn-text {
visibility: hidden;
opacity: 0;
}
.cklph-wdl-loading::after {
content: "";
position: absolute;
width: 10px;
height: 10px;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
border: 4px solid transparent;
border-top-color: #fff;
border-radius: 50%;
animation: cklph-wdl-btn-loading 1s ease infinite;
}
.cklph-wdl-bulk {
position: absolute;
bottom: 80%;
left: 84%;
}
@media screen and (max-width: 1366px) {
.shortener {
width: 40%;
}
.cklph-wdl-bulk {
bottom: 90%;
left: 78%;
}
.logo {
height: 40px;
}
}
@media screen and (max-width: 800px) {
.cklph-wdl-bulk {
left: 68%;
}
}
@media screen and (max-width: 480px) {
.shortener {
width: 90%;
}
.cklph-wdl-bulk {
bottom: 92%;
left: 65%;
}
}
@keyframes cklph-wdl-btn-loading {
from {
transform: rotate(0turn);
}
to {
transform: rotate(1turn);
}
}

1
admin/img/bitly.svg Normal file
View file

@ -0,0 +1 @@
<svg height="2500" viewBox=".82 .51 958.8 958.8" width="2499" xmlns="http://www.w3.org/2000/svg"><path d="m959.62 484.03c0 225.24-150.78 358.42-256.43 418.45-69.42 39.44-117.21 56.83-179.43 56.83-144.54 0-204.91-88.4-204.91-165.37 3.98-279.03-3.28-387.62-3.57-395.1-.38-9.73-.74-24.96-10.18-25.97-5.74-.62-9.44 1.14-16.04 9.38-9.89 13.19-29.85 18.41-42 11.36-14.67-8.52-17.41-27.33-9.98-44.64 20.6-46.15 53.63-66.57 98.15-65.93 76.91 1.1 98.98 51.52 98.98 114.51-1.14 63.62-1.71 112.47-1.71 146.53 27.2-22.25 67.18-46.12 133.95-46.12 92.23 0 171.73 67.96 171.73 193.41 0 73.7-19.24 111.55-19.38 111.91 93.32-81.42 145.02-209.07 145.02-314.79 0-173.03-134.42-377.17-385.79-377.17-206.51 0-389.84 173.73-389.84 380.2 0 114.96 59.77 230.05 146.23 306.07 22.28 19.59 23.27 47.55 11.28 59.16-14.21 13.78-39.02 15.25-59.84-.82-108.14-83.45-185.04-222.35-185.04-366.12 0-277.27 213.05-489.3 477.21-489.3 253.66 0 481.59 202.08 481.59 483.52zm-332.47 221.04c0-77.23-39.3-104.21-98.3-92.41-47.43 9.49-95.49 58.93-100.69 127.65-.86 11.34-1.15 30.13-.25 47.77 2.4 47.03 41.65 64.33 95.46 63.38 89.29-1.56 103.78-94.2 103.78-146.39z" fill="#ee6123"/></svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
admin/img/joturl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

22
admin/img/tinyurl.svg Normal file
View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="180" height="50" version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<metadata>
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
<dc:title/>
</cc:Work>
</rdf:RDF>
</metadata>
<rect width="180" height="50" fill="#17527d"/>
<g transform="matrix(.99605 0 0 1 .0045556 0)" fill="#fff" stroke-width=".9779">
<path d="m14.269 37.814h-5.577q-0.6541 0-0.96393-0.288-0.27541-0.324-0.27541-1.008v-17.676h-5.0607q-0.6541 0-0.96393-0.288-0.27541-0.324-0.27541-1.008v-4.356q0-0.684 0.27541-0.972 0.30984-0.324 0.96393-0.324h18.177q0.6541 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v4.356q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288h-5.0607v17.676q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288z" color="#ffffff"/>
<path d="m27.902 37.814q-0.6541 0-0.96393-0.288-0.27541-0.324-0.27541-1.008v-4.284q0-0.684 0.27541-0.972 0.30984-0.324 0.96393-0.324h3.4082v-12.096h-3.4082q-0.6541 0-0.96393-0.288-0.27541-0.324-0.27541-1.008v-4.356q0-0.684 0.27541-0.972 0.30984-0.324 0.96393-0.324h14.666q0.6541 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v4.356q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288h-3.3393v12.096h3.3393q0.6541 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v4.284q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288z" color="#ffffff"/>
<path d="m56.51 37.814h-5.1639q-0.6541 0-0.96394-0.288-0.27541-0.324-0.27541-1.008v-23.328q0-0.684 0.27541-0.972 0.30984-0.324 0.96394-0.324h3.5803q0.6541 0 1.1016 0.216 0.48197 0.216 0.92951 0.756l6.6787 7.992v-7.668q0-0.684 0.27541-0.972 0.30984-0.324 0.96394-0.324h5.1639q0.6541 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v23.328q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288h-5.1639q-0.6541 0-0.96394-0.288-0.27541-0.324-0.27541-1.008v-4.428l-5.8869-7.452v11.88q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288z" color="#ffffff"/>
<path d="m90.661 37.814h-5.577q-0.6541 0-0.96393-0.288-0.27541-0.324-0.27541-1.008v-4.608l-5.2672-7.38q-0.7918-1.116-1.0672-1.944-0.24098-0.828-0.24098-2.484v-6.912q0-0.684 0.27541-0.972 0.30984-0.324 0.96393-0.324h5.3016q0.6541 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v5.904q0 0.36 0.03442 0.756 0.03443 0.396 0.27541 0.756l1.5836 2.592q0.17213 0.324 0.34426 0.468 0.20656 0.144 0.48197 0.144h0.34426q0.27541 0 0.44754-0.144 0.20656-0.144 0.37869-0.468l1.5836-2.592q0.24098-0.36 0.27541-0.756 0.03442-0.396 0.03442-0.756v-5.904q0-0.684 0.27541-0.972 0.30984-0.324 0.96393-0.324h5.1295q0.65409 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v6.912q0 1.656-0.27542 2.484-0.27541 0.828-1.0328 1.944l-5.2328 7.416v4.572q0 0.684-0.30984 1.008-0.27541 0.288-0.92951 0.288z" color="#ffffff"/>
<path d="m125.53 31.01q0 1.656-0.48197 3.024-0.44754 1.332-1.6525 2.304-1.1705 0.936-3.2361 1.476-2.0656 0.54-5.2672 0.54t-5.2672-0.54q-2.0656-0.54-3.2705-1.476-1.1705-0.972-1.6525-2.304-0.44754-1.368-0.44754-3.024v-17.82q0-0.684 0.27541-0.972 0.30984-0.324 0.96394-0.324h5.4049q0.65411 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v16.416q0 0.72 0.51639 1.224 0.5164 0.468 2.3066 0.468 1.8246 0 2.341-0.468 0.5164-0.504 0.5164-1.224v-16.416q0-0.684 0.2754-0.972 0.30984-0.324 0.96394-0.324h5.2328q0.65409 0 0.9295 0.324 0.30984 0.288 0.30984 0.972z" color="#ffffff"/>
<path d="m139.96 18.374v4.608h2.582q0.89508 0 1.2049-0.468 0.30983-0.504 0.30983-1.224v-1.224q0-0.72-0.30983-1.188-0.30984-0.504-1.2049-0.504zm-1.2393 19.44h-5.3016q-0.6541 0-0.96393-0.288-0.27541-0.324-0.27541-1.008v-23.328q0-0.684 0.27541-0.972 0.30983-0.324 0.96393-0.324h11.464q3.8557 0 5.3016 1.692 1.4803 1.656 1.4803 4.32v1.656q0 1.512-0.44754 2.592-0.41312 1.044-1.4803 1.512 1.859 0.216 2.9951 1.584 1.1705 1.368 1.1705 3.636v7.632q0 0.684-0.30983 1.008-0.27541 0.288-0.92951 0.288h-5.3361q-0.6541 0-0.96394-0.288-0.2754-0.324-0.2754-1.008v-5.508q0-0.792-0.30984-1.152-0.27542-0.396-0.99837-0.396h-4.8197v7.056q0 0.684-0.30983 1.008-0.27541 0.288-0.9295 0.288z" color="#ffffff"/>
<path d="m178.59 37.814h-17.523q-0.6541 0-0.96394-0.288-0.2754-0.324-0.2754-1.008v-23.328q0-0.684 0.2754-0.972 0.30984-0.324 0.96394-0.324h5.3361q0.6541 0 0.92951 0.324 0.30984 0.288 0.30984 0.972v17.82h4.7164v-5.832q0-0.684 0.2754-0.972 0.30984-0.324 0.96393-0.324h4.9918q0.65411 0 0.92952 0.324 0.30983 0.288 0.30983 0.972v11.34q0 0.684-0.30983 1.008-0.27541 0.288-0.92952 0.288z" color="#ffffff"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.4 KiB

1
admin/img/yourls.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 23 KiB

1
admin/index.php Normal file
View file

@ -0,0 +1 @@
<?php // Silence is golden.

186
admin/js/cklph-wdl-admin.js Normal file
View file

@ -0,0 +1,186 @@
/**
* The JavaScript code for the admin
*
*/
// Copy the checkout url of product.
function cklph_wdl_copyurl( id ){
copyText = document.getElementById(`cklph-wdl-url-${id}`);
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
var tooltip = document.getElementById(`myTooltip-${id}`);
tooltip.innerHTML = "Link has been copied";
}
// Copy tooltip.
function cklph_wdl_out( id ) {
var tooltip = document.getElementById(`myTooltip-${id}`);
tooltip.innerHTML = "Copy";
}
// Refresh checkout links.
function cklph_wdl_rest( id ) {
var cklphWdlBtn = document.getElementById(`cklph-wdl-btn-${id}`);
var cklphWdlHidden = document.getElementById('cklph-wdl-hidden');
var countClick = 0;
cklphWdlHidden.value = 'clicked';
cklph_wdl_refresh(id);
cklphWdlBtn.onclick = () => {
if('created' === cklphWdlHidden.value) {
cklph_wdl_refresh(id);
cklphWdlHidden.value = 'exists';
countClick += 2;
} else {
countClick += 1;
if(1 === countClick) {
cklph_wdl_force_joturl_refresh(id);
cklphWdlHidden.value = 'exists';
} else {
cklph_wdl_refresh(id);
cklphWdlHidden.value = 'exists';
}
}
}
}
async function cklph_wdl_refresh(id) {
var cklphWdlHidden = document.getElementById('cklph-wdl-hidden');
var replyDiv = document.getElementById("cklph-wdl-reply");
var cklphWdlBtn = document.getElementById(`cklph-wdl-btn-${id}`);
var cklphWdlInput = document.getElementById(`cklph-wdl-url-${id}`);
var ckplhWdlReply = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
ckplhWdlReply.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
replyDiv.appendChild(ckplhWdlReply);
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
cklphWdlBtn.style.pointerEvents = "none";
// run wp rest api to convert links and return success or error.
try {
try {
const response = await fetch(`/wp-json/cklph-wdl/v1/product/${id}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
});
const urlResponse = await response.json();
cklphWdlInput.value = urlResponse[0];
switch (urlResponse[1]) {
case 200:
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
if('clicked' === cklphWdlHidden.value) {
ckplhWdlReply.innerHTML = `<p><strong>Link already exists. Please press Refresh Link again to force refresh shortlink. ${urlResponse[0]}</strong></p>`;
} else {
ckplhWdlReply.innerHTML = `<p><strong>Link already refreshed. ${urlResponse[0]}</strong></p>`;
}
break;
case 201:
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>Link successfully created. ${urlResponse[0]}</strong></p>`;
cklphWdlHidden.value = 'created';
break;
case '401-bitly':
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
break;
case '401-yourls':
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
break;
case '401-joturl':
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid JotURL Public & Private API Key.</strong></p>';
break;
default:
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>${urlResponse[1]}</strong></p>`;
break;
}
} catch (error) {
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}
} finally {
ckplhWdlReply.appendChild(dismissButton);
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
cklphWdlBtn.style.pointerEvents = "auto";
}
}
async function cklph_wdl_force_joturl_refresh(id) {
var cklphWdlHidden = document.getElementById('cklph-wdl-hidden');
var replyDiv = document.getElementById("cklph-wdl-reply");
var cklphWdlBtn = document.getElementById(`cklph-wdl-btn-${id}`);
var cklphWdlInput = document.getElementById(`cklph-wdl-url-${id}`);
var ckplhWdlReply = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
replyDiv.appendChild(ckplhWdlReply);
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
cklphWdlBtn.style.pointerEvents = "none";
// run wp rest api to convert links and return success or error.
try {
try {
const response = await fetch(`/wp-json/cklph-wdl/v1/product-joturl/${id}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
});
const urlResponse = await response.json();
cklphWdlInput.value = urlResponse[0];
switch (urlResponse[1]) {
case 200:
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>Link successfully refreshed. ${urlResponse[0]}</strong></p>`;
break;
case 201:
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>Link successfully created. ${urlResponse[0]}</strong></p>`;
cklphWdlHidden.value = 'created';
break;
case '401-bitly':
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
break;
case '401-yourls':
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
break;
case '401-joturl':
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid JotURL Public & Private API Key.</strong></p>';
break;
default:
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>${urlResponse[1]}</strong></p>`;
break;
}
} catch (error) {
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
ckplhWdlReply.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}
} finally {
ckplhWdlReply.appendChild(dismissButton);
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
cklphWdlBtn.style.pointerEvents = "auto";
}
}

View file

@ -0,0 +1,566 @@
/**
* The JavaScript code for the settings
*
*/
// show/hide setup for shortlink.
function cklph_wdl_shortlink_setup(shortlink) {
var tabBitly = document.getElementById('tab-bitly');
var tabYourls = document.getElementById('tab-yourls');
var tabjoturl = document.getElementById('tab-joturl');
var tabTinyurl = document.getElementById('tab-tinyurl');
switch (shortlink) {
case 'tinyurl':
tabTinyurl.classList.toggle('active');
tabBitly.classList.remove('active');
tabYourls.classList.remove('active');
tabjoturl.classList.remove('active');
break;
case 'bitly':
tabBitly.classList.toggle('active');
tabYourls.classList.remove('active');
tabTinyurl.classList.remove('active');
tabjoturl.classList.remove('active');
break;
case 'yourls':
tabYourls.classList.toggle('active');
tabBitly.classList.remove('active');
tabjoturl.classList.remove('active');
tabTinyurl.classList.remove('active');
break;
case 'joturl':
tabjoturl.classList.toggle('active');
tabYourls.classList.remove('active');
tabTinyurl.classList.remove('active');
tabBitly.classList.remove('active');
break;
default:
break;
}
}
// Bulk refresh checkout links.
function cklph_wdl_bulk_rest() {
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var cklphWdlBtn = document.getElementById('cklph-wdl-bulk-btn');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
cklphWdlBtn.style.pointerEvents = "none";
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Refreshing shortlinks please wait!</strong></p>';
fetch('/wp-json/cklph-wdl/v1/product/bulk', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(urlResponse => {
switch(urlResponse) {
case 200:
replyDiv.className = "notice notice-success settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Shortlinks Refreshed.</strong></p>';
break;
case '401-bitly':
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
break;
case '401-yourls':
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
break;
default:
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>No URL Shortener is activated. Please set and activate a shortener to refresh all links.</strong></p>';
break;
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
cklphWdlBtn.style.pointerEvents = "auto";
});
}
/**
* This Section is for tinyurl setup and activate.
*
* Activate tinyurl in settings page.
*/
function cklph_wdl_activate_tinyurl() {
var loadBitly = document.getElementById('bitly-span');
var loadYourls = document.getElementById('yourls-span');
var loadJoturl = document.getElementById('joturl-span');
var loadTinyurl = document.getElementById('tinyurl-span');
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var onclickTinyurl = document.getElementById('tinyurl_activate_btn');
var tinyurlApiKey = document.getElementById('tinyurl_access_token_options');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
onclickTinyurl.classList.toggle("cklph-wdl-loading");
onclickTinyurl.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/shortlink/tinyurl/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(shortlinkResponse => {
replyDiv.className = "notice notice-success settings-error is-dismissible";
if ('tinyurl' === shortlinkResponse) {
loadBitly.innerHTML = 'Activate';
loadYourls.innerHTML = 'Activate';
loadJoturl.innerHTML = 'Activate';
loadTinyurl.innerHTML = 'Deactivate';
if (tinyurlApiKey.value) {
replyDiv.innerHTML = '<p><strong>TinyURL successfully activated using API Key.</strong></p>';
return;
}
replyDiv.innerHTML = '<p><strong>TinyURL successfully activated without using API Key.</strong></p>';
} else {
loadTinyurl.innerHTML = 'Activate';
replyDiv.innerHTML = '<p><strong>TinyURL successfully deactivated.</strong></p>';
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
onclickTinyurl.classList.toggle("cklph-wdl-loading");
onclickTinyurl.style.pointerEvents = "auto";
});
}
// Save settings for tinyurl setup.
function cklph_wdl_setup_tinyurl() {
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var tinyurlSaveBtn = document.getElementById('tinyurl-save-btn');
var tinyurlDomain = document.getElementById('tinyurl_domain');
var tinyurlApiKey = document.getElementById('tinyurl_access_token_options');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
tinyurlSaveBtn.classList.toggle("cklph-wdl-loading");
tinyurlSaveBtn.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/tinyurl/?domain=${tinyurlDomain.value}&apikey=${tinyurlApiKey.value}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(tinyurlResponse => {
if (200 === tinyurlResponse) {
if (tinyurlApiKey.value) {
replyDiv.className = "notice notice-success settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
return;
}
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Unable to save TinyURL settings because Access token is empty.</strong></p>`;
} else if ('401-tinyurl' === tinyurlResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid TinyURL API Key.</strong></p>';
tinyurlDomain.value = '';
} else if ('422-tinyurl' === tinyurlResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Unable to save TinyURL Domain. An Error has occured. Error code: 422 : Domain not found</strong></p>';
tinyurlDomain.value = '';
} else {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Unable to save TinyURL settings. ${tinyurlResponse}</strong></p>`;
tinyurlDomain.value = '';
tinyurlApiKey.value = '';
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
tinyurlSaveBtn.classList.toggle("cklph-wdl-loading");
tinyurlSaveBtn.style.pointerEvents = "auto";
});
}
/**
* This Section is for bitly setup and activate.
*
* Activate bitly in settings page.
*/
function cklph_wdl_activate_bitly() {
var loadBitly = document.getElementById('bitly-span');
var loadYourls = document.getElementById('yourls-span');
var loadJoturl = document.getElementById('joturl-span');
var loadTinyurl = document.getElementById('tinyurl-span');
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var onclickBitly = document.getElementById('bitly_activate_btn');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
onclickBitly.classList.toggle("cklph-wdl-loading");
onclickBitly.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/shortlink/bitly/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(shortlinkResponse => {
replyDiv.className = "notice notice-success settings-error is-dismissible";
if ('bitly' === shortlinkResponse) {
loadTinyurl.innerHTML = 'Activate';
loadYourls.innerHTML = 'Activate';
loadJoturl.innerHTML = 'Activate';
loadBitly.innerHTML = 'Deactivate';
replyDiv.innerHTML = '<p><strong>Bitly successfully activated</strong></p>';
} else if ('401-bitly' === shortlinkResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid Bitly API Key to activate Bitly shortener.</strong></p>';
} else {
loadBitly.innerHTML = 'Activate';
replyDiv.innerHTML = '<p><strong>Bitly successfully deactivated.</strong></p>';
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
onclickBitly.classList.toggle("cklph-wdl-loading");
onclickBitly.style.pointerEvents = "auto";
});
}
// Save settings for bitly setup.
function cklph_wdl_setup_bitly() {
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var bitlySaveBtn = document.getElementById('bitly-save-btn');
var bitlyApiKey = document.getElementById('bitly_access_token_options');
var loadBitly = document.getElementById('bitly-span');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
bitlySaveBtn.classList.toggle("cklph-wdl-loading");
bitlySaveBtn.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/bitly/?apikey=${bitlyApiKey.value}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(bitlyResponse => {
if (200 === bitlyResponse) {
replyDiv.className = "notice notice-success settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
} else if ('401-bitly' === bitlyResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
loadBitly.innerHTML = 'Activate';
} else {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Unable to save Bitly settings. ${bitlyResponse}</strong></p>`;
loadBitly.innerHTML = 'Activate';
bitlyApiKey.value = '';
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
bitlySaveBtn.classList.toggle("cklph-wdl-loading");
bitlySaveBtn.style.pointerEvents = "auto";
});
}
/**
* This Section is for yourls setup and activate.
*
* Activate yourls in settings page.
*/
function cklph_wdl_activate_yourls() {
var loadBitly = document.getElementById('bitly-span');
var loadYourls = document.getElementById('yourls-span');
var loadJoturl = document.getElementById('joturl-span');
var loadTinyurl = document.getElementById('tinyurl-span');
var onclickYourls = document.getElementById('yourls_activate_btn');
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
onclickYourls.classList.toggle("cklph-wdl-loading");
onclickYourls.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/shortlink/yourls/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(shortlinkResponse => {
replyDiv.className = "notice notice-success settings-error is-dismissible";
if ('yourls' === shortlinkResponse) {
loadBitly.innerHTML = 'Activate';
loadJoturl.innerHTML = 'Activate';
loadTinyurl.innerHTML = 'Activate';
loadYourls.innerHTML = 'Deactivate';
replyDiv.innerHTML = '<p><strong>YoURLS successfully activated</strong></p>';
} else if ('401-yourls' === shortlinkResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature to activate YoURLS shortener.</strong></p>';
} else {
loadYourls.innerHTML = 'Activate';
replyDiv.innerHTML = '<p><strong>YoURLS successfully deactivated.</strong></p>';
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
onclickYourls.classList.toggle("cklph-wdl-loading");
onclickYourls.style.pointerEvents = "auto";
});
}
// Save settings for yourls setup.
function cklph_wdl_setup_yourls() {
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var yourlsSaveBtn = document.getElementById('yourls-save-btn');
var yourlsDomain = document.getElementById('yourls_domain');
var yourlsApiKey = document.getElementById('yourls_signature');
var loadYourls = document.getElementById('yourls-span');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
yourlsSaveBtn.classList.toggle("cklph-wdl-loading");
yourlsSaveBtn.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/yourls/?domain=${yourlsDomain.value}&apikey=${yourlsApiKey.value}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(yourlsResponse => {
if ('401-yourls' === yourlsResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
} else if (200 !== yourlsResponse) {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Unable to save YoURLS settings. ${yourlsResponse}</strong></p>`;
yourlsDomain.value = '';
yourlsApiKey.value = '';
loadYourls.innerHTML = 'Activate';
} else {
replyDiv.className = "notice notice-success settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
yourlsSaveBtn.classList.toggle("cklph-wdl-loading");
yourlsSaveBtn.style.pointerEvents = "auto";
});
}
/**
* This Section is for JotURL setup and activate.
*
* Activate JotURL in settings page.
*/
function cklph_wdl_activate_joturl() {
var loadBitly = document.getElementById('bitly-span');
var loadYourls = document.getElementById('yourls-span');
var loadJoturl = document.getElementById('joturl-span');
var loadTinyurl = document.getElementById('tinyurl-span');
var onclickJoturl = document.getElementById('joturl_activate_btn');
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
onclickJoturl.classList.toggle("cklph-wdl-loading");
onclickJoturl.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/shortlink/joturl/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(shortlinkResponse => {
replyDiv.className = "notice notice-success settings-error is-dismissible";
if ('joturl' === shortlinkResponse) {
loadBitly.innerHTML = 'Activate';
loadYourls.innerHTML = 'Activate';
loadTinyurl.innerHTML = 'Activate';
loadJoturl.innerHTML = 'Deactivate';
replyDiv.innerHTML = '<p><strong>JotURL successfully activated</strong></p>';
} else if ('401-joturl' === shortlinkResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid Public & Private API Key to activate JotURL shortener.</strong></p>';
} else {
loadJoturl.innerHTML = 'Activate';
replyDiv.innerHTML = '<p><strong>JotURL successfully deactivated.</strong></p>';
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
onclickJoturl.classList.toggle("cklph-wdl-loading");
onclickJoturl.style.pointerEvents = "auto";
});
}
function cklph_wdl_setup_joturl() {
var loadJoturl = document.getElementById('joturl-span');
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
var joturlSaveBtn = document.getElementById('joturl-save-btn');
var joturlPublic = document.getElementById('joturl_public_key');
var joturlPrivate = document.getElementById('joturl_private_key');
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
var dismissButton = document.createElement('button');
replyDiv.setAttribute('id', 'cklph-message');
dismissButton.setAttribute('type', 'button');
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
dismissButton.className = 'notice-dismiss';
ckplhWdlReply.appendChild(replyDiv);
joturlSaveBtn.classList.toggle("cklph-wdl-loading");
joturlSaveBtn.style.pointerEvents = "none";
fetch(`/wp-json/cklph-wdl/v1/joturl/?public=${joturlPublic.value}&private=${joturlPrivate.value}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cklph_wdl.nonce,
}
}).then(response => {
return response.json();
}).then(joturlResponse => {
if ('401-joturl' === joturlResponse) {
replyDiv.className = "notice notice-warning settings-error is-dismissible";
replyDiv.innerHTML = '<p><strong>Please enter a valid JotURL Public & Private API Key.</strong></p>';
} else if (200 !== joturlResponse) {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Unable to save JotURL settings. ${joturlResponse}</strong></p>`;
joturlPublic.value = '';
joturlPrivate.value = '';
loadJoturl.innerHTML = 'Activate';
} else {
replyDiv.className = "notice notice-success settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
}
}).catch(error => {
replyDiv.className = "notice notice-error settings-error is-dismissible";
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
}).finally(() => {
replyDiv.appendChild(dismissButton);
joturlSaveBtn.classList.toggle("cklph-wdl-loading");
joturlSaveBtn.style.pointerEvents = "auto";
});
}
jQuery( function( $ ) {
var short_link = $(".shortener");
if ( short_link.length ) {
short_link.each(function() {
var get_active_box = $(this).find('.cklph-wdl-btn-text').html();
if (get_active_box == 'Deactivate') {
// add a class to active shortlink method
$(this).addClass('active-shortener');
// add a css to active shortlink method
$(this).css('background-color','#d3d3d3');
$(this).find("[name='activate_btn']").css('background-color','#218838');
$(this).find("[name='activate_btn']").css('border-color','#1e7e34');
}
});
}
});

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