Created
November 21, 2020 10:43
-
-
Save cesjam7/4c16b73546e3e7504ea762c2984f58c4 to your computer and use it in GitHub Desktop.
Republicar posts con cron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /* CRON PARA PYMEX */ | |
add_action( 'init', 'cron_republicar_init' ); | |
function cron_republicar_init() { | |
register_post_type( | |
'cron-logs', array( | |
'labels' => array('name' => 'Cron Logs', 'singular_name' => 'Log'), | |
'public' => true, | |
'exclude_from_search' => true, | |
'show_in_admin_bar' => false, | |
'show_in_nav_menus' => false, | |
'publicly_queryable' => false, | |
'query_var' => false, | |
'supports' => array( 'title') | |
) | |
); | |
if (function_exists('acf_add_options_page') && function_exists('acf_add_local_field_group')) { | |
$parent = acf_add_options_page(array( | |
'page_title' => 'Configuración CRON', | |
'menu_title' => 'Configuración CRON', | |
'menu_slug' => 'configuracion-cron', | |
'redirect' => false | |
)); | |
acf_add_local_field_group(array( | |
'key' => 'group_5fb8e47df23e5', | |
'title' => 'Configuración CRON', | |
'fields' => array( | |
array( | |
'key' => 'field_5fb8e48ab2e36', | |
'label' => 'Categorías', | |
'name' => 'categorias', | |
'type' => 'taxonomy', | |
'instructions' => 'Si no eliges ninguno no se filtrará por categoría', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'taxonomy' => 'category', | |
'field_type' => 'multi_select', | |
'allow_null' => 0, | |
'add_term' => 0, | |
'save_terms' => 0, | |
'load_terms' => 0, | |
'return_format' => 'id', | |
'multiple' => 0, | |
), | |
array( | |
'key' => 'field_5fb8e4f1b2e37', | |
'label' => 'Usuarios', | |
'name' => 'usuarios', | |
'type' => 'user', | |
'instructions' => 'Si no eliges ninguno no se filtrará por usuario', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'role' => '', | |
'allow_null' => 0, | |
'multiple' => 1, | |
'return_format' => 'id', | |
), | |
array( | |
'key' => 'field_5fb8e539b2e38', | |
'label' => 'Horario Fecha', | |
'name' => 'horario_fecha', | |
'type' => 'checkbox', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'choices' => array( | |
1 => 'Lunes', | |
2 => 'Martes', | |
3 => 'Miércoles', | |
4 => 'Jueves', | |
5 => 'Viernes', | |
6 => 'Sábado', | |
7 => 'Domingo', | |
), | |
'allow_custom' => 0, | |
'default_value' => array( | |
), | |
'layout' => 'horizontal', | |
'toggle' => 0, | |
'return_format' => 'value', | |
'save_custom' => 0, | |
), | |
array( | |
'key' => 'field_5fb8e5a8b2e39', | |
'label' => 'Horario Desde', | |
'name' => 'horario_desde', | |
'type' => 'time_picker', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '50', | |
'class' => '', | |
'id' => '', | |
), | |
'display_format' => 'H:i:s', | |
'return_format' => 'Hi', | |
), | |
array( | |
'key' => 'field_5fb8e5ddb2e3a', | |
'label' => 'Horario Hasta', | |
'name' => 'horario_hasta', | |
'type' => 'time_picker', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '50', | |
'class' => '', | |
'id' => '', | |
), | |
'display_format' => 'H:i:s', | |
'return_format' => 'Hi', | |
), | |
), | |
'location' => array( | |
array( | |
array( | |
'param' => 'options_page', | |
'operator' => '==', | |
'value' => 'configuracion-cron', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'normal', | |
'style' => 'default', | |
'label_placement' => 'top', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
'active' => true, | |
'description' => '', | |
)); | |
} | |
} | |
add_action('cron_crear_post_cinco_minutos', 'crear_post_cinco_minutos'); | |
function crear_post_cinco_minutos() { | |
if (!function_exists('acf_add_options_page')) return; | |
$horario_fecha = get_field('horario_fecha', 'options'); | |
if (!in_array(date('N'), $horario_fecha)) return; | |
$horario_desde = intval(get_field('horario_desde', 'options')); | |
$horario_hasta = intval(get_field('horario_hasta', 'options')); | |
date_default_timezone_set('America/Lima'); | |
if (date('Gi') < $horario_desde || date('Gi') > $horario_hasta) return; | |
$args = array( | |
'post_status' => 'publish', | |
'orderby' => 'rand', | |
'showposts' => 1 | |
); | |
$categorias = get_field('categorias', 'options'); | |
if ($categorias) { | |
$args['tax_query'] = array( | |
'taxonomy' => 'category', | |
'operator' => 'IN', | |
'terms' => $categorias | |
); | |
} | |
$usuarios = get_field('usuarios', 'options'); | |
if ($categorias) { | |
$args['author__in'] = $usuarios; | |
} | |
//echo '<pre>';print_r($args); | |
//echo '</pre>'; | |
$random = new WP_Query($args); | |
while ($random->have_posts()) { $random->The_post(); | |
wp_update_post(array( | |
'ID' => get_the_id(), | |
'post_date' => date('Y-m-d H:i:s'), | |
'post_date_gmt' => date('Y-m-d H:i:s') | |
)); | |
wp_insert_post(array( | |
'post_title' => 'Se acaba de re-publicar el post: '.get_the_title(), | |
'post_type' => 'cron-logs' | |
)); | |
} | |
} | |
/* Fin de CRON */ ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment