<?php
/**
 * Media buttons for filter plus
 * Comes with WooCommerce filter plus as sub package
 */

add_action('admin_head', 'nfpfilter_add_my_tc_button');
function nfpfilter_add_my_tc_button() {
    global $typenow;
    // check user permissions
    if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) {
        return;
    }
    // verify the post type
    if( ! in_array( $typenow, array( 'post', 'page' ) ) )
        return;
    // check if WYSIWYG is enabled
    if ( get_user_option('rich_editing') == 'true') {

        wp_register_script('nfpfilterscript', plugins_url( '/js/media-button.js', __FILE__ ));

        global $wp_registered_sidebars;
        $curr_sidebar = '';
        if(is_array($wp_registered_sidebars)){

            foreach($wp_registered_sidebars as $wp_registered_sidebar){
                $curr_sidebar .= $wp_registered_sidebar['name'].'_nfpfilter_'.$wp_registered_sidebar['id'].',';
            }
        }

        $params = array(
            'sidebars' => $curr_sidebar,
        );
        wp_localize_script( 'nfpfilterscript','nfpfilerVars', $params );


        add_filter("mce_external_plugins", "nfpfilter_add_tinymce_plugin");
        add_filter('mce_buttons', 'nfpfilter_register_my_tc_button');
        wp_enqueue_script('nfpfilterscript');


    }
}


function nfpfilter_add_tinymce_plugin($plugin_array) {
    $plugin_array['nfpfilter_tc_button'] = plugins_url( '/js/media-button.js', __FILE__ );
    return $plugin_array;
}

function nfpfilter_register_my_tc_button($buttons) {
    array_push($buttons, "nfpfilter_tc_button");
    return $buttons;
}
