<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://nitin247.com/
 * @since             1.0.0
 * @package           Wc_Product_Author
 *
 * @wordpress-plugin
 * Plugin Name:       Product Author for WooCommerce
 * Plugin URI:        https://nitin247.com/plugin/wc-product-author/
 * Description:       Product Author for WooCommerce enables author functionality for Woocommerce Products, Author can be assigned to Woocommerce Product using this plugin.
 * Version:           2.0.1
 * Author:            Nitin Prakash
 * Author URI:        https://nitin247.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       wc-product-author
 * Domain Path:       /languages
 * Requires PHP:      7.4
 * Requires at least: 6.2
 * Tested up to: 6.8
 * WC requires at least: 8.2
 * WC tested up to: 10.4
 */

// Exit if accessed directly

// Include dependencies
if ( file_exists( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' ) ) {
	require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
} else {
	wp_die( 'Plugin dependencies not installed!!!' );
}

if ( ! function_exists( 'wcpa_fs' ) ) {
	// Create a helper function for easy SDK access.
	function wcpa_fs() {
		global $wcpa_fs;

		if ( ! isset( $wcpa_fs ) ) {

			// Activate multisite network integration.
			if ( ! defined( 'WP_FS__PRODUCT_4032_MULTISITE' ) ) {
				define( 'WP_FS__PRODUCT_4032_MULTISITE', true );
			}

			$wcpa_fs = fs_dynamic_init(
				array(
					'id'               => '4032',
					'slug'             => 'wc-product-author',
					'type'             => 'plugin',
					'public_key'       => 'pk_e7499d637a3698cec8a6594707be7',
					'is_premium'       => false,
					'has_addons'       => false,
					'has_paid_plans'   => false,
					'is_org_compliant' => true,
					'menu'             => array(
						'first-path' => 'plugins.php',
						'account'    => false,
						'support'    => false,
					),
					'is_live'          => true,
					'anonymous_mode'   => true,
				)
			);
		}

		return $wcpa_fs;
	}

	// Init Freemius.
	wcpa_fs();
	// Signal that SDK was initiated.
	do_action( 'wcpa_fs_loaded' );
}

defined( 'WCPA_FS_PLUGIN_VERSION' ) or define( 'WCPA_FS_PLUGIN_VERSION', '2.0.1' );
defined( 'WCPA_FS_SETTINGS_FIELD' ) or define( 'WCPA_FS_SETTINGS_FIELD', 'wcpa_fs_settings' );

final class Product_Author_WooCommerce {

	/**
	 * Default instance for class
	 *
	 * @var object instance
	 */
	private static $instance;

	/**
	 * Default Ssettings
	 *
	 * @var array
	 */
	private $settings;

	/**
	 * Get Instance
	 *
	 * @since 1.0.5
	 *
	 * @return object initialized object of class.
	 *
	 * Initialize class instance
	 */
	public static function get_instance() {

		if ( is_null( self::$instance ) ) {
				self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Function Construct
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Initialize instructor for class
	 */
	public function __construct() {

		if ( $this->run_check() ) {

			$this->default_settings();

			add_action( 'admin_menu', array( $this, 'add_author_menu_page' ), 100 );
			add_action( 'admin_init', array( $this, 'register_author_settings' ) );

			add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility' ) );
			add_action( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) );

			if ( 'yes' === $this->get_settings( 'enable_author_front' ) ) {
				add_action( 'woocommerce_single_product_summary', array( $this, 'show_author' ), 10, 3 );
			}

			// Add author support for product post type
			$this->post_type_support();
			// Add filter for to show all users in Users dropdown for product post type
			add_filter( 'wp_dropdown_users_args', array( $this, 'dropdown_user_filter' ), 10, 2 );
			// Add select2 to author dropdown
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		}
	}

	/**
	 * Function Declare Compatibility
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Declare compatibilities with WooCommerce
	 */
	public function declare_compatibility() {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'analytics', __FILE__, true );
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'new_navigation', __FILE__, true );
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
		}
	}

	/**
	 * Function Run Check
	 *
	 * @since 1.0.5
	 *
	 * @return bool.
	 *
	 * Check dependencies before running plugin
	 */
	public function run_check() {
		if ( ! $this->is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
			add_action( 'admin_notices', array( $this, 'wc_not_active_notice' ) );
			return false;
		}

		return true;
	}

	/**
	 * Function Add Author Menu Page
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Create admin menu page
	 */
	public function add_author_menu_page() {
		add_submenu_page(
			'woocommerce',
			__( 'Product Author', 'wc-product-author' ),
			__( 'Product Author', 'wc-product-author' ),
			'manage_woocommerce',
			'product-authors',
			array( $this, 'author_admin_page_callback' )
		);
	}

	/**
	 * Function Register Author Settings
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Register Admin Page settings
	 */
	public function register_author_settings() {

		add_settings_section(
			'wcpa_settings_section',
			__( 'Product Author Settings', 'wc-product-author' ),
			array( $this, 'settings_section_callback' ),
			'wcpa_settings_page'
		);

		add_settings_field(
			'enable_author_front',
			'Enable Author in Front',
			array( $this, 'enable_author_front_callback' ),
			'wcpa_settings_page',
			'wcpa_settings_section'
		);

		add_settings_field(
			'allowed_author_roles',
			__( 'Allowed Author Roles', 'wc-product-author' ),
			array( $this, 'allowed_author_roles_callback' ),
			'wcpa_settings_page',
			'wcpa_settings_section'
		);

		register_setting( 'wcpa_settings_page', WCPA_FS_SETTINGS_FIELD . '[enable_author_feature]', 'sanitize_text_field' );
		register_setting( 'wcpa_settings_page', WCPA_FS_SETTINGS_FIELD . '[allowed_author_roles]', array( $this, 'sanitize_roles_array' ) );
	}

	/**
	 * Function Settings Section Callback
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Callback function for settings section
	 */
	public function settings_section_callback() {
		echo esc_html__( 'Customize the settings for Product Author', 'wc-product-author' );
	}

	/**
	 * Function Enable Author Front Callback
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Callback for author front callback
	 */
	public function enable_author_front_callback() {
		$enable_author_feature = esc_html( $this->get_settings( 'enable_author_front' ) );

		echo 'Yes <input type="radio" name="' . esc_attr( WCPA_FS_SETTINGS_FIELD ) . '[enable_author_front]" value="yes" ' . checked( 'yes', $enable_author_feature, false ) . ' />';
		echo ' No <input type="radio" name="' . esc_attr( WCPA_FS_SETTINGS_FIELD ) . '[enable_author_front]" value="no" ' . checked( 'no', $enable_author_feature, false ) . ' />';
	}

	/**
	 * Function Author Admin Page Callback
	 *
	 * @since 1.0.5
	 *
	 * @return void.
	 *
	 * Callback for author admin page callback
	 */
	public function author_admin_page_callback() {
		if ( isset( $_POST['author_form_submitted'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
			// Handle saving author information if form is submitted.
			$posted_data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
			$this->save_setting( $posted_data[ WCPA_FS_SETTINGS_FIELD ] );
		}

		// Display the admin page content with the form.
		echo '<div class="wrap">';
		echo '<h1>' . esc_html__( 'Product Author', 'wc-product-author' ) . '</h1>';
		echo '<form method="post">';
		wp_nonce_field( 'wcpa_settings_page', WCPA_FS_SETTINGS_FIELD . '_nonce' );
		settings_fields( 'wcpa_settings_group' );
		do_settings_sections( 'wcpa_settings_page' );
		submit_button( esc_html__( 'Save Changes', 'wc-product-author' ) );
		echo '<input type="hidden" name="author_form_submitted" value="1" />';
		echo '</form>';

		echo '</div>';
	}

	/**
	 * Function Save Setting
	 *
	 * @since 1.0.5
	 *
	 * @param array $posted_data
	 *
	 * @return void.
	 *
	 * Save Admin backend settings
	 */
	private function save_setting( $posted_data = array() ) {

		if ( ! isset( $_POST[ WCPA_FS_SETTINGS_FIELD . '_nonce' ] ) || ! wp_verify_nonce( sanitize_text_field( $_POST[ WCPA_FS_SETTINGS_FIELD . '_nonce' ] ), 'wcpa_settings_page' ) ) {
			wp_die( 'Security check failed', 'wc-product-author' );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( 'You do not have permission to perform this action.', 'wc-product-author' );
		}

		// Save settings safely
		$default_settings = $this->get_settings( '', true );

		// Sanitize posted data
		$sanitized_data = array();
		if ( ! empty( $posted_data ) ) {
			foreach ( $posted_data as $key => $value ) {
				if ( $key === 'allowed_author_roles' ) {
					// Handle roles array separately
					$sanitized_data[ $key ] = $this->sanitize_roles_array( $value );
				} else {
					$sanitized_data[ $key ] = sanitize_text_field( $value );
				}
			}
		}

		$settings = wp_parse_args( $sanitized_data, $default_settings );

		update_option( WCPA_FS_SETTINGS_FIELD, $settings );
	}

	/**
	 * Function Sanitize Clean
	 *
	 * @since 1.0.5
	 *
	 * @param array $posted_data
	 *
	 * @return array $data sanitized array of input data.
	 *
	 * Sanitize function to clean inputs loop
	 */
	private function sanitize_clean( $posted_data ) {
		$data = array();
		if ( is_array( $posted_data ) ) {
			foreach ( $posted_data as $post_key => $post_data ) {
				$data[ $post_key ] = sanitize_text_field( $post_data );
			}
			return $data;
		} else {
			return sanitize_text_field( $data );
		}
	}

	/**
	 * Function Is Plugin Active
	 *
	 * @since 1.0.5
	 *
	 * @param string $plugin_slug
	 *
	 * @return bool.
	 *
	 * Check if plugin is active
	 */
	public function is_plugin_active( $plugin_slug ) {
		if ( ! in_array( $plugin_slug, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
			return false;
		}
		return true;
	}

	/**
	 * Function WC Not Active Notice
	 *
	 * @since 1.0.5
	 * @return void.
	 *
	 * Admin notice if WooCommerce not Active
	 */
	public function wc_not_active_notice() {
		echo '<div class="notice notice-error">';
		echo '<p>' . esc_html__( 'Product Author for WooCommerce requires active WooCommerce Installation!', 'wc-product-author' ) . '</p>';
		echo '</div>';
	}

	/**
	 * Function Post Type Support
	 *
	 * @since 1.0.5
	 * @return void.
	 *
	 * Add support for product post type
	 */
	public function post_type_support() {
		add_post_type_support( 'product', 'author' );
	}

	/**
	 * Function Action Links
	 *
	 * @since 1.0.5
	 * @param array $links
	 * @return array $links.
	 *
	 * Declare compatibilities with WooCommerce
	 */
	public function action_links( $links ) {
		$links = array_merge(
			array(
				'<a target="blank" href="' . esc_url(
					admin_url( 'admin.php?page=product-authors' )
				) . '">' . __( 'Settings', 'wc-product-author' ) . '</a>',
				'<a target="blank" href="' . esc_url( 'https://neebplugins.com/support/' ) . '">' . __( 'Support Desk', 'wc-product-author' ) . '</a>',
			),
			$links
		);

		return $links;
	}

	/**
	 * Function Show Author
	 *
	 * @since 1.0.5
	 *
	 * @param string $show_label
	 * @param bool   $author_label
	 * @param bool   $show_permalink
	 *
	 * @return bool.
	 *
	 * Declare compatibilities with WooCommerce
	 */
	public function show_author( $show_label = '', $author_label = true, $show_permalink = true ) {
		$author_name     = get_the_author();
		$author_template = '<div class="wcpa_container">';

		if ( true === $author_label ) {
			$author_template .= '<span class="wcpa_label">' . __( 'Author : ', 'wc-product-author' ) . '</span>';
		}
		if ( true === $show_permalink ) {
			$author_name = get_the_author_link();
		}

		$author_template .= '<span class="wcpa_name">' . $author_name . '</span>';
		$author_template .= '</div>';

		// Show Post Author
		echo wp_kses_post( $author_template );
	}

	/**
	 * Function Default Settings
	 *
	 * @since 1.0.5
	 * @return void.
	 *
	 * Add support for product post type
	 */
	public function default_settings() {
		$this->settings = array(
			'enable_author_front'  => 'yes',
			'allowed_author_roles' => array( 'administrator' ),
		);
	}

	/**
	 * Function Get Settings
	 *
	 * @since 1.0.5
	 *
	 * @param string $setting_name
	 * @param bool   $skip_merge
	 *
	 * @return array $settings.
	 *
	 * Get settings based on parameters
	 */
	public function get_settings( $setting_name = '', $skip_merge = false ) {

		$option = get_option( WCPA_FS_SETTINGS_FIELD, array() );

		if ( true === $skip_merge ) {
			$this->settings = $option;
		} else {
			$this->settings = wp_parse_args( $option, $this->settings );
		}

		if ( ! empty( $setting_name ) && isset( $this->settings[ $setting_name ] ) ) {
			return $this->settings[ $setting_name ];
		}

		return $this->settings;
	}

	/**
	 * Function String To Bool
	 *
	 * @since 1.0.5
	 * @return bool $param.
	 *
	 * Convert string to bool
	 */
	public function string_to_bool( $param ) {
		return filter_var( $param, FILTER_VALIDATE_BOOLEAN );
	}

	/*
	Function Dropdown User Filter
	*
	* @since 2.0.1
	* @param array  $args
	* @param object $post
	* @return array $args.
	*/
	public function dropdown_user_filter( $args, $post ) {
		global $pagenow;

		$is_product = false;

		// Method 1: Check the post object
		if ( isset( $post->post_type ) && $post->post_type === 'product' ) {
			$is_product = true;
		}

		// Method 2: Check global post if local post is empty
		if ( ! $is_product && ! $post ) {
			global $post; // phpcs:ignore
			if ( isset( $post->post_type ) && $post->post_type === 'product' ) {
				$is_product = true;
			}
		}

		// Method 3: Check query parameters on edit pages
		if ( ! $is_product && in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) {
			if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'product' ) { //phpcs:ignore
				$is_product = true;
			} elseif ( isset( $_GET['post'] ) ) { // phpcs:ignore
				$post_id = absint( $_GET['post'] ); // phpcs:ignore
				if ( get_post_type( $post_id ) === 'product' ) {
					$is_product = true;
				}
			}
		}

		if ( $is_product ) {
			$args['who']        = '';
			$args['capability'] = '';

			// Get allowed roles from settings
			$allowed_roles = $this->get_settings( 'allowed_author_roles' );

			// If roles are specified, filter users by those roles
			if ( ! empty( $allowed_roles ) && is_array( $allowed_roles ) ) {
				$args['role__in'] = $allowed_roles;
			}
		}

		return $args;
	}

	/**
	 * Enqueue Select2 for Author Dropdown
	 *
	 * @since 2.0.1
	 * @param string $hook
	 * @return void.
	 *
	 * Enqueue WooCommerce select2 for product author dropdown
	 */
	public function enqueue_scripts() {
		global $post, $pagenow;

		// Only load on product edit pages
		if ( ! in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) && !isset( $_GET['page'] ) ) { //phpcs:ignore
			return;
		}

		$is_product = false;
		$is_author = isset( $_GET['page'] ) && 'product-authors' === $_GET['page']; // phpcs:ignore

		if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'product' ) { // phpcs:ignore
			$is_product = true;
		} elseif ( isset( $_GET['post'] ) && get_post_type( absint( $_GET['post'] ) ) === 'product' ) { // phpcs:ignore
			$is_product = true;
		} elseif ( isset( $post->post_type ) && $post->post_type === 'product' ) {
			$is_product = true;
		}

		if ( ! $is_product && ! $is_author ) {
			return;
		}

		// Enqueue WooCommerce select2
		if ( function_exists( 'wc_enqueue_js' ) ) {

			wp_enqueue_style( 'woocommerce_admin_styles' );
			wp_enqueue_script( 'selectWoo' );
			wp_enqueue_style( 'select2' );

			// Initialize select2 on author dropdown
			wc_enqueue_js(
				"
					jQuery(document).ready(function($) {
						console.log( 'Hello' );
						if (typeof $.fn.selectWoo !== 'undefined') {
							$('#post_author_override').selectWoo({
								allowClear: true,
								placeholder: '" . esc_js( __( 'Select an author', 'wc-product-author' ) ) . "',
								width: '100%'
							});
							$('#wcpa_allowed_author_roles').selectWoo({
								placeholder: '" . esc_js( __( 'Select roles (leave empty for all users)', 'wc-product-author' ) ) . "',
								allowClear: true,
								width: '50%'
							});
						}
					});
				"
			);
		}
	}

	/**
	 * Function Allowed Author Roles Callback
	 *
	 * @since 2.0.1
	 *
	 * @return void.
	 *
	 * Callback for allowed author roles
	 */
	public function allowed_author_roles_callback() {
		$allowed_roles = $this->get_settings( 'allowed_author_roles' );
		
		if ( empty( $allowed_roles ) || ! is_array( $allowed_roles ) ) {
			$allowed_roles = array();
		}

		// Get all WordPress roles
		global $wp_roles;
		$all_roles = $wp_roles->roles;

		echo '<select name="' . esc_attr( WCPA_FS_SETTINGS_FIELD ) . '[allowed_author_roles][]" id="wcpa_allowed_author_roles" class="wc-enhanced-select" multiple="multiple" style="min-width: 50%;">';

		foreach ( $all_roles as $role_key => $role_data ) {
			$selected = in_array( $role_key, $allowed_roles, true ) ? 'selected="selected"' : '';
			echo '<option value="' . esc_attr( $role_key ) . '" ' . esc_attr( $selected ) . '>';
			echo esc_html( translate_user_role( $role_data['name'] ) );
			echo '</option>';
		}

		echo '</select>';
		echo '<p class="description">' . esc_html__( 'Select which user roles can be assigned as product authors. Leave empty to allow all users.', 'wc-product-author' ) . '</p>';
	}

	/**
	 * Function Sanitize Roles Array
	 *
	 * @since 2.0.1
	 *
	 * @param array $roles
	 *
	 * @return array $sanitized_roles.
	 *
	 * Sanitize roles array
	 */
	public function sanitize_roles_array( $roles ) {
		if ( ! is_array( $roles ) ) {
			return array();
		}

		global $wp_roles;
		$valid_roles     = array_keys( $wp_roles->roles );
		$sanitized_roles = array();

		foreach ( $roles as $role ) {
			if ( in_array( $role, $valid_roles, true ) ) {
				$sanitized_roles[] = sanitize_text_field( $role );
			}
		}

		return $sanitized_roles;
	}
}

// Initialize the plugin.
function wcpa_fs_init() {
	Product_Author_WooCommerce::get_instance();
}

add_action( 'plugins_loaded', 'wcpa_fs_init' );

if ( ! function_exists( 'wcpa_fs_string_to_bool' ) ) {
	/**
	 * Function WPCA String To Bool
	 *
	 * @since 1.0.5
	 *
	 * @param mixed $param
	 * @return bool.
	 *
	 * Convert string to bool
	 */
	function wcpa_fs_string_to_bool( $param ) {
		return Product_Author_WooCommerce::get_instance()->string_to_bool( $param );
	}
}
