studiou-wc-free-photo-product.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Plugin Name: QDR - Studiou WC Free Photo Product
  4. * Plugin URI: https://www.quadarax.com/plugins/studiou-wc-free-photo-product
  5. * Description: Allows publishing special WooCommerce products with variants and custom raw image upload for photo printing
  6. * Version: 1.0.0
  7. * Requires at least: 6.9.4
  8. * Requires PHP: 8.2
  9. * Author: Dalibor Votruba
  10. * Author URI: https://www.quadarax.com
  11. * License: GPL v2 or later
  12. * License URI: https://www.gnu.org/licenses/gpl-2.0.html
  13. * Text Domain: studiou-wc-free-photo-product
  14. * Domain Path: /languages
  15. * WC requires at least: 10.0
  16. * WC tested up to: 10.6
  17. * Woo: 12346:a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4
  18. */
  19. if (!defined('WPINC')) {
  20. die;
  21. }
  22. if (!defined('STUDIOU_WCFPP_VERSION')) define('STUDIOU_WCFPP_VERSION', '1.0.0');
  23. if (!defined('STUDIOU_WCFPP_PLUGIN_DIR')) define('STUDIOU_WCFPP_PLUGIN_DIR', plugin_dir_path(__FILE__));
  24. if (!defined('STUDIOU_WCFPP_PLUGIN_URL')) define('STUDIOU_WCFPP_PLUGIN_URL', plugin_dir_url(__FILE__));
  25. if (!defined('STUDIOU_WCFPP_PLUGIN_BASENAME')) define('STUDIOU_WCFPP_PLUGIN_BASENAME', plugin_basename(__FILE__));
  26. if (!defined('STUDIOU_WCFPP_CHUNK_SIZE')) define('STUDIOU_WCFPP_CHUNK_SIZE', 1024 * 1024); // 1MB chunks
  27. class Studiou_WC_Free_Photo_Product {
  28. /** @var Studiou_WC_FPP_DB */
  29. private $db;
  30. /** @var Studiou_WC_FPP_Product */
  31. private $product;
  32. /** @var Studiou_WC_FPP_Upload */
  33. private $upload;
  34. /** @var Studiou_WC_FPP_Shortcode */
  35. private $shortcode;
  36. /** @var Studiou_WC_FPP_Cart */
  37. private $cart;
  38. /** @var Studiou_WC_FPP_Admin */
  39. private $admin;
  40. public function __construct() {
  41. $this->load_dependencies();
  42. add_action('admin_init', array($this, 'check_required_plugins'));
  43. add_action('admin_menu', array($this, 'add_admin_menu'), 99);
  44. add_action('admin_enqueue_scripts', array($this, 'register_admin_assets'));
  45. add_action('wp_enqueue_scripts', array($this, 'register_frontend_assets'));
  46. $this->init_components();
  47. add_action('before_woocommerce_init', array($this, 'declare_hpos_compatibility'));
  48. }
  49. private function load_dependencies() {
  50. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-db.php';
  51. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-product.php';
  52. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-upload.php';
  53. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-shortcode.php';
  54. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-cart.php';
  55. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-admin.php';
  56. }
  57. private function init_components() {
  58. $this->db = new Studiou_WC_FPP_DB();
  59. $this->product = new Studiou_WC_FPP_Product();
  60. $this->upload = new Studiou_WC_FPP_Upload($this->db);
  61. $this->shortcode = new Studiou_WC_FPP_Shortcode();
  62. $this->cart = new Studiou_WC_FPP_Cart($this->db);
  63. $this->admin = new Studiou_WC_FPP_Admin($this->db);
  64. }
  65. public function check_required_plugins() {
  66. if (!class_exists('WooCommerce')) {
  67. add_action('admin_notices', function () {
  68. echo '<div class="notice notice-error"><p>';
  69. echo esc_html__('QDR - Studiou WC Free Photo Product requires WooCommerce to be installed and activated.', 'studiou-wc-free-photo-product');
  70. echo '</p></div>';
  71. });
  72. }
  73. }
  74. public function declare_hpos_compatibility() {
  75. if (class_exists('Automattic\WooCommerce\Utilities\FeaturesUtil')) {
  76. \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
  77. 'custom_order_tables',
  78. __FILE__,
  79. true
  80. );
  81. }
  82. }
  83. public function add_admin_menu() {
  84. if (class_exists('WooCommerce')) {
  85. add_submenu_page(
  86. 'edit.php?post_type=product',
  87. __('Free Photo Files', 'studiou-wc-free-photo-product'),
  88. __('Free Photo Files', 'studiou-wc-free-photo-product'),
  89. 'manage_woocommerce',
  90. 'studiou-fpp-summary',
  91. array($this->admin, 'render_summary_page')
  92. );
  93. }
  94. }
  95. public function register_admin_assets($hook) {
  96. // Product edit page
  97. $screen = get_current_screen();
  98. if ($screen && ($screen->id === 'product' || $screen->post_type === 'product')) {
  99. wp_enqueue_style(
  100. 'studiou-wcfpp-admin',
  101. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/admin.css',
  102. array(),
  103. STUDIOU_WCFPP_VERSION
  104. );
  105. wp_enqueue_script(
  106. 'studiou-wcfpp-admin',
  107. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/admin.js',
  108. array('jquery'),
  109. STUDIOU_WCFPP_VERSION,
  110. true
  111. );
  112. wp_localize_script('studiou-wcfpp-admin', 'studiouWcfpp', array(
  113. 'ajaxUrl' => admin_url('admin-ajax.php'),
  114. 'nonce' => wp_create_nonce('studiou-wcfpp-nonce'),
  115. 'i18n' => array(
  116. 'confirmDelete' => __('Are you sure you want to delete the selected files?', 'studiou-wc-free-photo-product'),
  117. 'confirmDeleteSingle' => __('Are you sure you want to delete this file?', 'studiou-wc-free-photo-product'),
  118. 'noFilesSelected' => __('No files selected.', 'studiou-wc-free-photo-product'),
  119. 'statusUpdated' => __('Status updated.', 'studiou-wc-free-photo-product'),
  120. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  121. 'generatingZip' => __('Generating ZIP archive...', 'studiou-wc-free-photo-product'),
  122. 'deleting' => __('Deleting...', 'studiou-wc-free-photo-product'),
  123. ),
  124. ));
  125. }
  126. // Admin summary page
  127. if ('product_page_studiou-fpp-summary' === $hook) {
  128. wp_enqueue_style(
  129. 'studiou-wcfpp-admin',
  130. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/admin.css',
  131. array(),
  132. STUDIOU_WCFPP_VERSION
  133. );
  134. wp_enqueue_script(
  135. 'studiou-wcfpp-admin',
  136. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/admin.js',
  137. array('jquery'),
  138. STUDIOU_WCFPP_VERSION,
  139. true
  140. );
  141. wp_localize_script('studiou-wcfpp-admin', 'studiouWcfpp', array(
  142. 'ajaxUrl' => admin_url('admin-ajax.php'),
  143. 'nonce' => wp_create_nonce('studiou-wcfpp-nonce'),
  144. 'i18n' => array(
  145. 'confirmDelete' => __('Are you sure you want to delete the selected files?', 'studiou-wc-free-photo-product'),
  146. 'confirmDeleteSingle' => __('Are you sure you want to delete this file?', 'studiou-wc-free-photo-product'),
  147. 'noFilesSelected' => __('No files selected.', 'studiou-wc-free-photo-product'),
  148. 'statusUpdated' => __('Status updated.', 'studiou-wc-free-photo-product'),
  149. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  150. 'generatingZip' => __('Generating ZIP archive...', 'studiou-wc-free-photo-product'),
  151. 'deleting' => __('Deleting...', 'studiou-wc-free-photo-product'),
  152. ),
  153. ));
  154. }
  155. }
  156. public function register_frontend_assets() {
  157. wp_register_style(
  158. 'studiou-wcfpp-frontend',
  159. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/frontend.css',
  160. array(),
  161. STUDIOU_WCFPP_VERSION
  162. );
  163. wp_register_script(
  164. 'studiou-wcfpp-frontend',
  165. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/frontend.js',
  166. array('jquery'),
  167. STUDIOU_WCFPP_VERSION,
  168. true
  169. );
  170. wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppFront', array(
  171. 'ajaxUrl' => admin_url('admin-ajax.php'),
  172. 'nonce' => wp_create_nonce('studiou-wcfpp-front-nonce'),
  173. 'chunkSize' => STUDIOU_WCFPP_CHUNK_SIZE,
  174. 'cartUrl' => wc_get_cart_url(),
  175. 'i18n' => array(
  176. 'uploading' => __('Uploading...', 'studiou-wc-free-photo-product'),
  177. 'uploadComplete' => __('Upload complete', 'studiou-wc-free-photo-product'),
  178. 'uploadError' => __('Upload failed. Please try again.', 'studiou-wc-free-photo-product'),
  179. 'fileTooLarge' => __('File is too large. Maximum size: %s MB', 'studiou-wc-free-photo-product'),
  180. 'selectVariant' => __('Please select a variant.', 'studiou-wc-free-photo-product'),
  181. 'uploadFile' => __('Please upload a file.', 'studiou-wc-free-photo-product'),
  182. 'addedToCart' => __('Product added to cart!', 'studiou-wc-free-photo-product'),
  183. 'addToCartError' => __('Could not add to cart. Please try again.', 'studiou-wc-free-photo-product'),
  184. 'dragDropText' => __('Drag & drop your file here or click to browse', 'studiou-wc-free-photo-product'),
  185. 'removeFile' => __('Remove', 'studiou-wc-free-photo-product'),
  186. ),
  187. ));
  188. }
  189. public static function activate() {
  190. $db = new Studiou_WC_FPP_DB();
  191. $db->create_tables();
  192. // Create chunks temp directory
  193. $upload_dir = wp_upload_dir();
  194. $chunks_dir = $upload_dir['basedir'] . '/studiou-fpp-chunks';
  195. if (!file_exists($chunks_dir)) {
  196. wp_mkdir_p($chunks_dir);
  197. file_put_contents($chunks_dir . '/.htaccess', 'deny from all');
  198. file_put_contents($chunks_dir . '/index.php', '<?php // Silence is golden');
  199. }
  200. }
  201. public static function deactivate() {
  202. // Clean up temp chunks
  203. $upload_dir = wp_upload_dir();
  204. $chunks_dir = $upload_dir['basedir'] . '/studiou-fpp-chunks';
  205. if (is_dir($chunks_dir)) {
  206. $files = glob($chunks_dir . '/*');
  207. foreach ($files as $file) {
  208. if (is_file($file) && basename($file) !== '.htaccess' && basename($file) !== 'index.php') {
  209. unlink($file);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. // Activation/deactivation hooks
  216. register_activation_hook(__FILE__, array('Studiou_WC_Free_Photo_Product', 'activate'));
  217. register_deactivation_hook(__FILE__, array('Studiou_WC_Free_Photo_Product', 'deactivate'));
  218. function run_studiou_wc_free_photo_product() {
  219. static $initialized = false;
  220. if ($initialized) {
  221. return;
  222. }
  223. $initialized = true;
  224. load_plugin_textdomain(
  225. 'studiou-wc-free-photo-product',
  226. false,
  227. dirname(plugin_basename(__FILE__)) . '/languages/'
  228. );
  229. new Studiou_WC_Free_Photo_Product();
  230. }
  231. add_action('plugins_loaded', 'run_studiou_wc_free_photo_product');