<?php

/**
*
*  Custom settings page
* * * * * * * * * * * * * * * * * * /
*
* @internal never define functions inside callbacks.
* these functions could be run multiple times; this would result in a fatal error.
* https://developer.wordpress.org/plugins/settings/custom-settings-page/
*/

// if( ! function_exists('') ){}

/**
* custom option and settings
*/
if( ! function_exists('hangar_wpplugin_settings_init') ){
  function hangar_wpplugin_settings_init() {
    // register a new setting for "hangar_wpplugin" page
    register_setting( 'hangar_wpplugin_settings', 'hangar_wpplugin_options' );

    // register a new section in the "hangar_wpplugin" page
    add_settings_section(
      'hangar_wpplugin_section_control_home',
      __( 'Home control.', 'hangar-wpplugin-textdomain' ),
      'hangar_wpplugin_section_control_home_cb',
      'hangar_wpplugin_settings'
    );


    // register a new field in the "hangar_wpplugin_section_control_home" section, inside the "hangar_wpplugin" page
    //
    // uncoment below to activate
    // add_settings_field(
    //   'hangar_wpplugin_field_control_home', // as of WP 4.6 this value is used only internally
    //   // use $args' label_for to populate the id inside the callback
    //   __( 'This is the content being shown in home page:', 'hangar-wpplugin-textdomain' ),
    //   'hangar_wpplugin_field_control_home_cb',
    //   'hangar_wpplugin_settings',
    //   'hangar_wpplugin_section_control_home'
    // );

    // register a new section in the "hangar_wpplugin" page
    //
    // uncoment below to activate

    // add_settings_section(
    //   'hangar_wpplugin_section_olderthandate',
    //   __( 'Older than options.', 'hangar-wpplugin-textdomain' ),
    //   'hangar_wpplugin_section_olderthandate_cb',
    //   'hangar_wpplugin_settings'
    // );


    // register a new field in the "hangar_wpplugin_section_olderthandate" section, inside the "hangar_wpplugin" page
    //
    // uncoment below to activate

    // add_settings_field(
    //   'hangar_wpplugin_field_olderthandate', // as of WP 4.6 this value is used only internally
    //   // use $args' label_for to populate the id inside the callback
    //   __( 'Hide content older than...', 'hangar-wpplugin-textdomain' ),
    //   'hangar_wpplugin_field_olderthandate_cb',
    //   'hangar_wpplugin_settings',
    //   'hangar_wpplugin_section_olderthandate',
    //   [
    //     'label_for' => 'hangar_wpplugin_field_olderthandate',
    //     'class' => 'hangar-wpplugin-row',
    //     'hangar_wpplugin_custom_data' => 'custom',
    //   ]
    // );
  }
}


/**
* register our hangar_wpplugin_settings_init to the admin_init action hook
*/
add_action( 'admin_init', 'hangar_wpplugin_settings_init' );

/**
* custom option and settings:
* callback functions
*/


// home control panel cb

// to see wich pages and post are shown in home page
if( ! function_exists('hangar_wpplugin_section_control_home_cb') ){
  function hangar_wpplugin_section_control_home_cb(){

    global $wpdb;
    $querystr = "
    SELECT wposts.post_name,wposts.ID, users.user_login, m1.meta_value as inicio, m2.meta_value as orden
    FROM $wpdb->posts wposts
    INNER JOIN $wpdb->postmeta m1 ON wposts.ID = m1.post_id
    INNER JOIN $wpdb->postmeta m2 ON wposts.ID = m2.post_id
    INNER JOIN $wpdb->users users ON wposts.post_author = users.ID
    WHERE (wposts.post_type = 'post' OR wposts.post_type = 'page')
    AND m1.meta_key = 'Inici'
    AND m1.meta_value = '1'
    AND m2.meta_key = 'Ordre_inici'
    GROUP BY ID
    ORDER BY inicio DESC, CAST(orden AS SIGNED) ASC
    ";

    echo' <div id="wpbody-content"> ';
    // title controlled in settings_section
    // echo '<div class="wrap"><h2>Control Home</h2><br/></div>';

    $pageposts = $wpdb->get_results($querystr, OBJECT);
    if ($pageposts):
      global $post;


      echo "<table class=\"wp-list-table widefat fixed posts\" cellspacing=\"0\">
      <thead>
      <tr>
      <th scope='col' width='150' class='manage-column column-slug sortable desc'>Nombre Post</th>
      <th scope='col' class='manage-column column-slug sortable desc'>Id</th>
      <th scope='col' class='manage-column column-slug sortable desc'>Autor Post</th>
      <th scope='col' class='manage-column column-slug sortable desc'>Inicio</th>
      <th scope='col' class='manage-column column-slug sortable desc'>Orden</th>
      </tr>
      </thead>
      <tbody id=\"the-list\">";

      foreach ($pageposts as $post):
        $nom = $post->post_name;
        $id = $post->ID;
        $autor = $post->user_login;
        $inici = $post->inicio;
        $ordre = $post->orden;
        echo "<tr>
        <td><a href='post.php?post=$id&action=edit' class=\"row-tittle\">$nom</a></td>
        <td>$id</td>
        <td>$autor</td>
        <td>$inici</td>
        <td>$ordre</td>
        </tr>";
      endforeach;
      echo "</tbody></table></div>";
    endif;

  }
}



// olderthandate section cb

// section callbacks can accept an $args parameter, which is an array.
// $args have the following keys defined: title, id, callback.
// the values are defined at the add_settings_section() function.
if( ! function_exists('hangar_wpplugin_section_olderthandate_cb') ){
  function hangar_wpplugin_section_olderthandate_cb( $args ) {
    ?>
    <p id="<?php echo esc_attr( $args['id'] ); ?>">
      <?php esc_html_e( 'Here you should set the options for the older content to be marked as it.', 'hangar-wpplugin-textdomain' ); ?>
    </p>
    <?php
  }
}

// daysback field cb

// field callbacks can accept an $args parameter, which is an array.
// $args is defined at the add_settings_field() function.
// wordpress has magic interaction with the following keys: label_for, class.
// the "label_for" key value is used for the "for" attribute of the <label>.
// the "class" key value is used for the "class" attribute of the <tr> containing the field.
// you can add custom key value pairs to be used inside your callbacks.
if( ! function_exists('hangar_wpplugin_field_olderthandate_cb') ){
  function hangar_wpplugin_field_olderthandate_cb( $args ) {
    // get the value of the setting we've registered with register_setting()
    $options = get_option( 'hangar_wpplugin_options' );
    // output the field
    ?>

    <p class="description">
      <?php _e( 'You should set up the <b>number of days</b> from today to the past or the date from wich will be <b>considered old posts</b>. Then the class <span style="font-family:monospace;">oldpost</span> will be added to the post and pages so you can apply a css style to your theme.', 'hangar-wpplugin-textdomain' ); ?>
    </p>
    <form id="hangar_wpplugin-oldpost-numberofdays" class="hangar_wpplugin-oldpost-numberofdays">
      <label for="hangar_wpplugin-oldpost-numberofdays">Number of days:</label>
      <input type="number" id="hangar_wpplugin-oldpost-numberofdays" name="hangar_wpplugin-oldpost-numberofdays" min="1" max="100">
    </form>
    <form id="hangar_wpplugin-oldpost-enddate" class="hangar_wpplugin-oldpost-enddate">
      <label for="hangar_wpplugin-oldpost-enddate">Start date:</label>
      <input type="date" id="hangar_wpplugin-oldpost-enddate" name="hangar_wpplugin-oldpost-endate" value="">
    </form>

    <!-- borrar desde aqui -->
<!--
    <p>
    label_for:
    <?php //echo esc_attr( $args['label_for'] ); ?>
    <br />
    hangar_wpplugin_custom_data:
    <?php //echo esc_attr( $args['hangar_wpplugin_custom_data'] ); ?>
    <br />
    class:
    <?php //echo esc_attr( $args['class'] ); ?>
    <br />
    <?php //esc_html_e( 'daysback', 'hangar-wpplugin-textdomain' ); ?>
    <br />
  </p>
-->
<!-- borrar hasta aqui -->

<?php
}
}

/**
* top level menu
* https://developer.wordpress.org/reference/functions/add_menu_page/
*/
if( ! function_exists('hangar_wpplugin_options_page') ){
  function hangar_wpplugin_options_page() {
    // add top level menu page
    add_menu_page(
      $page_title = 'Hangar plugin options', // $page_title
      $menu_title = 'Hangar Options', //$menu_title
      $capability = 'manage_options', //'edit_others_posts', // $capability
      $menu_slug  = 'hangar-options', // $menu_slug
      $function   = 'hangar_wpplugin_options_page_html', //$function
      $icon_url   = 'dashicons-rest-api',// $icon_url //https://developer.wordpress.org/resource/dashicons/#menu
      $position = '25'// $position
    );
  }
}
// register our hangar_wpplugin_options_page to the admin_menu action hook
add_action( 'admin_menu', 'hangar_wpplugin_options_page' );

// top level menu: callback functions
if( ! function_exists('hangar_wpplugin_options_page_html') ){
  function hangar_wpplugin_options_page_html() {
    // check user capabilities
    if ( ! current_user_can( 'manage_options' ) ) {
      ?>
      <p class="description">
        <?php esc_html_e( 'Sorry, you do not have permission to edit this settings, please, contact any admin to get granted.', 'hangar-wpplugin-textdomain' ); ?>
      </p>
      <?php
      return;
    }

    // add error/update messages

    // check if the user have submitted the settings
    // wordpress will add the "settings-updated" $_GET parameter to the url
    if ( isset( $_GET['settings-updated'] ) ) {
      // add settings saved message with the class of "updated"
      add_settings_error( 'hangar_wpplugin_messages', 'hangar_wpplugin_message', __( 'Settings Saved', 'hangar-wpplugin-textdomain' ), 'updated' );
    }

    // show error/update messages
    settings_errors( 'hangar_wpplugin_messages' );
    ?>
    <div class="wrap">
      <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
      <form action="options.php" method="post">
        <?php
        // output security fields for the registered setting "hangar_wpplugin"
        settings_fields( 'hangar_wpplugin_settings' );
        // output setting sections and their fields
        // (sections are registered for "hangar_wpplugin", each field is registered to a specific section)
        do_settings_sections( 'hangar_wpplugin_settings' );
        // output save settings button
        submit_button( 'Save Settings' );
        ?>
      </form>
    </div>
    <?php
  }
}