, * Lord Francis Navarro * @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 '

' . esc_html__( 'Cannot activate WooCommerce Direct Links without WooCommerce installed and activated.', 'woocommerce-direct-links' ) . '

'; } /** * 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( 'Settings' ); 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( '

%2$s

', 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( '

%2$s

', 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( '

%2$s

', 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( '

%2$s

', 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( '

%2$s

', 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( '', 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( '', 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( '', 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( '', 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( '', 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( '', 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( '', 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( '

%2$s

', 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( '

%2$s

', 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( '

%2$s

', 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( '

%2$s

', esc_attr( $joturl_img ), esc_attr( $joturl_text ), esc_attr( $setup_text ), esc_attr( $active_joturl ), ); } } }