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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.5.8
  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.5.8');
  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->maybe_upgrade_db();
  67. $this->product = new Studiou_WC_FPP_Product();
  68. $this->upload = new Studiou_WC_FPP_Upload($this->db);
  69. $this->shortcode = new Studiou_WC_FPP_Shortcode();
  70. $this->single_product = new Studiou_WC_FPP_Single_Product($this->db);
  71. $this->cart = new Studiou_WC_FPP_Cart($this->db);
  72. $this->admin = new Studiou_WC_FPP_Admin($this->db);
  73. $this->pricing = new Studiou_WC_FPP_Pricing();
  74. }
  75. /**
  76. * Run dbDelta when the installed schema is older than the current plugin version.
  77. * dbDelta handles adding the batch_token column and its KEY on existing installs.
  78. */
  79. private function maybe_upgrade_db() {
  80. $installed = get_option('studiou_wcfpp_db_version', '');
  81. if (version_compare((string) $installed, STUDIOU_WCFPP_VERSION, '<')) {
  82. $this->db->create_tables();
  83. }
  84. }
  85. public function check_required_plugins() {
  86. if (!class_exists('WooCommerce')) {
  87. add_action('admin_notices', function () {
  88. echo '<div class="notice notice-error"><p>';
  89. echo esc_html__('QDR - Studiou WC Free Photo Product requires WooCommerce to be installed and activated.', 'studiou-wc-free-photo-product');
  90. echo '</p></div>';
  91. });
  92. }
  93. if (!self::is_pvtfw_active()) {
  94. add_action('admin_notices', function () {
  95. echo '<div class="notice notice-error"><p>';
  96. 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');
  97. echo '</p></div>';
  98. });
  99. }
  100. }
  101. /**
  102. * Detects whether the Product Variant Table for WooCommerce (pvtfw) plugin is active,
  103. * regardless of its main file name, by matching the active-plugins list on the folder slug.
  104. */
  105. public static function is_pvtfw_active() {
  106. $slug = 'product-variant-table-for-woocommerce/';
  107. $active = (array) get_option('active_plugins', array());
  108. foreach ($active as $plugin) {
  109. if (strpos($plugin, $slug) === 0) {
  110. return true;
  111. }
  112. }
  113. if (is_multisite()) {
  114. $network = (array) get_site_option('active_sitewide_plugins', array());
  115. foreach (array_keys($network) as $plugin) {
  116. if (strpos($plugin, $slug) === 0) {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. public function declare_hpos_compatibility() {
  124. if (class_exists('Automattic\WooCommerce\Utilities\FeaturesUtil')) {
  125. \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
  126. 'custom_order_tables',
  127. __FILE__,
  128. true
  129. );
  130. }
  131. }
  132. public function add_admin_menu() {
  133. if (class_exists('WooCommerce')) {
  134. add_submenu_page(
  135. 'edit.php?post_type=product',
  136. __('Free Photo Files', 'studiou-wc-free-photo-product'),
  137. __('Free Photo Files', 'studiou-wc-free-photo-product'),
  138. 'manage_woocommerce',
  139. 'studiou-fpp-summary',
  140. array($this->admin, 'render_summary_page')
  141. );
  142. }
  143. }
  144. public function register_admin_assets($hook) {
  145. // Product edit page
  146. $screen = get_current_screen();
  147. if ($screen && ($screen->id === 'product' || $screen->post_type === 'product')) {
  148. wp_enqueue_style(
  149. 'studiou-wcfpp-admin',
  150. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/admin.css',
  151. array(),
  152. STUDIOU_WCFPP_VERSION
  153. );
  154. wp_enqueue_script(
  155. 'studiou-wcfpp-admin',
  156. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/admin.js',
  157. array('jquery'),
  158. STUDIOU_WCFPP_VERSION,
  159. true
  160. );
  161. wp_localize_script('studiou-wcfpp-admin', 'studiouWcfpp', array(
  162. 'ajaxUrl' => admin_url('admin-ajax.php'),
  163. 'nonce' => wp_create_nonce('studiou-wcfpp-nonce'),
  164. 'i18n' => array(
  165. 'confirmDelete' => __('Are you sure you want to delete the selected files?', 'studiou-wc-free-photo-product'),
  166. 'confirmDeleteSingle' => __('Are you sure you want to delete this file?', 'studiou-wc-free-photo-product'),
  167. 'confirmDownloadZip' => __('Download selected files as ZIP archive?', 'studiou-wc-free-photo-product'),
  168. 'confirmStatusChange' => __('Are you sure you want to change the status of the selected files?', 'studiou-wc-free-photo-product'),
  169. 'noFilesSelected' => __('No files selected.', 'studiou-wc-free-photo-product'),
  170. 'statusUpdated' => __('Status updated.', 'studiou-wc-free-photo-product'),
  171. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  172. 'generatingZip' => __('Generating ZIP archive...', 'studiou-wc-free-photo-product'),
  173. 'generatingCsv' => __('Generating CSV file...', 'studiou-wc-free-photo-product'),
  174. 'deleting' => __('Deleting...', 'studiou-wc-free-photo-product'),
  175. ),
  176. ));
  177. }
  178. // Admin summary page
  179. if ('product_page_studiou-fpp-summary' === $hook) {
  180. wp_enqueue_style(
  181. 'studiou-wcfpp-admin',
  182. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/admin.css',
  183. array(),
  184. STUDIOU_WCFPP_VERSION
  185. );
  186. wp_enqueue_script(
  187. 'studiou-wcfpp-admin',
  188. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/admin.js',
  189. array('jquery'),
  190. STUDIOU_WCFPP_VERSION,
  191. true
  192. );
  193. wp_localize_script('studiou-wcfpp-admin', 'studiouWcfpp', array(
  194. 'ajaxUrl' => admin_url('admin-ajax.php'),
  195. 'nonce' => wp_create_nonce('studiou-wcfpp-nonce'),
  196. 'i18n' => array(
  197. 'confirmDelete' => __('Are you sure you want to delete the selected files?', 'studiou-wc-free-photo-product'),
  198. 'confirmDeleteSingle' => __('Are you sure you want to delete this file?', 'studiou-wc-free-photo-product'),
  199. 'confirmDownloadZip' => __('Download selected files as ZIP archive?', 'studiou-wc-free-photo-product'),
  200. 'confirmStatusChange' => __('Are you sure you want to change the status of the selected files?', 'studiou-wc-free-photo-product'),
  201. 'noFilesSelected' => __('No files selected.', 'studiou-wc-free-photo-product'),
  202. 'statusUpdated' => __('Status updated.', 'studiou-wc-free-photo-product'),
  203. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  204. 'generatingZip' => __('Generating ZIP archive...', 'studiou-wc-free-photo-product'),
  205. 'generatingCsv' => __('Generating CSV file...', 'studiou-wc-free-photo-product'),
  206. 'deleting' => __('Deleting...', 'studiou-wc-free-photo-product'),
  207. ),
  208. ));
  209. }
  210. }
  211. public function register_frontend_assets() {
  212. wp_register_style(
  213. 'studiou-wcfpp-frontend',
  214. STUDIOU_WCFPP_PLUGIN_URL . 'assets/css/frontend.css',
  215. array(),
  216. STUDIOU_WCFPP_VERSION
  217. );
  218. wp_register_script(
  219. 'studiou-wcfpp-frontend',
  220. STUDIOU_WCFPP_PLUGIN_URL . 'assets/js/frontend.js',
  221. array('jquery'),
  222. STUDIOU_WCFPP_VERSION,
  223. true
  224. );
  225. wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppFront', array(
  226. 'ajaxUrl' => admin_url('admin-ajax.php'),
  227. 'nonce' => wp_create_nonce('studiou-wcfpp-front-nonce'),
  228. 'chunkSize' => STUDIOU_WCFPP_CHUNK_SIZE,
  229. 'cartUrl' => wc_get_cart_url(),
  230. 'currency' => array(
  231. 'symbol' => function_exists('get_woocommerce_currency_symbol') ? get_woocommerce_currency_symbol() : '',
  232. 'priceFormat' => function_exists('get_woocommerce_price_format') ? get_woocommerce_price_format() : '%1$s%2$s',
  233. 'decimalSeparator' => function_exists('wc_get_price_decimal_separator') ? wc_get_price_decimal_separator() : '.',
  234. 'thousandSeparator' => function_exists('wc_get_price_thousand_separator') ? wc_get_price_thousand_separator() : ',',
  235. 'decimals' => function_exists('wc_get_price_decimals') ? wc_get_price_decimals() : 2,
  236. ),
  237. 'i18n' => array(
  238. 'uploading' => __('Uploading...', 'studiou-wc-free-photo-product'),
  239. 'uploadComplete' => __('Upload complete', 'studiou-wc-free-photo-product'),
  240. 'processing' => __('Processing...', 'studiou-wc-free-photo-product'),
  241. 'uploadError' => __('Upload failed. Please try again.', 'studiou-wc-free-photo-product'),
  242. 'fileTooLarge' => __('File is too large. Maximum size: %s MB', 'studiou-wc-free-photo-product'),
  243. 'selectVariant' => __('Please select a variant.', 'studiou-wc-free-photo-product'),
  244. 'uploadFile' => __('Please upload a file.', 'studiou-wc-free-photo-product'),
  245. 'addedToCart' => __('Product added to cart!', 'studiou-wc-free-photo-product'),
  246. 'addToCartError' => __('Could not add to cart. Please try again.', 'studiou-wc-free-photo-product'),
  247. 'dragDropText' => __('Drag & drop your file here or click to browse', 'studiou-wc-free-photo-product'),
  248. 'removeFile' => __('Remove', 'studiou-wc-free-photo-product'),
  249. 'close' => __('Close', 'studiou-wc-free-photo-product'),
  250. 'enlarge' => __('Enlarge', 'studiou-wc-free-photo-product'),
  251. 'addToCart' => __('Add to cart', 'studiou-wc-free-photo-product'),
  252. 'maxUploadsReached' => __('Maximum number of uploads reached (%d).', 'studiou-wc-free-photo-product'),
  253. 'outOfStock' => __('out of stock', 'studiou-wc-free-photo-product'),
  254. 'error' => __('An error occurred.', 'studiou-wc-free-photo-product'),
  255. 'qtyTiersTitle' => __('Quantity Discount', 'studiou-wc-free-photo-product'),
  256. 'qtyTiersFromQty' => __('From Qty', 'studiou-wc-free-photo-product'),
  257. 'qtyTiersDiscount' => __('Discount', 'studiou-wc-free-photo-product'),
  258. 'qtyTiersUnitPrice' => __('Unit Price', 'studiou-wc-free-photo-product'),
  259. ),
  260. ));
  261. }
  262. public static function activate() {
  263. require_once STUDIOU_WCFPP_PLUGIN_DIR . 'includes/class-studiou-wc-fpp-db.php';
  264. $db = new Studiou_WC_FPP_DB();
  265. $db->create_tables();
  266. // Create chunks temp directory
  267. $upload_dir = wp_upload_dir();
  268. $chunks_dir = $upload_dir['basedir'] . '/studiou-fpp-chunks';
  269. if (!file_exists($chunks_dir)) {
  270. wp_mkdir_p($chunks_dir);
  271. file_put_contents($chunks_dir . '/.htaccess', 'deny from all');
  272. file_put_contents($chunks_dir . '/index.php', '<?php // Silence is golden');
  273. }
  274. }
  275. public static function deactivate() {
  276. // Clean up temp chunks
  277. $upload_dir = wp_upload_dir();
  278. $chunks_dir = $upload_dir['basedir'] . '/studiou-fpp-chunks';
  279. if (is_dir($chunks_dir)) {
  280. $files = glob($chunks_dir . '/*');
  281. foreach ($files as $file) {
  282. if (is_file($file) && basename($file) !== '.htaccess' && basename($file) !== 'index.php') {
  283. unlink($file);
  284. }
  285. }
  286. }
  287. }
  288. }
  289. // Activation/deactivation hooks
  290. register_activation_hook(__FILE__, array('Studiou_WC_Free_Photo_Product', 'activate'));
  291. register_deactivation_hook(__FILE__, array('Studiou_WC_Free_Photo_Product', 'deactivate'));
  292. function run_studiou_wc_free_photo_product() {
  293. static $initialized = false;
  294. if ($initialized) {
  295. return;
  296. }
  297. $initialized = true;
  298. load_plugin_textdomain(
  299. 'studiou-wc-free-photo-product',
  300. false,
  301. dirname(plugin_basename(__FILE__)) . '/languages/'
  302. );
  303. new Studiou_WC_Free_Photo_Product();
  304. }
  305. add_action('plugins_loaded', 'run_studiou_wc_free_photo_product');