<?php
add_action('admin_menu', 'fm_init_download_subscribers_menu');

function fm_init_download_subscribers_menu(){
    $menu = add_menu_page(
      'Descargar Suscriptores', 
      'Descargar Suscriptores', 
      'administrator', 
      'download-subscribers', 
      'fm_init_download_subscribers'
    );

    if ( $menu ){
      add_action( "load-$menu", 'fm_start_download_subscribers' );
    }
    
}

function fm_init_download_subscribers(){
  CDController::init_controller('admin', array('template' => 'download_subscribers'));
}

function fm_download_subscribers_assets() {
  wp_enqueue_style('download-subscribers-styles', get_template_directory_uri().'/download-subscribers/assets/css/app.css');
  wp_enqueue_script( 'download-subscribers-fa', 'https://use.fontawesome.com/releases/v5.0.8/js/all.js', array(), '20180313', true );
  wp_enqueue_script( 'download-subscribers-main', get_template_directory_uri() . '/download-subscribers/assets//js/main.js', array('jquery'), '20180313', true );
}

add_action('admin_enqueue_scripts', 'fm_download_subscribers_assets');

function fm_start_download_subscribers(){
  if(isset($_POST['download'])){
    $download_subscribers = new AdminDownloadSubscribers();
    $download_subscribers->download_xlsx();
  }
}
