<?php
defined( 'ABSPATH' ) || exit();

class WPCP_Pro_Admin {
    /**
     * The single instance of the class.
     *
     * @var WPCP_Pro_Admin
     * @since 1.0.0
     */
    protected static $_instance = null;

    /**
     * @since 1.2.0
     * WPCP_Pro_Admin constructor.
     */
    public function __construct() {
        $this->includes();
        $this->init_hooks();
    }

    /**
     * Includes
     * @since 1.2.0
     */
    public function includes() {
        require_once( dirname( __FILE__ ) . '/metabox-functions.php' );
    }

    /**
     * Init hooks
     * @since 1.2.0
     */
    public function init_hooks() {
        //unlock fields
        add_filter( 'wpcp_text_input', array( $this, 'unlock_input_field' ) );
        add_filter( 'wpcp_select_input', array( $this, 'unlock_input_field' ) );
        add_filter( 'wpcp_textarea_input', array( $this, 'unlock_input_field' ) );
        add_filter( 'wpcp_checkbox_input', array( $this, 'unlock_input_field' ) );
        add_filter( 'wpcp_range_input', array( $this, 'unlock_campaign_target' ) );
        remove_action( 'wpcp_advanced_settings_metabox', 'wpcp_search_replace_metafield' );
        remove_action( 'wpcp_advanced_settings_metabox', 'wpcp_post_meta_metafield' );
        //unlock modules
        add_filter( 'wpcp_campaign_modules_list', array( $this, 'campaign_modules_list_unlock' ) );
        //save pro meta
        add_action( 'wpcp_update_campaign_settings', array( $this, 'update_campaign_settings' ), 10, 2 );

        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    }

    /**
     * @param $args
     *
     * @return array
     * @since 1.2.0
     */
    public function unlock_input_field( $args ) {
        if ( isset( $args['wrapper_class'] ) && strpos( $args['wrapper_class'], 'pro' ) !== false && isset( $args['attrs'] ) && array_key_exists( 'disabled', $args['attrs'] ) ) {
            unset( $args['attrs']['disabled'] );
            $args['wrapper_class'] = str_replace( 'pro', '', $args['wrapper_class'] );
        }

        return $args;
    }

    /**
     * @param $modules
     *
     * @return mixed
     * @since 1.2.0
     */
    public function campaign_modules_list_unlock( $modules ) {
        foreach ( $modules as $key => $module ) {
            $modules[ $key ]['disabled'] = false;
        }

        return $modules;
    }

    /**
     * @param $campaign_id
     * @param $posted
     *
     * @since 1.2.0
     */
    public function update_campaign_settings( $campaign_id, $posted ) {
        update_post_meta( $campaign_id, '_external_post', isset( $posted['_external_post'] ) ? sanitize_text_field( $posted['_external_post'] ) : '' );
        update_post_meta( $campaign_id, '_random_featured_image', isset( $posted['_random_featured_image'] ) ? sanitize_text_field( $posted['_random_featured_image'] ) : '' );
        update_post_meta( $campaign_id, '_canonical_tag', isset( $posted['_canonical_tag'] ) ? sanitize_text_field( $posted['_canonical_tag'] ) : '' );
        update_post_meta( $campaign_id, '_add_rel_no_follow_target', isset( $posted['_add_rel_no_follow_target'] ) ? sanitize_text_field( $posted['_add_rel_no_follow_target'] ) : '' );
        update_post_meta( $campaign_id, '_min_words', isset( $posted['_min_words'] ) ? intval( $posted['_min_words'] ) : '' );
        update_post_meta( $campaign_id, '_required_words', isset( $posted['_required_words'] ) ? sanitize_text_field( $posted['_required_words'] ) : '' );
        update_post_meta( $campaign_id, '_banned_words', isset( $posted['_banned_words'] ) ? sanitize_text_field( $posted['_banned_words'] ) : '' );
//        update_post_meta( $campaign_id, '_wpcp_search_replaces', isset( $posted['_wpcp_search_replaces'] ) ? sanitize_textarea_field( $posted['_wpcp_search_replaces'] ) : '' );
//        update_post_meta( $campaign_id, '_wpcp_custom_meta_fields', isset( $posted['_wpcp_custom_meta_fields'] ) ? sanitize_textarea_field( $posted['_wpcp_custom_meta_fields'] ) : '' );
        update_post_meta( $campaign_id, '_keyword_to_category', isset( $posted['_keyword_to_category'] ) ? sanitize_text_field( $posted['_keyword_to_category'] ) : '' );
        update_post_meta( $campaign_id, '_keyword_to_tag', isset( $posted['_keyword_to_tag'] ) ? sanitize_text_field( $posted['_keyword_to_tag'] ) : '' );
        update_post_meta( $campaign_id, '_translator', isset( $posted['_translate_to'] ) ? sanitize_key( $posted['_translator'] ) : '' );
        update_post_meta( $campaign_id, '_translate_to', isset( $posted['_translate_to'] ) ?  sanitize_text_field($posted['_translate_to'])  : '' );

        update_post_meta( $campaign_id, '_article_region', isset( $posted['_article_region'] ) ? sanitize_text_field( $posted['_article_region'] ) : '' );
        update_post_meta( $campaign_id, '_article_language', isset( $posted['_article_language'] ) ? sanitize_text_field( $posted['_article_language'] ) : '' );

    }

    public function enqueue_scripts() {
        $js_dir     = WPCP_PRO_ASSETS_URL . '/js/';
        $css_dir    = WPCP_PRO_ASSETS_URL . '/css/';
        $vendor_dir = WPCP_PRO_ASSETS_URL . '/vendor/';

        // Use minified libraries if SCRIPT_DEBUG is turned off
        $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '';
        wp_enqueue_script( 'jquery-repeater', '//cdnjs.cloudflare.com/ajax/libs/jquery.repeater/1.2.1/jquery.repeater.min.js', array( 'jquery' ), WPCP_VERSION, false );
        wp_enqueue_script( 'wp-content-pilot-pro', $js_dir . 'wp-content-pilot-pro' . $suffix . '.js', array(
            'jquery',
            'wp-util',
            'jquery-repeater'
        ), WPCP_VERSION, false );
        wp_enqueue_style( 'wp-content-pilot-pro', $css_dir . 'wp-content-pilot-pro.css', array(), WPCP_VERSION );
    }

    /**
     * Main WPCP_Pro_Admin Instance.
     *
     * Ensures only one instance of WPCP_Admin is loaded or can be loaded.
     *
     * @return WPCP_Pro_Admin - Main instance.
     * @since 1.0.0
     * @static
     */
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

WPCP_Pro_Admin::instance();
