<?php
/**
 * Pro dependencies class.
 *
 * @package SaveToWishlist\Classes\Integrations
 * @since 1.0.2
 */
namespace SaveToWishlist\Classes\Integrations;

use SaveToWishlist\Abstracts\Singleton;
use SaveToWishlist\Helpers\Helper_Functions;

/**
 * Class Pro_Dependencies
 *
 * This class serves as the pro dependencies class.
 */
class Pro_Dependencies extends Singleton {
    /**
     * Helper functions.
     *
     * @var Helper_Functions
     */
    private $helper_functions;

    /**
     * Constructor.
     *
     * @since 1.0.0
     * @access public
     */
    public function __construct() {
        $this->helper_functions = Helper_Functions::instance();
    }

    /**
     * Check if PRO is installed.
     *
     * @since 1.0.2
     * @access public
     *
     * @return boolean True if PRO is installed, false otherwise.
     */
    public function is_pro_installed() {
        if ( ! function_exists( 'is_plugin_active' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        return is_plugin_active( 'saveto-wishlist-pro-for-woocommerce/saveto-wishlist-pro-for-woocommerce.php' );
    }

    /**
     * Get the pro dependencies.
     *
     * @since 1.0.2
     * @access public
     *
     * @return array Pro dependencies.
     */
    public function get_pro_dependencies() {
        $pro_dependencies = array();
        if ( $this->is_pro_installed() ) {
            $pro_dependencies = array(
                'activePro' => true,
                'wpRoles'   => $this->get_wp_roles(),
            );
        }

        // Filter runs unconditionally so sibling plugins (e.g. the registry
        // extension) can advertise their own capability flags even when pro
        // isn't installed.
        return apply_filters( 'stwlite_get_pro_dependencies', $pro_dependencies );
    }

    /**
     * Get the WP roles.
     *
     * @since 1.0.2
     * @access public
     *
     * @return array WP roles.
     */
    public function get_wp_roles() {
        $roles    = array();
        $wp_roles = new \WP_Roles();
        foreach ( $wp_roles->roles as $role => $data ) {
            $roles[ $role ] = $data['name'];
        }

        return apply_filters( 'stwlite_wordpress_roles', $roles );
    }
}
