From 2fde6edfdc5b2a2711092c4d00c96b55735d0a72 Mon Sep 17 00:00:00 2001 From: janine Date: Fri, 1 Mar 2024 19:04:22 +0530 Subject: [PATCH] Plugin code --- LICENSE.txt | 339 ++++ README.txt | 24 + admin/class-cklph-wdl-admin.php | 884 ++++++++++ admin/css/cklph-wdl-admin.css | 177 ++ admin/css/cklph-wdl-metabox.css | 128 ++ admin/css/cklph-wdl-settings.css | 113 ++ admin/img/bitly.svg | 1 + admin/img/joturl.png | Bin 0 -> 1691 bytes admin/img/tinyurl.svg | 22 + admin/img/yourls.svg | 1 + admin/index.php | 1 + admin/js/cklph-wdl-admin.js | 186 ++ admin/js/cklph-wdl-settings.js | 566 ++++++ admin/partials/cklph-wdl-admin-page.php | 33 + admin/partials/cklph-wdl-settings-page.php | 105 ++ admin/partials/class-cklph-wdl-rest-api.php | 409 +++++ admin/partials/class-cklph-wdl-shortlinks.php | 616 +++++++ .../partials/class-cklph-wdl-wc-products.php | 179 ++ .../class-cklph-wdl-list-table.php | 462 +++++ .../list-tables/class-wp-list-table.php | 1522 +++++++++++++++++ includes/class-cklph-wdl-i18n.php | 37 + includes/class-cklph-wdl-loader.php | 122 ++ includes/class-cklph-wdl.php | 178 ++ includes/index.php | 1 + index.php | 1 + languages/woocommerce-direct-links.pot | 107 ++ uninstall.php | 55 + woocommerce-direct-links.php | 69 + 28 files changed, 6338 insertions(+) create mode 100644 LICENSE.txt create mode 100644 README.txt create mode 100644 admin/class-cklph-wdl-admin.php create mode 100644 admin/css/cklph-wdl-admin.css create mode 100644 admin/css/cklph-wdl-metabox.css create mode 100644 admin/css/cklph-wdl-settings.css create mode 100644 admin/img/bitly.svg create mode 100644 admin/img/joturl.png create mode 100644 admin/img/tinyurl.svg create mode 100644 admin/img/yourls.svg create mode 100644 admin/index.php create mode 100644 admin/js/cklph-wdl-admin.js create mode 100644 admin/js/cklph-wdl-settings.js create mode 100644 admin/partials/cklph-wdl-admin-page.php create mode 100644 admin/partials/cklph-wdl-settings-page.php create mode 100644 admin/partials/class-cklph-wdl-rest-api.php create mode 100644 admin/partials/class-cklph-wdl-shortlinks.php create mode 100644 admin/partials/class-cklph-wdl-wc-products.php create mode 100644 admin/partials/list-tables/class-cklph-wdl-list-table.php create mode 100644 admin/partials/list-tables/class-wp-list-table.php create mode 100644 includes/class-cklph-wdl-i18n.php create mode 100644 includes/class-cklph-wdl-loader.php create mode 100644 includes/class-cklph-wdl.php create mode 100644 includes/index.php create mode 100644 index.php create mode 100644 languages/woocommerce-direct-links.pot create mode 100644 uninstall.php create mode 100644 woocommerce-direct-links.php diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..ecbc059 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. \ No newline at end of file diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..ad6f67e --- /dev/null +++ b/README.txt @@ -0,0 +1,24 @@ +=== Chykalophia Woocommerce Direct Links === +Contributors: Joel Sanguenza (joel@chykalophia.com), Lord Francis Navarro (francis@chykalophia.com) +Tags: Woocommerce, Chykalophia, Woocommerce Direct Links +Requires at least: +Tested up to: +Stable tag: +License: GPLv2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html + +A plugin that displays the list of Woocommerce products their add-to-cart links which directly proceeds to checkout. + +== Description == +Woocommerce Direct Links is an extension plugin for the Woocommerce plugin which displays the list of Woocommerce products (simple and variable products) along with its add-to-cart links. These add-to-cart links automatically adds the product or product variation to the cart and redirects the user to the checkout page. You may choose from two (2) url shorteners (Tinyurl and Bitly) to shrink the links. + +== Installation == + +1. Upload `cklph-woo-directlinks` to the `/wp-content/plugins/` directory. Or upload it through the admin dashboard. +2. Activate the plugin through the 'Plugins' menu in WordPress. Make sure that Woocommerce is already activated before activation this plugin. + +== Frequently Asked Questions == + +== Screenshots == + +== Changelog == \ No newline at end of file diff --git a/admin/class-cklph-wdl-admin.php b/admin/class-cklph-wdl-admin.php new file mode 100644 index 0000000..fc4ceb7 --- /dev/null +++ b/admin/class-cklph-wdl-admin.php @@ -0,0 +1,884 @@ +, + * 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 ), + ); + } + } +} diff --git a/admin/css/cklph-wdl-admin.css b/admin/css/cklph-wdl-admin.css new file mode 100644 index 0000000..fea44ff --- /dev/null +++ b/admin/css/cklph-wdl-admin.css @@ -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; + } +} \ No newline at end of file diff --git a/admin/css/cklph-wdl-metabox.css b/admin/css/cklph-wdl-metabox.css new file mode 100644 index 0000000..70bc53a --- /dev/null +++ b/admin/css/cklph-wdl-metabox.css @@ -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%; + } +} diff --git a/admin/css/cklph-wdl-settings.css b/admin/css/cklph-wdl-settings.css new file mode 100644 index 0000000..8b344f9 --- /dev/null +++ b/admin/css/cklph-wdl-settings.css @@ -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); + } +} \ No newline at end of file diff --git a/admin/img/bitly.svg b/admin/img/bitly.svg new file mode 100644 index 0000000..edf042b --- /dev/null +++ b/admin/img/bitly.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/admin/img/joturl.png b/admin/img/joturl.png new file mode 100644 index 0000000000000000000000000000000000000000..2234008cb420163fb45216db6f18b01aa9a93b1f GIT binary patch literal 1691 zcmV;M24wk(P)Qotolb zTKeGM=zx68#Po3h000eiQchC<8<+9WIZ?pCf7P*pv&BT0000I4Nklvp6d z5QH-l9VB5v;QgO<=bq6~xigSdn%7oj7fuu^u6JBfi{v+O{y!iq<^wMi z880Br&vKbgQUV`={cN=Gq5Ngk$O{SUe;L&CV!{rl#`^We^aWSD+0=>j*{th?`)u2F zqIs{gVK3X3gOpO@Liw@c{)0O*ONOfLiKB_^2vMdyc`&eK1=s}JRv^k=;P@9su;M)S@M(^z0@@AG};zYSA6Elfo7O| z9{)Y*bNfej9cL$hKL6-z|97zG{+<7x{sWD{%_{VEU$PrhSA1GMSde!v%p&uz8g z7Gvj)5bCVQpRX7fFMbE$i^Au)QoBM(%1Me%IqqEQQ{(&mUXd%l)su7JqvNw8@C~8( ztkTjXw4Sc`j%z2o;#*Y?eB*Zg^26u2)+a@ftlQ2;;ETdXv*NQB!4CLJ;F|*RiBz2f z@B!cN!^hr>j{#p4KF1r24Dhv#FEsjW(`y2JQTTXXeAeb2_$tDuJ##1U)r3zA34Ar; zldgj@A+^39>C=G*zACJX_84_u_Iv6JMeEcP)}=__m#1y0t#K%}VB|FFO@~i~(KEo8 z86VBpH)w|?KMH&W@X0)@eZW@+pH7%BD_$^a58s&Bx%fTZrLyJP_V5j9os0IPSJ`HX z_V77vRa&k$7^Tc4*D~tliLVE~D0~MGN4_~h%YV$+XdnL_YU;ETl9Ipdq|M=$s^_kJo!u3J?88ot39pSgR` zm!fFyaVc{dPvDEVJ@fI@va1Ju!S^VGQ!eKMd|Mu!`QTekp89b(yNiQ|g? zx!xRYJvk9RLRXX#A6xmj!O44?r+KuRq%g$rc}keKXN2ek z#s&EN@P)n!K+fu&Ih95C%C|Ex13piDf$!l6U1PU&4Z!D#FEIS3u>DE4X*KZq;tLGF zbH0=zvA5f@1D`Lxs$RbceBStKdig2vdE=|;)x*H&kFR9_6c-TkhY{ zt(TpjC7pi~Ov)NAfGB%!P_p$utna-=JwxcV9e@N?ujvXHX+b-L-kURMgoU9!u3xhN z0k2_zfLBuu9KBp98&UIGB?xxa60~JeLcy+T4ISHP1_|rXgN^_I002ovPDHLkV1hMmW(5EM literal 0 HcmV?d00001 diff --git a/admin/img/tinyurl.svg b/admin/img/tinyurl.svg new file mode 100644 index 0000000..84cee89 --- /dev/null +++ b/admin/img/tinyurl.svg @@ -0,0 +1,22 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/admin/img/yourls.svg b/admin/img/yourls.svg new file mode 100644 index 0000000..537412a --- /dev/null +++ b/admin/img/yourls.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..bacffcc --- /dev/null +++ b/admin/index.php @@ -0,0 +1 @@ + { + 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 = `

Link already exists. Please press Refresh Link again to force refresh shortlink. ${urlResponse[0]}

`; + } else { + ckplhWdlReply.innerHTML = `

Link already refreshed. ${urlResponse[0]}

`; + } + break; + case 201: + ckplhWdlReply.className = "notice notice-success settings-error is-dismissible"; + ckplhWdlReply.innerHTML = `

Link successfully created. ${urlResponse[0]}

`; + cklphWdlHidden.value = 'created'; + break; + case '401-bitly': + ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible"; + ckplhWdlReply.innerHTML = '

Please enter a valid Bitly API Key.

'; + break; + case '401-yourls': + ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible"; + ckplhWdlReply.innerHTML = '

Please enter a valid YoURLS Site & Signature.

'; + break; + case '401-joturl': + ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible"; + ckplhWdlReply.innerHTML = '

Please enter a valid JotURL Public & Private API Key.

'; + break; + default: + ckplhWdlReply.className = "notice notice-error settings-error is-dismissible"; + ckplhWdlReply.innerHTML = `

${urlResponse[1]}

`; + break; + } + } catch (error) { + ckplhWdlReply.className = "notice notice-error settings-error is-dismissible"; + ckplhWdlReply.innerHTML = `

An unknown error occured. ${error}

`; + } + } 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 = `

Link successfully refreshed. ${urlResponse[0]}

`; + break; + case 201: + ckplhWdlReply.className = "notice notice-success settings-error is-dismissible"; + ckplhWdlReply.innerHTML = `

Link successfully created. ${urlResponse[0]}

`; + cklphWdlHidden.value = 'created'; + break; + case '401-bitly': + ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible"; + ckplhWdlReply.innerHTML = '

Please enter a valid Bitly API Key.

'; + break; + case '401-yourls': + ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible"; + ckplhWdlReply.innerHTML = '

Please enter a valid YoURLS Site & Signature.

'; + break; + case '401-joturl': + ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible"; + ckplhWdlReply.innerHTML = '

Please enter a valid JotURL Public & Private API Key.

'; + break; + default: + ckplhWdlReply.className = "notice notice-error settings-error is-dismissible"; + ckplhWdlReply.innerHTML = `

${urlResponse[1]}

`; + break; + } + } catch (error) { + ckplhWdlReply.className = "notice notice-error settings-error is-dismissible"; + ckplhWdlReply.innerHTML = `

An unknown error occured. ${error}

`; + } + } finally { + ckplhWdlReply.appendChild(dismissButton); + cklphWdlBtn.classList.toggle("cklph-wdl-loading"); + cklphWdlBtn.style.pointerEvents = "auto"; + } +} \ No newline at end of file diff --git a/admin/js/cklph-wdl-settings.js b/admin/js/cklph-wdl-settings.js new file mode 100644 index 0000000..bf90f3b --- /dev/null +++ b/admin/js/cklph-wdl-settings.js @@ -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 = '

Refreshing shortlinks please wait!

'; + + 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 = '

Shortlinks Refreshed.

'; + break; + case '401-bitly': + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid Bitly API Key.

'; + break; + case '401-yourls': + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid YoURLS Site & Signature.

'; + break; + default: + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

No URL Shortener is activated. Please set and activate a shortener to refresh all links.

'; + break; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = '

TinyURL successfully activated using API Key.

'; + return; + } + replyDiv.innerHTML = '

TinyURL successfully activated without using API Key.

'; + } else { + loadTinyurl.innerHTML = 'Activate'; + replyDiv.innerHTML = '

TinyURL successfully deactivated.

'; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = `

Settings Saved.

`; + return; + } + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

Unable to save TinyURL settings because Access token is empty.

`; + } else if ('401-tinyurl' === tinyurlResponse) { + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid TinyURL API Key.

'; + tinyurlDomain.value = ''; + } else if ('422-tinyurl' === tinyurlResponse) { + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Unable to save TinyURL Domain. An Error has occured. Error code: 422 : Domain not found

'; + tinyurlDomain.value = ''; + } else { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

Unable to save TinyURL settings. ${tinyurlResponse}

`; + tinyurlDomain.value = ''; + tinyurlApiKey.value = ''; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = '

Bitly successfully activated

'; + } else if ('401-bitly' === shortlinkResponse) { + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid Bitly API Key to activate Bitly shortener.

'; + } else { + loadBitly.innerHTML = 'Activate'; + replyDiv.innerHTML = '

Bitly successfully deactivated.

'; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = `

Settings Saved.

`; + } else if ('401-bitly' === bitlyResponse) { + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid Bitly API Key.

'; + loadBitly.innerHTML = 'Activate'; + } else { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

Unable to save Bitly settings. ${bitlyResponse}

`; + loadBitly.innerHTML = 'Activate'; + bitlyApiKey.value = ''; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = '

YoURLS successfully activated

'; + } else if ('401-yourls' === shortlinkResponse) { + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid YoURLS Site & Signature to activate YoURLS shortener.

'; + } else { + loadYourls.innerHTML = 'Activate'; + replyDiv.innerHTML = '

YoURLS successfully deactivated.

'; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = '

Please enter a valid YoURLS Site & Signature.

'; + } else if (200 !== yourlsResponse) { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

Unable to save YoURLS settings. ${yourlsResponse}

`; + yourlsDomain.value = ''; + yourlsApiKey.value = ''; + loadYourls.innerHTML = 'Activate'; + } else { + replyDiv.className = "notice notice-success settings-error is-dismissible"; + replyDiv.innerHTML = `

Settings Saved.

`; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = '

JotURL successfully activated

'; + } else if ('401-joturl' === shortlinkResponse) { + replyDiv.className = "notice notice-warning settings-error is-dismissible"; + replyDiv.innerHTML = '

Please enter a valid Public & Private API Key to activate JotURL shortener.

'; + } else { + loadJoturl.innerHTML = 'Activate'; + replyDiv.innerHTML = '

JotURL successfully deactivated.

'; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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 = '

Please enter a valid JotURL Public & Private API Key.

'; + } else if (200 !== joturlResponse) { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

Unable to save JotURL settings. ${joturlResponse}

`; + joturlPublic.value = ''; + joturlPrivate.value = ''; + loadJoturl.innerHTML = 'Activate'; + } else { + replyDiv.className = "notice notice-success settings-error is-dismissible"; + replyDiv.innerHTML = `

Settings Saved.

`; + } + }).catch(error => { + replyDiv.className = "notice notice-error settings-error is-dismissible"; + replyDiv.innerHTML = `

An unknown error occured. ${error}

`; + }).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'); + } + }); + } +}); \ No newline at end of file diff --git a/admin/partials/cklph-wdl-admin-page.php b/admin/partials/cklph-wdl-admin-page.php new file mode 100644 index 0000000..5bccdeb --- /dev/null +++ b/admin/partials/cklph-wdl-admin-page.php @@ -0,0 +1,33 @@ + + + +
+

+ +

+ +
+ +prepare_items(); + $wdl_table->search_box( __( 'Search Products', 'woocommerce-direct-links' ), 'search' ); + $wdl_table->display(); diff --git a/admin/partials/cklph-wdl-settings-page.php b/admin/partials/cklph-wdl-settings-page.php new file mode 100644 index 0000000..3c30eb1 --- /dev/null +++ b/admin/partials/cklph-wdl-settings-page.php @@ -0,0 +1,105 @@ + +
+

+ +

+
+
+ + + + + +
+ + +
+
+ + +
+
+ + + +
+
+ + + +
+
+
diff --git a/admin/partials/class-cklph-wdl-rest-api.php b/admin/partials/class-cklph-wdl-rest-api.php new file mode 100644 index 0000000..9d454dd --- /dev/null +++ b/admin/partials/class-cklph-wdl-rest-api.php @@ -0,0 +1,409 @@ +\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\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[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'; + } + } + } +} diff --git a/admin/partials/class-cklph-wdl-shortlinks.php b/admin/partials/class-cklph-wdl-shortlinks.php new file mode 100644 index 0000000..f66508e --- /dev/null +++ b/admin/partials/class-cklph-wdl-shortlinks.php @@ -0,0 +1,616 @@ + '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; + } + } +} diff --git a/admin/partials/class-cklph-wdl-wc-products.php b/admin/partials/class-cklph-wdl-wc-products.php new file mode 100644 index 0000000..99542b8 --- /dev/null +++ b/admin/partials/class-cklph-wdl-wc-products.php @@ -0,0 +1,179 @@ +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 ) : '', + '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 ) : '', + 'attrib' => '', + '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' => '', + '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, + ); + } + } +} diff --git a/admin/partials/list-tables/class-cklph-wdl-list-table.php b/admin/partials/list-tables/class-cklph-wdl-list-table.php new file mode 100644 index 0000000..a20a5e2 --- /dev/null +++ b/admin/partials/list-tables/class-cklph-wdl-list-table.php @@ -0,0 +1,462 @@ + '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' => '' . __( 'Image', 'woocommerce-direct-links' ) . '', + '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( + '%s: %s', + __( '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( + '%s: %s', + __( 'ID', 'woocommerce-direct-links' ), + esc_attr( $item_id ) + ), + 'edit' => sprintf( + '%s', + esc_attr( get_edit_post_link( $item_id ) ), + __( 'Edit', 'woocommerce-direct-links' ) + ), + 'view' => sprintf( + '%s', + 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( + '%2$s%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( + '
+ +
', + 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( + '', + 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( + '', + 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 ) { + ?> +
+ screen->id && is_admin() ) { + if ( 'top' === $which ) { + _e( '
' ); + + $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( '
' ); + } + } + } + ?> +
+ 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( '
' ); + parent::search_box( $text, $input_id ); + _e( '
' ); + } + } + } +} \ No newline at end of file diff --git a/admin/partials/list-tables/class-wp-list-table.php b/admin/partials/list-tables/class-wp-list-table.php new file mode 100644 index 0000000..619e475 --- /dev/null +++ b/admin/partials/list-tables/class-wp-list-table.php @@ -0,0 +1,1522 @@ +get_column_info(). + * + * @since 4.1.0 + * @var array + */ + protected $_column_headers; + + /** + * {@internal Missing Summary} + * + * @var array + */ + protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' ); + + /** + * {@internal Missing Summary} + * + * @var array + */ + protected $compat_methods = array( + 'set_pagination_args', + 'get_views', + 'get_bulk_actions', + 'bulk_actions', + 'row_actions', + 'months_dropdown', + 'view_switcher', + 'comments_bubble', + 'get_items_per_page', + 'pagination', + 'get_sortable_columns', + 'get_column_info', + 'get_table_classes', + 'display_tablenav', + 'extra_tablenav', + 'single_row_columns', + ); + + /** + * Constructor. + * + * The child class should call this constructor from its own constructor to override + * the default $args. + * + * @since 3.1.0 + * + * @param array|string $args { + * Array or string of arguments. + * + * @type string $plural Plural value used for labels and the objects being listed. + * This affects things such as CSS class-names and nonces used + * in the list table, e.g. 'posts'. Default empty. + * @type string $singular Singular label for an object being listed, e.g. 'post'. + * Default empty + * @type bool $ajax Whether the list table supports Ajax. This includes loading + * and sorting data, for example. If true, the class will call + * the _js_vars() method in the footer to provide variables + * to any scripts handling Ajax events. Default false. + * @type string $screen String containing the hook name used to determine the current + * screen. If left null, the current screen will be automatically set. + * Default null. + * } + */ + public function __construct( $args = array() ) { + $args = wp_parse_args( + $args, + array( + 'plural' => '', + 'singular' => '', + 'ajax' => false, + 'screen' => null, + ) + ); + + $this->screen = convert_to_screen( $args['screen'] ); + + add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 ); + + if ( ! $args['plural'] ) { + $args['plural'] = $this->screen->base; + } + + $args['plural'] = sanitize_key( $args['plural'] ); + $args['singular'] = sanitize_key( $args['singular'] ); + + $this->_args = $args; + + if ( $args['ajax'] ) { + // wp_enqueue_script( 'list-table' ); + add_action( 'admin_footer', array( $this, '_js_vars' ) ); + } + + if ( empty( $this->modes ) ) { + $this->modes = array( + 'list' => __( 'Compact view' ), + 'excerpt' => __( 'Extended view' ), + ); + } + } + + /** + * Make private properties readable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Property to get. + * @return mixed Property. + */ + public function __get( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return $this->$name; + } + } + + /** + * Make private properties settable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Property to check if set. + * @param mixed $value Property value. + * @return mixed Newly-set property. + */ + public function __set( $name, $value ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return $this->$name = $value; + } + } + + /** + * Make private properties checkable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Property to check if set. + * @return bool Whether the property is a back-compat property and it is set. + */ + public function __isset( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + return isset( $this->$name ); + } + + return false; + } + + /** + * Make private properties un-settable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Property to unset. + */ + public function __unset( $name ) { + if ( in_array( $name, $this->compat_fields, true ) ) { + unset( $this->$name ); + } + } + + /** + * Make private/protected methods readable for backward compatibility. + * + * @since 4.0.0 + * + * @param string $name Method to call. + * @param array $arguments Arguments to pass when calling. + * @return mixed|bool Return value of the callback, false otherwise. + */ + public function __call( $name, $arguments ) { + if ( in_array( $name, $this->compat_methods, true ) ) { + return $this->$name( ...$arguments ); + } + return false; + } + + /** + * Checks the current user's permissions + * + * @since 3.1.0 + * @abstract + */ + public function ajax_user_can() { + die( 'function WP_List_Table::ajax_user_can() must be overridden in a subclass.' ); + } + + /** + * Prepares the list of items for displaying. + * + * @uses WP_List_Table::set_pagination_args() + * + * @since 3.1.0 + * @abstract + */ + public function prepare_items() { + die( 'function WP_List_Table::prepare_items() must be overridden in a subclass.' ); + } + + /** + * An internal method that sets all the necessary pagination arguments + * + * @since 3.1.0 + * + * @param array|string $args Array or string of arguments with information about the pagination. + */ + protected function set_pagination_args( $args ) { + $args = wp_parse_args( + $args, + array( + 'total_items' => 0, + 'total_pages' => 0, + 'per_page' => 0, + ) + ); + + if ( ! $args['total_pages'] && $args['per_page'] > 0 ) { + $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] ); + } + + // Redirect if page number is invalid and headers are not already sent. + if ( ! headers_sent() && ! wp_doing_ajax() && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) { + wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) ); + exit; + } + + $this->_pagination_args = $args; + } + + /** + * Access the pagination args. + * + * @since 3.1.0 + * + * @param string $key Pagination argument to retrieve. Common values include 'total_items', + * 'total_pages', 'per_page', or 'infinite_scroll'. + * @return int Number of items that correspond to the given pagination argument. + */ + public function get_pagination_arg( $key ) { + if ( 'page' === $key ) { + return $this->get_pagenum(); + } + + if ( isset( $this->_pagination_args[ $key ] ) ) { + return $this->_pagination_args[ $key ]; + } + + return 0; + } + + /** + * Whether the table has items to display or not + * + * @since 3.1.0 + * + * @return bool + */ + public function has_items() { + return ! empty( $this->items ); + } + + /** + * Message to be displayed when there are no items + * + * @since 3.1.0 + */ + public function no_items() { + _e( 'No items found.' ); + } + + /** + * Displays the search box. + * + * @since 3.1.0 + * + * @param string $text The 'submit' button label. + * @param string $input_id ID attribute value for the search input field. + */ + public function search_box( $text, $input_id ) { + if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { + return; + } + + $input_id = $input_id . '-search-input'; + + if ( ! empty( $_REQUEST['orderby'] ) ) { + echo ''; + } + if ( ! empty( $_REQUEST['order'] ) ) { + echo ''; + } + if ( ! empty( $_REQUEST['post_mime_type'] ) ) { + echo ''; + } + if ( ! empty( $_REQUEST['detached'] ) ) { + echo ''; + } + ?> + + 'link'` + * + * @since 3.1.0 + * + * @return array + */ + protected function get_views() { + return array(); + } + + /** + * Displays the list of views available on this table. + * + * @since 3.1.0 + */ + public function views() { + $views = $this->get_views(); + /** + * Filters the list of available list table views. + * + * The dynamic portion of the hook name, `$this->screen->id`, refers + * to the ID of the current screen. + * + * @since 3.1.0 + * + * @param string[] $views An array of available list table views. + */ + $views = apply_filters( "views_{$this->screen->id}", $views ); + + if ( empty( $views ) ) { + return; + } + + $this->screen->render_screen_reader_content( 'heading_views' ); + + echo "
    \n"; + foreach ( $views as $class => $view ) { + $views[ $class ] = "\t
  • $view"; + } + echo implode( " |
  • \n", $views ) . "\n"; + echo '
'; + } + + /** + * Retrieves the list of bulk actions available for this table. + * + * The format is an associative array where each element represents either a top level option value and label, or + * an array representing an optgroup and its options. + * + * For a standard option, the array element key is the field value and the array element value is the field label. + * + * For an optgroup, the array element key is the label and the array element value is an associative array of + * options as above. + * + * Example: + * + * [ + * 'edit' => 'Edit', + * 'delete' => 'Delete', + * 'Change State' => [ + * 'feature' => 'Featured', + * 'sale' => 'On Sale', + * ] + * ] + * + * @since 3.1.0 + * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup. + * + * @return array + */ + protected function get_bulk_actions() { + return array(); + } + + /** + * Displays the bulk actions dropdown. + * + * @since 3.1.0 + * + * @param string $which The location of the bulk actions: 'top' or 'bottom'. + * This is designated as optional for backward compatibility. + */ + protected function bulk_actions( $which = '' ) { + if ( is_null( $this->_actions ) ) { + $this->_actions = $this->get_bulk_actions(); + + /** + * Filters the items in the bulk actions menu of the list table. + * + * The dynamic portion of the hook name, `$this->screen->id`, refers + * to the ID of the current screen. + * + * @since 3.1.0 + * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup. + * + * @param array $actions An array of the available bulk actions. + */ + $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + + $two = ''; + } else { + $two = '2'; + } + + if ( empty( $this->_actions ) ) { + return; + } + + echo ''; + echo '\n"; + + submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) ); + echo "\n"; + } + + /** + * Gets the current action selected from the bulk actions dropdown. + * + * @since 3.1.0 + * + * @return string|false The action name. False if no action was selected. + */ + public function current_action() { + if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) { + return false; + } + + if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) { + return $_REQUEST['action']; + } + + return false; + } + + /** + * Generates the required HTML for a list of row action links. + * + * @since 3.1.0 + * + * @param string[] $actions An array of action links. + * @param bool $always_visible Whether the actions should be always visible. + * @return string The HTML for the row actions. + */ + protected function row_actions( $actions, $always_visible = false ) { + $action_count = count( $actions ); + + if ( ! $action_count ) { + return ''; + } + + $mode = get_user_setting( 'posts_list_mode', 'list' ); + + if ( 'excerpt' === $mode ) { + $always_visible = true; + } + + $out = '
'; + + $i = 0; + + foreach ( $actions as $action => $link ) { + ++$i; + + $sep = ( $i < $action_count ) ? ' | ' : ''; + + $out .= "$link$sep"; + } + + $out .= '
'; + + $out .= ''; + + return $out; + } + + /** + * Displays a dropdown for filtering items in the list table by month. + * + * @since 3.1.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * @global WP_Locale $wp_locale WordPress date and time locale object. + * + * @param string $post_type The post type. + */ + protected function months_dropdown( $post_type ) { + global $wpdb, $wp_locale; + + /** + * Filters whether to remove the 'Months' drop-down from the post list table. + * + * @since 4.2.0 + * + * @param bool $disable Whether to disable the drop-down. Default false. + * @param string $post_type The post type. + */ + if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) { + return; + } + + /** + * Filters to short-circuit performing the months dropdown query. + * + * @since 5.7.0 + * + * @param object[]|false $months 'Months' drop-down results. Default false. + * @param string $post_type The post type. + */ + $months = apply_filters( 'pre_months_dropdown_query', false, $post_type ); + + if ( ! is_array( $months ) ) { + $extra_checks = "AND post_status != 'auto-draft'"; + if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) { + $extra_checks .= " AND post_status != 'trash'"; + } elseif ( isset( $_GET['post_status'] ) ) { + $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] ); + } + + $months = $wpdb->get_results( + $wpdb->prepare( + " + SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month + FROM $wpdb->posts + WHERE post_type = %s + $extra_checks + ORDER BY post_date DESC + ", + $post_type + ) + ); + } + + /** + * Filters the 'Months' drop-down results. + * + * @since 3.7.0 + * + * @param object[] $months Array of the months drop-down query results. + * @param string $post_type The post type. + */ + $months = apply_filters( 'months_dropdown_results', $months, $post_type ); + + $month_count = count( $months ); + + if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) { + return; + } + + $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0; + ?> + + + + +
+ modes as $mode => $title ) { + $classes = array( 'view-' . $mode ); + $aria_current = ''; + + if ( $current_mode === $mode ) { + $classes[] = 'current'; + $aria_current = ' aria-current="page"'; + } + + printf( + "%s\n", + esc_url( remove_query_arg( 'attachment-filter', add_query_arg( 'mode', $mode ) ) ), + implode( ' ', $classes ), + $title + ); + } + ?> +
+ %s', + __( 'No comments' ) + ); + } elseif ( $approved_comments && 'trash' === get_post_status( $post_id ) ) { + // Don't link the comment bubble for a trashed post. + printf( + '%s', + $approved_comments_number, + $pending_comments ? $approved_phrase : $approved_only_phrase + ); + } elseif ( $approved_comments ) { + // Link the comment bubble to approved comments. + printf( + '%s', + esc_url( + add_query_arg( + array( + 'p' => $post_id, + 'comment_status' => 'approved', + ), + admin_url( 'edit-comments.php' ) + ) + ), + $approved_comments_number, + $pending_comments ? $approved_phrase : $approved_only_phrase + ); + } else { + // Don't link the comment bubble when there are no approved comments. + printf( + '%s', + $approved_comments_number, + $pending_comments ? __( 'No approved comments' ) : __( 'No comments' ) + ); + } + + if ( $pending_comments ) { + printf( + '%s', + esc_url( + add_query_arg( + array( + 'p' => $post_id, + 'comment_status' => 'moderated', + ), + admin_url( 'edit-comments.php' ) + ) + ), + $pending_comments_number, + $pending_phrase + ); + } else { + printf( + '%s', + $pending_comments_number, + $approved_comments ? __( 'No pending comments' ) : __( 'No comments' ) + ); + } + } + + /** + * Gets the current page number. + * + * @since 3.1.0 + * + * @return int + */ + public function get_pagenum() { + $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0; + + if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) { + $pagenum = $this->_pagination_args['total_pages']; + } + + return max( 1, $pagenum ); + } + + /** + * Gets the number of items to display on a single page. + * + * @since 3.1.0 + * + * @param string $option + * @param int $default + * @return int + */ + protected function get_items_per_page( $option, $default = 20 ) { + $per_page = (int) get_user_option( $option ); + if ( empty( $per_page ) || $per_page < 1 ) { + $per_page = $default; + } + + /** + * Filters the number of items to be displayed on each page of the list table. + * + * The dynamic hook name, `$option`, refers to the `per_page` option depending + * on the type of list table in use. Possible filter names include: + * + * - `edit_comments_per_page` + * - `sites_network_per_page` + * - `site_themes_network_per_page` + * - `themes_network_per_page'` + * - `users_network_per_page` + * - `edit_post_per_page` + * - `edit_page_per_page'` + * - `edit_{$post_type}_per_page` + * - `edit_post_tag_per_page` + * - `edit_category_per_page` + * - `edit_{$taxonomy}_per_page` + * - `site_users_network_per_page` + * - `users_per_page` + * + * @since 2.9.0 + * + * @param int $per_page Number of items to be displayed. Default 20. + */ + return (int) apply_filters( "{$option}", $per_page ); + } + + /** + * Displays the pagination. + * + * @since 3.1.0 + * + * @param string $which + */ + protected function pagination( $which ) { + if ( empty( $this->_pagination_args ) ) { + return; + } + + $total_items = $this->_pagination_args['total_items']; + $total_pages = $this->_pagination_args['total_pages']; + $infinite_scroll = false; + if ( isset( $this->_pagination_args['infinite_scroll'] ) ) { + $infinite_scroll = $this->_pagination_args['infinite_scroll']; + } + + if ( 'top' === $which && $total_pages > 1 ) { + $this->screen->render_screen_reader_content( 'heading_pagination' ); + } + + $output = '' . sprintf( + /* translators: %s: Number of items. */ + _n( '%s item', '%s items', $total_items ), + number_format_i18n( $total_items ) + ) . ''; + + $current = $this->get_pagenum(); + $removable_query_args = wp_removable_query_args(); + + $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); + + $current_url = remove_query_arg( $removable_query_args, $current_url ); + + $page_links = array(); + + $total_pages_before = ''; + $total_pages_after = ''; + + $disable_first = false; + $disable_last = false; + $disable_prev = false; + $disable_next = false; + + if ( 1 == $current ) { + $disable_first = true; + $disable_prev = true; + } + if ( $total_pages == $current ) { + $disable_last = true; + $disable_next = true; + } + + if ( $disable_first ) { + $page_links[] = ''; + } else { + $page_links[] = sprintf( + "%s", + esc_url( remove_query_arg( 'paged', $current_url ) ), + __( 'First page' ), + '«' + ); + } + + if ( $disable_prev ) { + $page_links[] = ''; + } else { + $page_links[] = sprintf( + "%s", + esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ) ), + __( 'Previous page' ), + '‹' + ); + } + + if ( 'bottom' === $which ) { + $html_current_page = $current; + $total_pages_before = '' . __( 'Current Page' ) . ''; + } else { + $html_current_page = sprintf( + "%s", + '', + $current, + strlen( $total_pages ) + ); + } + $html_total_pages = sprintf( "%s", number_format_i18n( $total_pages ) ); + $page_links[] = $total_pages_before . sprintf( + /* translators: 1: Current page, 2: Total pages. */ + _x( '%1$s of %2$s', 'paging' ), + $html_current_page, + $html_total_pages + ) . $total_pages_after; + + if ( $disable_next ) { + $page_links[] = ''; + } else { + $page_links[] = sprintf( + "%s", + esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ) ), + __( 'Next page' ), + '›' + ); + } + + if ( $disable_last ) { + $page_links[] = ''; + } else { + $page_links[] = sprintf( + "%s", + esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ), + __( 'Last page' ), + '»' + ); + } + + $pagination_links_class = 'pagination-links'; + if ( ! empty( $infinite_scroll ) ) { + $pagination_links_class .= ' hide-if-js'; + } + $output .= "\n" . implode( "\n", $page_links ) . ''; + + if ( $total_pages ) { + $page_class = $total_pages < 2 ? ' one-page' : ''; + } else { + $page_class = ' no-pages'; + } + $this->_pagination = "
$output
"; + + echo $this->_pagination; + } + + /** + * Gets a list of columns. + * + * The format is: + * - `'internal-name' => 'Title'` + * + * @since 3.1.0 + * @abstract + * + * @return array + */ + public function get_columns() { + die( 'function WP_List_Table::get_columns() must be overridden in a subclass.' ); + } + + /** + * Gets a list of sortable columns. + * + * The format is: + * - `'internal-name' => 'orderby'` + * - `'internal-name' => array( 'orderby', 'asc' )` - The second element sets the initial sorting order. + * - `'internal-name' => array( 'orderby', true )` - The second element makes the initial order descending. + * + * @since 3.1.0 + * + * @return array + */ + protected function get_sortable_columns() { + return array(); + } + + /** + * Gets the name of the default primary column. + * + * @since 4.3.0 + * + * @return string Name of the default primary column, in this case, an empty string. + */ + protected function get_default_primary_column_name() { + $columns = $this->get_columns(); + $column = ''; + + if ( empty( $columns ) ) { + return $column; + } + + // We need a primary defined so responsive views show something, + // so let's fall back to the first non-checkbox column. + foreach ( $columns as $col => $column_name ) { + if ( 'cb' === $col ) { + continue; + } + + $column = $col; + break; + } + + return $column; + } + + /** + * Public wrapper for WP_List_Table::get_default_primary_column_name(). + * + * @since 4.4.0 + * + * @return string Name of the default primary column. + */ + public function get_primary_column() { + return $this->get_primary_column_name(); + } + + /** + * Gets the name of the primary column. + * + * @since 4.3.0 + * + * @return string The name of the primary column. + */ + protected function get_primary_column_name() { + $columns = get_column_headers( $this->screen ); + $default = $this->get_default_primary_column_name(); + + // If the primary column doesn't exist, + // fall back to the first non-checkbox column. + if ( ! isset( $columns[ $default ] ) ) { + $default = self::get_default_primary_column_name(); + } + + /** + * Filters the name of the primary column for the current list table. + * + * @since 4.3.0 + * + * @param string $default Column name default for the specific list table, e.g. 'name'. + * @param string $context Screen ID for specific list table, e.g. 'plugins'. + */ + $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id ); + + if ( empty( $column ) || ! isset( $columns[ $column ] ) ) { + $column = $default; + } + + return $column; + } + + /** + * Gets a list of all, hidden, and sortable columns, with filter applied. + * + * @since 3.1.0 + * + * @return array + */ + protected function get_column_info() { + // $_column_headers is already set / cached. + if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) { + /* + * Backward compatibility for `$_column_headers` format prior to WordPress 4.3. + * + * In WordPress 4.3 the primary column name was added as a fourth item in the + * column headers property. This ensures the primary column name is included + * in plugins setting the property directly in the three item format. + */ + $column_headers = array( array(), array(), array(), $this->get_primary_column_name() ); + foreach ( $this->_column_headers as $key => $value ) { + $column_headers[ $key ] = $value; + } + + return $column_headers; + } + + $columns = get_column_headers( $this->screen ); + $hidden = get_hidden_columns( $this->screen ); + + $sortable_columns = $this->get_sortable_columns(); + /** + * Filters the list table sortable columns for a specific screen. + * + * The dynamic portion of the hook name, `$this->screen->id`, refers + * to the ID of the current screen. + * + * @since 3.1.0 + * + * @param array $sortable_columns An array of sortable columns. + */ + $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns ); + + $sortable = array(); + foreach ( $_sortable as $id => $data ) { + if ( empty( $data ) ) { + continue; + } + + $data = (array) $data; + if ( ! isset( $data[1] ) ) { + $data[1] = false; + } + + $sortable[ $id ] = $data; + } + + $primary = $this->get_primary_column_name(); + $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); + + return $this->_column_headers; + } + + /** + * Returns the number of visible columns. + * + * @since 3.1.0 + * + * @return int + */ + public function get_column_count() { + list ( $columns, $hidden ) = $this->get_column_info(); + $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) ); + return count( $columns ) - count( $hidden ); + } + + /** + * Prints column headers, accounting for hidden and sortable columns. + * + * @since 3.1.0 + * + * @param bool $with_id Whether to set the ID attribute or not + */ + public function print_column_headers( $with_id = true ) { + list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); + + $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); + $current_url = remove_query_arg( 'paged', $current_url ); + + if ( isset( $_GET['orderby'] ) ) { + $current_orderby = $_GET['orderby']; + } else { + $current_orderby = ''; + } + + if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) { + $current_order = 'desc'; + } else { + $current_order = 'asc'; + } + + if ( ! empty( $columns['cb'] ) ) { + static $cb_counter = 1; + $columns['cb'] = '' + . ''; + $cb_counter++; + } + + foreach ( $columns as $column_key => $column_display_name ) { + $class = array( 'manage-column', "column-$column_key" ); + + if ( in_array( $column_key, $hidden, true ) ) { + $class[] = 'hidden'; + } + + if ( 'cb' === $column_key ) { + $class[] = 'check-column'; + } elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ), true ) ) { + $class[] = 'num'; + } + + if ( $column_key === $primary ) { + $class[] = 'column-primary'; + } + + if ( isset( $sortable[ $column_key ] ) ) { + list( $orderby, $desc_first ) = $sortable[ $column_key ]; + + if ( $current_orderby === $orderby ) { + $order = 'asc' === $current_order ? 'desc' : 'asc'; + + $class[] = 'sorted'; + $class[] = $current_order; + } else { + $order = strtolower( $desc_first ); + + if ( ! in_array( $order, array( 'desc', 'asc' ), true ) ) { + $order = $desc_first ? 'desc' : 'asc'; + } + + $class[] = 'sortable'; + $class[] = 'desc' === $order ? 'asc' : 'desc'; + } + + $column_display_name = sprintf( + '%s', + esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ), + $column_display_name + ); + } + + $tag = ( 'cb' === $column_key ) ? 'td' : 'th'; + $scope = ( 'th' === $tag ) ? 'scope="col"' : ''; + $id = $with_id ? "id='$column_key'" : ''; + + if ( ! empty( $class ) ) { + $class = "class='" . implode( ' ', $class ) . "'"; + } + + echo "<$tag $scope $id $class>$column_display_name"; + } + } + + /** + * Displays the table. + * + * @since 3.1.0 + */ + public function display() { + $singular = $this->_args['singular']; + + $this->display_tablenav( 'top' ); + + $this->screen->render_screen_reader_content( 'heading_list' ); + ?> + + + + print_column_headers(); ?> + + + + + > + display_rows_or_placeholder(); ?> + + + + + print_column_headers( false ); ?> + + + +
+ display_tablenav( 'bottom' ); + } + + /** + * Gets a list of CSS classes for the WP_List_Table table tag. + * + * @since 3.1.0 + * + * @return string[] Array of CSS classes for the table tag. + */ + protected function get_table_classes() { + $mode = get_user_setting( 'posts_list_mode', 'list' ); + + $mode_class = esc_attr( 'table-view-' . $mode ); + + return array( 'widefat', 'fixed', 'striped', $mode_class, $this->_args['plural'] ); + } + + /** + * Generates the table navigation above or below the table + * + * @since 3.1.0 + * @param string $which + */ + protected function display_tablenav( $which ) { + if ( 'top' === $which ) { + // wp_nonce_field( 'bulk-' . $this->_args['plural'] ); + } + ?> +
+ + has_items() ) : ?> +
+ bulk_actions( $which ); ?> +
+ extra_tablenav( $which ); + $this->pagination( $which ); + ?> + +
+
+ has_items() ) { + $this->display_rows(); + } else { + echo ''; + $this->no_items(); + echo ''; + } + } + + /** + * Generates the table rows. + * + * @since 3.1.0 + */ + public function display_rows() { + foreach ( $this->items as $item ) { + $this->single_row( $item ); + } + } + + /** + * Generates content for a single row of the table. + * + * @since 3.1.0 + * + * @param object|array $item The current item + */ + public function single_row( $item ) { + echo ''; + $this->single_row_columns( $item ); + echo ''; + } + + /** + * @param object|array $item + * @param string $column_name + */ + protected function column_default( $item, $column_name ) {} + + /** + * @param object|array $item + */ + protected function column_cb( $item ) {} + + /** + * Generates the columns for a single row of the table. + * + * @since 3.1.0 + * + * @param object|array $item The current item. + */ + protected function single_row_columns( $item ) { + list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); + + foreach ( $columns as $column_name => $column_display_name ) { + $classes = "$column_name column-$column_name"; + if ( $primary === $column_name ) { + $classes .= ' has-row-actions column-primary'; + } + + if ( in_array( $column_name, $hidden, true ) ) { + $classes .= ' hidden'; + } + + // Comments column uses HTML in the display name with screen reader text. + // Strip tags to get closer to a user-friendly string. + $data = 'data-colname="' . esc_attr( wp_strip_all_tags( $column_display_name ) ) . '"'; + + $attributes = "class='$classes' $data"; + + if ( 'cb' === $column_name ) { + echo ''; + echo $this->column_cb( $item ); + echo ''; + } elseif ( method_exists( $this, '_column_' . $column_name ) ) { + echo call_user_func( + array( $this, '_column_' . $column_name ), + $item, + $classes, + $data, + $primary + ); + } elseif ( method_exists( $this, 'column_' . $column_name ) ) { + echo ""; + echo call_user_func( array( $this, 'column_' . $column_name ), $item ); + echo $this->handle_row_actions( $item, $column_name, $primary ); + echo ''; + } else { + echo ""; + echo $this->column_default( $item, $column_name ); + echo $this->handle_row_actions( $item, $column_name, $primary ); + echo ''; + } + } + } + + /** + * Generates and display row actions links for the list table. + * + * @since 4.3.0 + * + * @param object|array $item The item being acted upon. + * @param string $column_name Current column name. + * @param string $primary Primary column name. + * @return string The row actions HTML, or an empty string + * if the current column is not the primary column. + */ + protected function handle_row_actions( $item, $column_name, $primary ) { + return $column_name === $primary ? '' : ''; + } + + /** + * Handles an incoming ajax request (called from admin-ajax.php) + * + * @since 3.1.0 + */ + public function ajax_response() { + $this->prepare_items(); + + ob_start(); + if ( ! empty( $_REQUEST['no_placeholder'] ) ) { + $this->display_rows(); + } else { + $this->display_rows_or_placeholder(); + } + + $rows = ob_get_clean(); + + $response = array( 'rows' => $rows ); + + if ( isset( $this->_pagination_args['total_items'] ) ) { + $response['total_items_i18n'] = sprintf( + /* translators: Number of items. */ + _n( '%s item', '%s items', $this->_pagination_args['total_items'] ), + number_format_i18n( $this->_pagination_args['total_items'] ) + ); + } + if ( isset( $this->_pagination_args['total_pages'] ) ) { + $response['total_pages'] = $this->_pagination_args['total_pages']; + $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] ); + } + + die( wp_json_encode( $response ) ); + } + + /** + * Sends required variables to JavaScript land. + * + * @since 3.1.0 + */ + public function _js_vars() { + $args = array( + 'class' => get_class( $this ), + 'screen' => array( + 'id' => $this->screen->id, + 'base' => $this->screen->base, + ), + ); + + printf( "\n", wp_json_encode( $args ) ); + } +} diff --git a/includes/class-cklph-wdl-i18n.php b/includes/class-cklph-wdl-i18n.php new file mode 100644 index 0000000..108b8fe --- /dev/null +++ b/includes/class-cklph-wdl-i18n.php @@ -0,0 +1,37 @@ +, + * Lord Francis Navarro + */ + +if ( ! class_exists( 'Cklph_Wdl_i18n' ) ) { + + /** + * Internationalization class + */ + class Cklph_Wdl_I18n { + + /** + * Load wdl text domain for translation. + * + * @since 0.1.0 + */ + public function load_plugin_textdomain() { + + load_plugin_textdomain( + 'plugin-name', + false, + dirname( dirname( CKLPH_WDL_BASE ) ) . '/languages/' + ); + } + } +} diff --git a/includes/class-cklph-wdl-loader.php b/includes/class-cklph-wdl-loader.php new file mode 100644 index 0000000..7346b31 --- /dev/null +++ b/includes/class-cklph-wdl-loader.php @@ -0,0 +1,122 @@ +, + * Lord Francis Navarro + */ + +if ( ! class_exists( 'Cklph_Wdl_Loader' ) ) { + + /** + * Loader of all actions and filters for WooCommerce direct links. + */ + class Cklph_Wdl_Loader { + + /** + * The array of actions registered with WordPress. + * + * @since 0.1.0 + * @access protected + * @var array $actions The actions registered with WordPress to fire when wdl loads. + */ + protected $actions; + + /** + * The array of filters registered with WordPress. + * + * @since 0.1.0 + * @access protected + * @var array $filters The filters registered with WordPress to fire when wdl loads. + */ + protected $filters; + + /** + * Initialize the collections used to maintain the actions and filters. + * + * @since 0.1.0 + */ + public function __construct() { + + $this->actions = array(); + $this->filters = array(); + + } + + /** + * Add a new action to the collection to be registered with WordPress. + * + * @since 0.1.0 + * @param string $hook The name of the WordPress action that is being registered. + * @param object $component A reference to the instance of the object on which the action is defined. + * @param string $callback The name of the function definition on the $component. + * @param int $priority Optional. The priority at which the function should be fired. Default is 10. + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. + */ + public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { + $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); + } + + /** + * Add a new filter to the collection to be registered with WordPress. + * + * @since 0.1.0 + * @param string $hook The name of the WordPress filter that is being registered. + * @param object $component A reference to the instance of the object on which the filter is defined. + * @param string $callback The name of the function definition on the $component. + * @param int $priority Optional. The priority at which the function should be fired. Default is 10. + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. + */ + public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { + $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); + } + + /** + * A utility function that is used to register the actions and hooks into a single + * collection. + * + * @since 0.1.0 + * @access private + * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). + * @param string $hook The name of the WordPress filter that is being registered. + * @param object $component A reference to the instance of the object on which the filter is defined. + * @param string $callback The name of the function definition on the $component. + * @param int $priority The priority at which the function should be fired. + * @param int $accepted_args The number of arguments that should be passed to the $callback. + * @return array The collection of actions and filters registered with WordPress. + */ + private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { + + $hooks[] = array( + 'hook' => $hook, + 'component' => $component, + 'callback' => $callback, + 'priority' => $priority, + 'accepted_args' => $accepted_args, + ); + + return $hooks; + + } + + /** + * Register the filters and actions with WordPress. + * + * @since 0.1.0 + */ + public function run() { + + foreach ( $this->filters as $hook ) { + add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); + } + + foreach ( $this->actions as $hook ) { + add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); + } + } + } +} diff --git a/includes/class-cklph-wdl.php b/includes/class-cklph-wdl.php new file mode 100644 index 0000000..414cc5d --- /dev/null +++ b/includes/class-cklph-wdl.php @@ -0,0 +1,178 @@ +, + * Lord Francis Navarro + */ + +// If this file is called directly, abort. +if (!defined('ABSPATH')) { + die; +} + +if (!class_exists('Cklph_Wdl')) { + + /** + * Loads the dependencies, internationalization, and the loader class. + */ + class Cklph_Wdl + { + + /** + * The loader that's responsible for maintaining and registering all the hooks + * + * @since 0.1.0 + * @access protected + * @var Wdl_Loader $wdl_loader Maintains and registers all hooks for wdl. + */ + protected $wdl_loader; + + /** + * The unique identifier of wdl. + * + * @since 0.1.0 + * @access protected + * @var string $wdl_name The string used to uniquely identify wdl. + */ + protected $wdl_name; + + /** + * The current version + * + * @since 0.1.0 + * @access protected + * @var string $wdl_version The current version of the wdl. + */ + protected $wdl_version; + + /** + * Define the core functionality of wdl. + * + * @since 0.1.0 + */ + public function __construct() + { + if (defined('WDL_VERSION')) { + $this->wdl_version = WDL_VERSION; + } else { + $this->wdl_version = '0.1.0'; + } + $this->wdl_name = 'woocommerce-direct-links'; + + $this->load_dependencies(); + $this->set_locale(); + $this->define_admin_hooks(); + } + + /** + * Load the required dependencies for wdl + * Also Creates an instance of the loader which will be used to register the hooks. + * + * @since 0.1.0 + * @access private + */ + private function load_dependencies() + { + + require_once CKLPH_WDL_PATH . 'admin/partials/list-tables/class-cklph-wdl-list-table.php'; + require_once CKLPH_WDL_PATH . 'admin/partials/class-cklph-wdl-wc-products.php'; + require_once CKLPH_WDL_PATH . 'admin/partials/class-cklph-wdl-shortlinks.php'; + require_once CKLPH_WDL_PATH . 'admin/partials/class-cklph-wdl-rest-api.php'; + require_once CKLPH_WDL_PATH . 'includes/class-cklph-wdl-loader.php'; + require_once CKLPH_WDL_PATH . 'includes/class-cklph-wdl-i18n.php'; + require_once CKLPH_WDL_PATH . 'admin/class-cklph-wdl-admin.php'; + + $this->wdl_loader = new Cklph_Wdl_Loader(); + } + + /** + * Define the locale for wdl for internationalization. + * + * @since 0.1.0 + * @access private + */ + private function set_locale() + { + + $wdl_i18n = new Cklph_Wdl_I18n(); + + $this->wdl_loader->add_action('plugins_loaded', $wdl_i18n, 'load_plugin_textdomain'); + + } + + /** + * Register all of the hooks related to the admin area functionality + * of wdl. + * + * @since 0.1.0 + * @access private + */ + private function define_admin_hooks() + { + $wdl_admin = new Cklph_Wdl_Admin($this->get_plugin_name(), $this->get_version()); + + $this->wdl_loader->add_action('rest_api_init', $wdl_admin, 'cklph_wdl_rest_api'); + $this->wdl_loader->add_action('admin_init', $wdl_admin, 'cklph_wdl_has_woocommerce'); + $this->wdl_loader->add_action('admin_init', $wdl_admin, 'cklph_wdl_settings_init'); + $this->wdl_loader->add_action('admin_menu', $wdl_admin, 'cklph_wdl_add_admin_pages'); + $this->wdl_loader->add_action('admin_enqueue_scripts', $wdl_admin, 'cklph_wdl_enqueue_styles'); + $this->wdl_loader->add_action('admin_enqueue_scripts', $wdl_admin, 'cklph_wdl_enqueue_scripts'); + $this->wdl_loader->add_action('add_meta_boxes', $wdl_admin, 'cklph_wdl_add_meta_box'); + + $this->wdl_loader->add_filter('plugin_action_links_' . CKLPH_WDL_BASE, $wdl_admin, 'cklph_wdl_add_action_links'); + $this->wdl_loader->add_filter('set-screen-option', $wdl_admin, 'cklph_wdl_set_pagination_option', 10, 3); + } + + /** + * Run the wdl loader to execute all of the hooks with WordPress. + * + * @since 0.1.0 + */ + public function run() + { + $this->wdl_loader->run(); + } + + /** + * The name of the plugin used to uniquely identify it within the context of + * WordPress and to define internationalization functionality. + * + * @since 0.1.0 + * @return string The name of the plugin. + */ + public function get_plugin_name() + { + return $this->wdl_name; + } + + /** + * The reference to the class that orchestrates the hooks with wdl. + * + * @since 0.1.0 + * @return WDL_Loader Orchestrates the hooks wdl. + */ + public function get_loader() + { + return $this->wdl_loader; + } + + /** + * Retrieve the version number of wdl. + * + * @since 0.1.0 + * @return string The version number of wdl. + */ + public function get_version() + { + return $this->wdl_version; + } + } +} \ No newline at end of file diff --git a/includes/index.php b/includes/index.php new file mode 100644 index 0000000..49d255d --- /dev/null +++ b/includes/index.php @@ -0,0 +1 @@ +\n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"POT-Creation-Date: 2022-05-16T17:09:25+02:00\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"X-Generator: WP-CLI 2.6.0\n" +"X-Domain: woocommerce-direct-links\n" + +#. Plugin Name of the plugin +msgid "WooCommerce Direct Links" +msgstr "" + +#. Plugin URI of the plugin +msgid "https://chykalophia.com/woocommerce-direct-links" +msgstr "" + +#. Description of the plugin +msgid "An extension plugin for Woocommerce which displays the list of products (simple and variable products) and its variations along with its add-to-cart links. These links automatically adds the product or product variation to the cart and redirects the user to the checkout page. You may choose from two (2) url shorteners (Tinyurl and Bitly) to shrink the links." +msgstr "" + +#. Author of the plugin +msgid "Joel Sanguenza, Lord Francis Navarro" +msgstr "" + +#. Author URI of the plugin +msgid "https://chykalophia.com" +msgstr "" + +#: admin/class-cklph-wdl-admin.php:82 +#: admin/class-cklph-wdl-admin.php:83 +msgid "Woocommerce Direct Links" +msgstr "" + +#: admin/class-cklph-wdl-admin.php:93 +msgid "WooCommerce Direct Link Settings" +msgstr "" + +#: admin/class-cklph-wdl-admin.php:94 +msgid "Settings" +msgstr "" + +#: admin/class-cklph-wdl-admin.php:130 +msgid "Woo Products Checkout URLs" +msgstr "" + +#: admin/partials/cklph-wdl-admin-page.php:26 +msgid "Search Products" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:52 +msgid "Product Name" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:53 +msgid "SKU" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:54 +msgid "Attributes" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:55 +msgid "Thumbnail" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:57 +msgid "Add to Cart Link" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:58 +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:354 +msgid "Copy Link" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:278 +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:302 +msgid "ID" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:291 +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:325 +msgid "Variation ID" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:308 +msgid "Edit" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:313 +msgid "View" +msgstr "" + +#: admin/partials/list-tables/class-cklph-wdl-list-table.php:419 +msgid "Filter" +msgstr "" + +#: woocommerce-direct-links.php:68 +msgid "Cannot activate WooCommerce Direct Links without WooCommerce installed and activated." +msgstr "" diff --git a/uninstall.php b/uninstall.php new file mode 100644 index 0000000..7413498 --- /dev/null +++ b/uninstall.php @@ -0,0 +1,55 @@ + array( 'product_variation', 'product' ), + 'post_status' => 'publish', + 'posts_per_page' => -1, + 'fields' => 'ids', +); + +$query = new WP_Query( $post_args ); + +foreach ( $query->posts as $product ) { + delete_post_meta( $product, 'bitly_link' ); + delete_post_meta( $product, 'joturl_link' ); + delete_post_meta( $product, 'yourls_link' ); + delete_post_meta( $product, 'tinyurl_link' ); +} + +delete_option( 'url_options' ); +delete_option( 'yourls_domain' ); +delete_option( 'joturl_public' ); +delete_option( 'tinyurl_domain' ); +delete_option( 'joturl_private' ); +delete_option( 'yourls_signature' ); +delete_option( 'bitly_access_token_options' ); +delete_option( 'tinyurl_access_token_options' ); diff --git a/woocommerce-direct-links.php b/woocommerce-direct-links.php new file mode 100644 index 0000000..8297980 --- /dev/null +++ b/woocommerce-direct-links.php @@ -0,0 +1,69 @@ + + * Lord Francis Navarro + * @license GPL-2.0+ http://www.gnu.org/licenses/gpl-2.0.txt + * @link https://chykalophia.com/woocommerce-direct-links + * @since 0.1.0 + * + * @wordpress-plugin + * Plugin Name: WooCommerce Direct Links + * Plugin URI: https://chykalophia.com/woocommerce-direct-links + * Description: An extension plugin for Woocommerce which displays the list of products (simple and variable products) and its variations along with its add-to-cart links. These links automatically adds the product or product variation to the cart and redirects the user to the checkout page. You may choose from two (2) url shorteners (Tinyurl and Bitly) to shrink the links. + * Version: 0.1.0 + * Author: Joel Sanguenza, Lord Francis Navarro + * Author URI: https://chykalophia.com + * License: GPL-2.0+ + * License URI: http://www.gnu.org/licenses/gpl-2.0.txt + * Text Domain: woocommerce-direct-links + * Domain Path: /languages + */ + +// If this file is called directly, abort. +if (!defined('ABSPATH')) { + die; +} + +/** + * Current plugin version. + */ +define('WDL_VERSION', '0.1.0'); + +// Constants. +define('CKLPH_WDL_URL', plugin_dir_url(__FILE__)); +define('CKLPH_WDL_PATH', plugin_dir_path(__FILE__)); +define('CKLPH_WDL_BASE', plugin_basename(__FILE__)); + +/** + * The code that runs during plugin activation. + */ +function cklph_wdl_activate() +{ + flush_rewrite_rules(); +} +register_activation_hook(__FILE__, 'cklph_wdl_activate'); +register_deactivation_hook(__FILE__, 'cklph_wdl_activate'); + + +/** + * The core plugin class that is used to define internationalization, + * admin-specific hooks, and public-facing site hooks. + */ +require CKLPH_WDL_PATH . 'includes/class-cklph-wdl.php'; + +/** + * Begins execution of the plugin. + * + * @since 0.1.0 + */ +function run_wdl() +{ + $plugin = new Cklph_Wdl(); // To be updated into a singleton class. + $plugin->run(); +} + +run_wdl(); \ No newline at end of file