| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_FPP_Shortcode {
- public function __construct() {
- add_shortcode('studiou_free_photo_product', array($this, 'render_shortcode'));
- }
- public function render_shortcode($atts) {
- $atts = shortcode_atts(array(
- 'id' => 0,
- ), $atts, 'studiou_free_photo_product');
- $product_id = absint($atts['id']);
- if (!$product_id) {
- return '<p class="studiou-fpp-error">' . esc_html__('Product ID is required.', 'studiou-wc-free-photo-product') . '</p>';
- }
- $product = wc_get_product($product_id);
- if (!$product) {
- return '<p class="studiou-fpp-error">' . esc_html__('Product not found.', 'studiou-wc-free-photo-product') . '</p>';
- }
- if (!Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
- return '<p class="studiou-fpp-error">' . esc_html__('This product does not support photo upload.', 'studiou-wc-free-photo-product') . '</p>';
- }
- if (!$product->is_type('variable')) {
- return '<p class="studiou-fpp-error">' . esc_html__('This product must be a variable product.', 'studiou-wc-free-photo-product') . '</p>';
- }
- // Enqueue assets
- wp_enqueue_style('studiou-wcfpp-frontend');
- wp_enqueue_script('studiou-wcfpp-frontend');
- $max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id);
- // Set global product so WC template functions work
- global $post;
- $original_post = $post;
- $post = get_post($product_id);
- setup_postdata($post);
- ob_start();
- include STUDIOU_WCFPP_PLUGIN_DIR . 'views/frontend-product.php';
- $output = ob_get_clean();
- // Restore original post
- $post = $original_post;
- if ($original_post) {
- setup_postdata($original_post);
- } else {
- wp_reset_postdata();
- }
- return $output;
- }
- }
|