<?php
if( function_exists('acf_add_options_page') ) {

	$option_page = acf_add_options_page(array(
		'page_title' 	=> 'Opciones del Sitio',
		'menu_title' 	=> 'Opciones del Sitio',
		'menu_slug' 	=> 'opciones-sitio',
		'capability' 	=> 'edit_posts',
		'redirect' 	=> false
	));

  $notification_page = acf_add_options_page(array(
		'page_title' 	=> 'Archivo de Revistas',
		'menu_title' 	=> 'Archivo de Revistas',
		'menu_slug' 	=> 'magazine_archive',
		'capability' 	=> 'edit_posts',
    'parent_slug' => 'edit.php?post_type=fm_magazine',
		'redirect' 	=> false
	));

	$not_found_page = acf_add_options_page(array(
		'page_title' 	=> '404',
		'menu_title' 	=> '404',
		'menu_slug' 	=> '404_page',
		'capability' 	=> 'edit_posts',
    'parent_slug' => 'edit.php?post_type=page',
		'redirect' 	=> false
	));

  $modules_page = acf_add_options_page(array(
		'page_title' 	=> 'Módulos',
		'menu_title' 	=> 'Módulos',
		'menu_slug' 	=> 'modules',
		'capability' 	=> 'edit_posts',
    'parent_slug' => 'edit.php?post_type=page',
		'redirect' 	=> false
	));
}

add_filter( 'timber_context', 'cw_add_to_context' );

function cw_add_to_context($data){
  $data['site_options']['logout_fm_url'] =  wp_logout_url( get_permalink(6) );
	$data['site_options']['default_images'] =  get_field('option_default_images','option');
	$data['site_options']['contact_data'] =  get_field('option_contact_data','option');
	$data['site_options']['current_color'] =  get_field('option_current_color','option');
	$data['site_options']['social_media'] =  get_field('option_social_media','option');
  return $data;
}


function cw_magazine_archive_query( $query ) {
    if (  $query->is_main_query() && $query->post_type = 'fm_magazine' && is_archive() && !is_admin() ) {
      $query->set( 'posts_per_page', 12 );
      $query->set( 'post_parent', 0 );
    }
}
add_filter( 'pre_get_posts', 'cw_magazine_archive_query' );

function cw_get_last_magazine(){

  $args = array(
    'post_type' => 'fm_magazine',
    'post_parent' => 0,
    'posts_per_page' => 1,
    'orderby' => 'date',
    'order' => 'DESC'
  );


  $last_post_query = Timber::get_posts($args);

  if($last_post_query){
    return $last_post_query[0];
  }else{
    return false;
  }
}

add_action('after_setup_theme', 'remove_admin_bar');
	function remove_admin_bar() {
	if (!( current_user_can('administrator') || current_user_can('editor') ) && !is_admin()) {
	  show_admin_bar(false);
	}
}

require get_template_directory() . '/download-subscribers/download-subscribers.php';
