Advertisement
aitormendez

Untitled

Feb 14th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2.    /*
  3.    Plugin Name: telmo timeline
  4.    Plugin URI:
  5.    Description: escribe json
  6.    Version: 1.0
  7.    Author: Aitor Méndez
  8.    Author URI: https//e451.net
  9.    License: WTFPL – Do What the Fuck You Want to Public License
  10.    */
  11. ?>
  12. <?php
  13.  
  14. function create_timeline_json() {
  15.  
  16.     $posts_tl = get_posts(array(
  17.       'post_type' => array( 'post' ),
  18.       'posts_per_page' => '-1',
  19.       'post_status' => 'publish',
  20.     ));
  21.  
  22.  
  23.     $timeline = array(
  24.       'events' => [],
  25.     );
  26.  
  27.  
  28.     foreach ($posts_tl as $post) {
  29.       $fecha_ok = new DateTime(get_field('fecha_conversacion', $post->ID, false));
  30.       $anio = date_format($fecha_ok,"Y");
  31.       $mes = date_format($fecha_ok,"m");
  32.       $dia = date_format($fecha_ok,"d");
  33.       $titulo = get_the_title($post->ID);
  34.       $img_url = get_the_post_thumbnail_url($post->ID, 'merz-image');
  35.  
  36.  
  37.       $media = array(
  38.         "url" => $img_url,
  39.         "caption" => " ",
  40.         "credit" => " ",
  41.       );
  42.  
  43.       $start_date = array(
  44.         "month" => $mes,
  45.         "day"   => $dia,
  46.         "year"  => $anio,
  47.       );
  48.  
  49.       $text = array(
  50.         "headline" => $titulo,
  51.       );
  52.  
  53.       $evento = array(
  54.         "media" => $media,
  55.         "start_date" => $start_date,
  56.         "text" => $text,
  57.       );
  58.  
  59.       $timeline[events][] = $evento;
  60.     }
  61.     wp_reset_postdata();
  62.  
  63.     $posts_tl = get_posts(array(
  64.       'post_type' => array( 'dialogos' ),
  65.       'posts_per_page' => '-1',
  66.       'post_status' => 'publish',
  67.     ));
  68.  
  69.     foreach ($posts_tl as $post) {
  70.       $fecha_ok = new DateTime(get_field('fecha_conversacion', $post->ID, false));
  71.       $anio = date_format($fecha_ok,"Y");
  72.       $mes = date_format($fecha_ok,"m");
  73.       $dia = date_format($fecha_ok,"d");
  74.       $titulo = get_the_title($post->ID);
  75.       $texto = '<p>'.$post->post_content.'<p>';
  76.  
  77.       $start_date = array(
  78.         "month" => $mes,
  79.         "day"   => $dia,
  80.         "year"  => $anio,
  81.       );
  82.  
  83.       $text = array(
  84.         "headline" => $titulo,
  85.         "text" => $texto,
  86.       );
  87.  
  88.       $evento = array(
  89.         "start_date" => $start_date,
  90.         "text" => $text,
  91.       );
  92.  
  93.       $timeline[events][] = $evento;
  94.     }
  95.     wp_reset_postdata();
  96.  
  97.     $output =json_encode($timeline);
  98.     $output_1 = str_replace("\\/","/",$output);
  99.     $output_2 = str_replace("\\n\\n","</p><p>",$output_1);
  100.     $output_3 = str_replace("\\n","<br/>",$output_2);
  101.  
  102.  
  103.     $fp = fopen( ABSPATH . '../app/themes/sage/timeline.json', 'w' );
  104.     fwrite( $fp, $output_3 );
  105.     fclose( $fp );
  106.  
  107. }
  108. add_action('save_post', __NAMESPACE__ . '\\create_timeline_json');
  109.  
  110. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement