<?php
/*
 * Plugin Name: WP Adverts - Restricted Categories
 * Plugin URI: http://wpadverts.com/
 * Description: This module allows to accept payments using WooCommerce plugin.
 * Author: Mark Winiarski
 * Text Domain: wpadverts-wc
 * Version: 1.0.1
 *
 * Adverts is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.
 *
 * Adverts is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Adverts. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package Adverts
 * @subpackage RestrictedCategories
 * @author Mark Winiarski
 * @global $adverts_namespace array
 * @version 0.1
 */

add_action( 'plugins_loaded', 'adext_restricted_categories_namespace' );
add_action( 'init', 'adext_restricted_categories_init' );

if(is_admin() ) {
    add_action( 'init', 'adext_restricted_categories_init_admin' );
} else {
    add_action( 'init', 'adext_restricted_categories_init_frontend' );
}

/**
 * Adds default addon configuration to $adverts_namespace
 *
 * @global array $adverts_namespace
 *
 * @access public
 * @since 1.0
 * @return void
 */
function adext_restricted_categories_namespace() {
    global $adverts_namespace;

    // Add Restricted Categories to adverts_namespace, in order to store module options and default options
    $adverts_namespace['restricted_categories'] = array(
        'option_name' => 'adext_restricted_categories_config',
        'default' => array(

        )
    );
}

/**
 * Inits Adverts-Restricted Categories global filters
 *
 * This function executes actions/filters that need to be run with every request
 * when the integration is enabled.
 *
 * @access public
 * @since 1.0
 * @return void
 */
function adext_restricted_categories_init() {

    load_plugin_textdomain("wpadverts-restricted_categories", false, dirname(plugin_basename(__FILE__))."/languages/");

    if( ! defined( "ADVERTS_PATH" ) ) {
        return;
    }

}

/**
 * Init Restricted Categories admin filters and actions
 *
 * @since 1.0
 * @return void
 */
function adext_restricted_categories_init_admin() {
    if( ! defined( "ADVERTS_PATH" ) ) {
        return;
    }

    include_once ADVERTS_PATH . 'includes/class-updates-manager.php';
    $manager = new Adverts_Updates_Manager(
        "wpadverts-restricted-categories/wpadverts-restricted-categories.php",
        "wpadverts-restricted-categories",
        "1.0.1"
    );
    $manager->connect();

    // Save field
    add_action( 'edited_advert_category', 'wpadverts_restricted_categories_save_category');
    add_action( 'create_advert_category', 'wpadverts_restricted_categories_save_category');

    // Add field to form
    add_action( 'advert_category_edit_form_fields', 'wpadverts_restricted_categories_category_form_fields_edit');
    add_action( 'advert_category_add_form_fields', 'wpadverts_restricted_categories_category_form_fields');

    // Change columns
    add_filter( 'manage_edit-advert_category_columns', 'wpadverts_restricted_categories_edit_restricted_columns' );
    add_filter( 'manage_advert_category_custom_column', 'wpadverts_restricted_categories_manage_advert_category_column', 10, 3 );

    add_filter('list_cats', 'wpadverts_restricted_categories_admin_form', 10, 2);

    include_once plugin_dir_path( __FILE__ ) . "/includes/admin-pages.php";
}

/**
 * Init Restricted Categoriess frontend filters and actions
 *
 * @since 1.0
 * @return void
 */
function adext_restricted_categories_init_frontend() {
    if( ! defined( "ADVERTS_PATH" ) ) {
        return;
    }

    add_filter('list_cats', 'wpadverts_restricted_categories_frontend_form', 10, 2);
}


/**
 * Feature disables the ability to choose restricted category in frontend
 *
 * @param string $name - category name
 * @param object $category - category object
 * @return filter
 */
function wpadverts_restricted_categories_frontend_form($name, $category = "") {

    if(is_object($category)) {
        $is_restricted = get_term_meta($category->term_id, "wpadvert_is_restricted", true);
    }

    if($is_restricted) {
        ?>
            <script type="text/javascript">
                jQuery(window).load(function() {
                    if(typeof jQuery("input[name='advert_category[]'][value='<?php echo $category->term_id; ?>']") !== 'undefined') {
                        jQuery("input[name='advert_category[]'][value='<?php echo $category->term_id; ?>']").prop('disabled', true);
                    }

                    if(typeof jQuery("#advert_category") !== 'undefined') {
                        jQuery("#advert_category option[value='<?php echo $category->term_id; ?>']").prop('disabled', true);

                        jQuery("#advert_category option").each(function() {
                            if(jQuery(this).is(":enabled")) {
                                //jQuery(this).attr('selected', 'selected');
                                return false;
                            } else {
                                jQuery(this).attr('selected', false);
                            }
                        });
                    }
                });
            </script>
        <?php
    }

    return apply_filters( 'wpadverts_restricted_categories_frontend_form', $name, $category );
}
