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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.3.6
  7. * Requires at least: 6.9.4
  8. * Requires PHP: 8.2
  9. * Requires Plugins: woocommerce, product-variant-table-for-woocommerce
  10. * Author: Dalibor Votruba
  11. * Author URI: https://www.quadarax.com
  12. * License: GPL v2 or later
  13. * License URI: https://www.gnu.org/licenses/gpl-2.0.html
  14. * Text Domain: studiou-wc-free-photo-product
  15. * Domain Path: /languages
  16. * WC requires at least: 10.0
  17. * WC tested up to: 10.6
  18. * Woo: 12346:a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4
  19. */
  20. if (!defined('WPINC')) {
  21. die;
  22. }
  23. if (!defined('STUDIOU_WCFPP_VERSION')) define('STUDIOU_WCFPP_VERSION', '1.3.6');
  24. if (!defined('STUDIOU_WCFPP_PLUGIN_DIR')) define('STUDIOU_WCFPP_PLUGIN_DIR', plugin_dir_path(__FILE__));
  25. if (!defined('STUDIOU_WCFPP_PLUGIN_URL')) define('STUDIOU_WCFPP_PLUGIN_URL', plugin_dir_url(__FILE__));
  26. if (!defined('STUDIOU_WCFPP_PLUGIN_BASENAME')) define('STUDIOU_WCFPP_PLUGIN_BASENAME', plugin_basename(__FILE__));
  27. if (!defined('STUDIOU_WCFPP_CHUNK_SIZE')) define('STUDIOU_WCFPP_CHUNK_SIZE', 1024 * 1024); // 1MB chunks
  28. class Studiou_WC_Free_Photo_Product {
  29. /** @var Studiou_WC_FPP_DB */
  30. private $db;
  31. /** @var Studiou_WC_FPP_Product */
  32. private $product;
  33. /** @var Studiou_WC_FPP_Upload */
  34. private $upload;
  35. /** @var Studiou_WC_FPP_Shortcode */
  36. private $shortcode;
  37. /** @var Studiou_WC_FPP_Single_Product */
  38. private $single_product;
  39. /** @var Studiou_WC_FPP_Cart */
  40. private $cart;
  41. /** @var Studiou_WC_FPP_Admin */
  42. private $admin;
  43. /** @var Studiou_WC_FPP_Pricing */
  44. private $pricing;
  45. public function __construct() {
  46. $this->load_dependencies();
  47. add_action('admin_init', array($this, 'check_required_plugins'));
  48. add_action('admin_menu', array($this, 'add_admin_menu'), 99);
  49. add_action('admin_enqueue_scripts', array($this, 'register_admin_assets'));
  50. add_action('wp_enqueue_scripts', array($this, 'register_frontend_assets'));
  51. $this->init_components();
  52. add_action('before_woocommerce_init', array($this, 'declare_hpos_compatibility'));
  53. }
  54. private function load_dependencies() {
  55. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-db.php';
  56. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-product.php';
  57. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-upload.php';
  58. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-shortcode.php';
  59. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-single-product.php';
  60. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-cart.php';
  61. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-admin.php';
  62. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-pricing.php';
  63. }
  64. private function init_components() {
  65. $this->db = new Studiou_WC_FPP_DB();
  66. $this->product = new Studiou_WC_FPP_Product();
  67. $this->upload = new Studiou_WC_FPP_Upload($this->db);
  68. $this->shortcode = new Studiou_WC_FPP_Shortcode();
  69. $this->single_product = new Studiou_WC_FPP_Single_Product($this->db);
  70. $this->cart = new Studiou_WC_FPP_Cart($this->db);
  71. $this->admin = new Studiou_WC_FPP_Admin($this->db);
  72. $this->pricing = new Studiou_WC_FPP_Pricing();
  73. }
  74. public function check_required_plugins() {
  75. if (!class_exists('WooCommerce')) {
  76. add_action('admin_notices', function () {
  77. echo '<div class="notice notice-error"><p>';
  78. echo esc_html__('QDR - Studiou WC Free Photo Product requires WooCommerce to be installed and activated.', 'studiou-wc-free-photo-product');
  79. echo '</p></div>';
  80. });
  81. }
  82. if (!self::is_pvtfw_active()) {
  83. add_action('admin_notices', function () {
  84. echo '<div class="notice notice-error"><p>';
  85. echo esc_html__('QDR - Studiou WC Free Photo Product requires the "Product Variant Table for WooCommerce" plugin to be installed and activated.', 'studiou-wc-free-photo-product');
  86. echo '</p></div>';
  87. });
  88. }
  89. }
  90. /**
  91. * Detects whether the Product Variant Table for WooCommerce (pvtfw) plugin is active,
  92. * regardless of its main file name, by matching the active-plugins list on the folder slug.
  93. */
  94. public static function is_pvtfw_active() {
  95. $slug = 'product-variant-table-for-woocommerce/';
  96. $active = (array) get_option('active_plugins', array());
  97. foreach ($active as $plugin) {
  98. if (strpos($plugin, $slug) === 0) {
  99. return true;
  100. }
  101. }
  102. if (is_multisite()) {
  103. $network = (array) get_site_option('active_sitewide_plugins', array());
  104. foreach (array_keys($network) as $plugin) {
  105. if (strpos($plugin, $slug) === 0) {
  106. return true;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. public function declare_hpos_compatibility() {
  113. if (class_exists('Automattic\WooCommerce\Utilities\FeaturesUtil')) {
  114. \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
  115. 'custom_order_tables',
  116. __FILE__,
  117. true
  118. );
  119. }
  120. }
  121. public function add_admin_menu() {
  122. if (class_exists('WooCommerce')) {
  123. add_submenu_page(
  124. 'edit.php?post_type=product',
  125. __('Free Photo Files', 'studiou-wc-free-photo-product'),
  126. __('Free Photo Files', 'studiou-wc-free-photo-product'),
  127. 'manage_woocommerce',
  128. 'studiou-fpp-summary',
  129. array($this->admin, 'render_summary_page')
  130. );
  131. }
  132. }
  133. public function register_admin_assets($hook) {
  134. // Product edit page
  135. $screen = get_current_screen();
  136. if ($screen && ($screen->id === 'product' || $screen->post_type === 'product')) {
  137. wp_enqueue_style(
  138. 'studiou-wcfpp-admin',
  139. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/admin.css',
  140. array(),
  141. STUDIOU_WCFPP_VERSION
  142. );
  143. wp_enqueue_script(
  144. 'studiou-wcfpp-admin',
  145. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/admin.js',
  146. array('jquery'),
  147. STUDIOU_WCFPP_VERSION,
  148. true
  149. );
  150. wp_localize_script('studiou-wcfpp-admin', 'studiouWcfpp', array(
  151. 'ajaxUrl' => admin_url('admin-ajax.php'),
  152. 'nonce' => wp_create_nonce('studiou-wcfpp-nonce'),
  153. 'i18n' => array(
  154. 'confirmDelete' => __('Are you sure you want to delete the selected files?', 'studiou-wc-free-photo-product'),
  155. 'confirmDeleteSingle' => __('Are you sure you want to delete this file?', 'studiou-wc-free-photo-product'),
  156. 'confirmDownloadZip' => __('Download selected files as ZIP archive?', 'studiou-wc-free-photo-product'),
  157. 'confirmStatusChange' => __('Are you sure you want to change the status of the selected files?', 'studiou-wc-free-photo-product'),
  158. 'noFilesSelected' => __('No files selected.', 'studiou-wc-free-photo-product'),
  159. 'statusUpdated' => __('Status updated.', 'studiou-wc-free-photo-product'),
  160. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  161. 'generatingZip' => __('Generating ZIP archive...', 'studiou-wc-free-photo-product'),
  162. 'generatingCsv' => __('Generating CSV file...', 'studiou-wc-free-photo-product'),
  163. 'deleting' => __('Deleting...', 'studiou-wc-free-photo-product'),
  164. ),
  165. ));
  166. }
  167. // Admin summary page
  168. if ('product_page_studiou-fpp-summary' === $hook) {
  169. wp_enqueue_style(
  170. 'studiou-wcfpp-admin',
  171. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/admin.css',
  172. array(),
  173. STUDIOU_WCFPP_VERSION
  174. );
  175. wp_enqueue_script(
  176. 'studiou-wcfpp-admin',
  177. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/admin.js',
  178. array('jquery'),
  179. STUDIOU_WCFPP_VERSION,
  180. true
  181. );
  182. wp_localize_script('studiou-wcfpp-admin', 'studiouWcfpp', array(
  183. 'ajaxUrl' => admin_url('admin-ajax.php'),
  184. 'nonce' => wp_create_nonce('studiou-wcfpp-nonce'),
  185. 'i18n' => array(
  186. 'confirmDelete' => __('Are you sure you want to delete the selected files?', 'studiou-wc-free-photo-product'),
  187. 'confirmDeleteSingle' => __('Are you sure you want to delete this file?', 'studiou-wc-free-photo-product'),
  188. 'confirmDownloadZip' => __('Download selected files as ZIP archive?', 'studiou-wc-free-photo-product'),
  189. 'confirmStatusChange' => __('Are you sure you want to change the status of the selected files?', 'studiou-wc-free-photo-product'),
  190. 'noFilesSelected' => __('No files selected.', 'studiou-wc-free-photo-product'),
  191. 'statusUpdated' => __('Status updated.', 'studiou-wc-free-photo-product'),
  192. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  193. 'generatingZip' => __('Generating ZIP archive...', 'studiou-wc-free-photo-product'),
  194. 'generatingCsv' => __('Generating CSV file...', 'studiou-wc-free-photo-product'),
  195. 'deleting' => __('Deleting...', 'studiou-wc-free-photo-product'),
  196. ),
  197. ));
  198. }
  199. }
  200. public function register_frontend_assets() {
  201. wp_register_style(
  202. 'studiou-wcfpp-frontend',
  203. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/frontend.css',
  204. array(),
  205. STUDIOU_WCFPP_VERSION
  206. );
  207. wp_register_script(
  208. 'studiou-wcfpp-frontend',
  209. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/frontend.js',
  210. array('jquery'),
  211. STUDIOU_WCFPP_VERSION,
  212. true
  213. );
  214. wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppFront', array(
  215. 'ajaxUrl' => admin_url('admin-ajax.php'),
  216. 'nonce' => wp_create_nonce('studiou-wcfpp-front-nonce'),
  217. 'chunkSize' => STUDIOU_WCFPP_CHUNK_SIZE,
  218. 'cartUrl' => wc_get_cart_url(),
  219. 'currency' => array(
  220. 'symbol' => function_exists('get_woocommerce_currency_symbol') ? get_woocommerce_currency_symbol() : '',
  221. 'priceFormat' => function_exists('get_woocommerce_price_format') ? get_woocommerce_price_format() : '%1$s%2$s',
  222. 'decimalSeparator' => function_exists('wc_get_price_decimal_separator') ? wc_get_price_decimal_separator() : '.',
  223. 'thousandSeparator' => function_exists('wc_get_price_thousand_separator') ? wc_get_price_thousand_separator() : ',',
  224. 'decimals' => function_exists('wc_get_price_decimals') ? wc_get_price_decimals() : 2,
  225. ),
  226. 'i18n' => array(
  227. 'uploading' => __('Uploading...', 'studiou-wc-free-photo-product'),
  228. 'uploadComplete' => __('Upload complete', 'studiou-wc-free-photo-product'),
  229. 'processing' => __('Processing...', 'studiou-wc-free-photo-product'),
  230. 'uploadError' => __('Upload failed. Please try again.', 'studiou-wc-free-photo-product'),
  231. 'fileTooLarge' => __('File is too large. Maximum size: %s MB', 'studiou-wc-free-photo-product'),
  232. 'selectVariant' => __('Please select a variant.', 'studiou-wc-free-photo-product'),
  233. 'uploadFile' => __('Please upload a file.', 'studiou-wc-free-photo-product'),
  234. 'addedToCart' => __('Product added to cart!', 'studiou-wc-free-photo-product'),
  235. 'addToCartError' => __('Could not add to cart. Please try again.', 'studiou-wc-free-photo-product'),
  236. 'dragDropText' => __('Drag & drop your file here or click to browse', 'studiou-wc-free-photo-product'),
  237. 'removeFile' => __('Remove', 'studiou-wc-free-photo-product'),
  238. 'qtyTiersTitle' => __('Quantity Discount', 'studiou-wc-free-photo-product'),
  239. 'qtyTiersFromQty' => __('From Qty', 'studiou-wc-free-photo-product'),
  240. 'qtyTiersDiscount' => __('Discount', 'studiou-wc-free-photo-product'),
  241. 'qtyTiersUnitPrice' => __('Unit Price', 'studiou-wc-free-photo-product'),
  242. ),
  243. ));
  244. }
  245. public static function activate() {
  246. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-db.php';
  247. $db = new Studiou_WC_FPP_DB();
  248. $db->create_tables();
  249. // Create chunks temp directory
  250. $upload_dir = wp_upload_dir();
  251. $chunks_dir = $upload_dir['basedir'] . '/studiou-fpp-chunks';
  252. if (!file_exists($chunks_dir)) {
  253. wp_mkdir_p($chunks_dir);
  254. file_put_contents($chunks_dir . '/.htaccess', 'deny from all');
  255. file_put_contents($chunks_dir . '/index.php', '<?php // Silence is golden');
  256. }
  257. }
  258. public static function deactivate() {
  259. // Clean up temp chunks
  260. $upload_dir = wp_upload_dir();
  261. $chunks_dir = $upload_dir['basedir'] . '/studiou-fpp-chunks';
  262. if (is_dir($chunks_dir)) {
  263. $files = glob($chunks_dir . '/*');
  264. foreach ($files as $file) {
  265. if (is_file($file) && basename($file) !== '.htaccess' && basename($file) !== 'index.php') {
  266. unlink($file);
  267. }
  268. }
  269. }
  270. }
  271. }
  272. // Activation/deactivation hooks
  273. register_activation_hook(__FILE__, array('Studiou_WC_Free_Photo_Product', 'activate'));
  274. register_deactivation_hook(__FILE__, array('Studiou_WC_Free_Photo_Product', 'deactivate'));
  275. function run_studiou_wc_free_photo_product() {
  276. static $initialized = false;
  277. if ($initialized) {
  278. return;
  279. }
  280. $initialized = true;
  281. load_plugin_textdomain(
  282. 'studiou-wc-free-photo-product',
  283. false,
  284. dirname(plugin_basename(__FILE__)) . '/languages/'
  285. );
  286. new Studiou_WC_Free_Photo_Product();
  287. }
  288. add_action('plugins_loaded', 'run_studiou_wc_free_photo_product');