<?php


/**
 * Custom settings page
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @link       https://oficinasuport.xarxaprod.cat
 * @since      1.0.0
 * @package    ofisuport-wp-plugin 
 * @subpackage ofisuport-wp-plugin/includes
 * @author     Jorge - vitrubio.net <jorge@vitrubio.net>
 *
 * @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/
 */


/**
* custom option and settings
*/
if( ! function_exists('ofisuport_wpplugin_settings_init') ){
  function ofisuport_wpplugin_settings_init() {
    // register a new setting for "ofisuport_wpplugin" page
    register_setting( 'ofisuport_wpplugin_settings', 'ofisuport_wpplugin_options' );

    // register a new section in the "ofisuport_wpplugin" page
    add_settings_section(
      'ofisuport_wpplugin_section_control_home',
      __( 'Home control.', 'ofisuport-wpplugin-textdomain' ),
      'ofisuport_wpplugin_section_control_home_cb',
      'ofisuport_wpplugin_settings'
    );


    // register a new field in the "ofisuport_wpplugin_section_control_home" section, inside the "ofisuport_wpplugin" page
    //
    // uncoment below to activate
    // add_settings_field(
    //   'ofisuport_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:', 'ofisuport-wpplugin-textdomain' ),
    //   'ofisuport_wpplugin_field_control_home_cb',
    //   'ofisuport_wpplugin_settings',
    //   'ofisuport_wpplugin_section_control_home'
    // );

    // register a new section in the "ofisuport_wpplugin" page
    add_settings_section(
      'ofisuport_wpplugin_section_reusableblocks',
      __( 'Reusable blocks.', 'ofisuport-wpplugin-textdomain' ),
      'ofisuport_wpplugin_section_reusableblocks_cb',
      'ofisuport_wpplugin_reusableblocks'
    );
    // register a new field in the "ofisuport_wpplugin_section_reusableblocks" section, inside the "ofisuport_wpplugin" page
    //
    // uncoment below to activate
     add_settings_field(
       'ofisuport_wpplugin_field_reusableblocks', // as of WP 4.6 this value is used only internally
       // use $args' label_for to populate the id inside the callback
       __( 'Follow this link to edit the reusable blocks <a href="' . admin_url() . 'edit.php?post_type=wp_block" class="but.ton"> Reusable blocks</a>', 'ofisuport-wpplugin-textdomain' ),
       'ofisuport_wpplugin_field_reusableblocks_cb',
       'ofisuport_wpplugin_settings',
       'ofisuport_wpplugin_section_reusableblocks',

        // edit.php?post_type=wp_block
     );
    
    
    // register a new section in the "ofisuport_wpplugin" page
    //
    // uncoment below to activate

    // add_settings_section(
    //   'ofisuport_wpplugin_section_olderthandate',
    //   __( 'Older than options.', 'ofisuport-wpplugin-textdomain' ),
    //   'ofisuport_wpplugin_section_olderthandate_cb',
    //   'ofisuport_wpplugin_settings'
    // );


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

    // add_settings_field(
    //   'ofisuport_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...', 'ofisuport-wpplugin-textdomain' ),
    //   'ofisuport_wpplugin_field_olderthandate_cb',
    //   'ofisuport_wpplugin_settings',
    //   'ofisuport_wpplugin_section_olderthandate',
    //   [
    //     'label_for' => 'ofisuport_wpplugin_field_olderthandate',
    //     'class' => 'ofisuport-wpplugin-row',
    //     'ofisuport_wpplugin_custom_data' => 'custom',
    //   ]
    // );
  }
}


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

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





// 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('ofisuport_wpplugin_section_olderthandate_cb') ){
  function ofisuport_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.', 'ofisuport-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('ofisuport_wpplugin_field_olderthandate_cb') ){
  function ofisuport_wpplugin_field_olderthandate_cb( $args ) {
    // get the value of the setting we've registered with register_setting()
    $options = get_option( 'ofisuport_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.', 'ofisuport-wpplugin-textdomain' ); ?>
    </p>
    <form id="ofisuport_wpplugin-oldpost-numberofdays" class="ofisuport_wpplugin-oldpost-numberofdays">
      <label for="ofisuport_wpplugin-oldpost-numberofdays">Number of days:</label>
      <input type="number" id="ofisuport_wpplugin-oldpost-numberofdays" name="ofisuport_wpplugin-oldpost-numberofdays" min="1" max="100">
    </form>
    <form id="ofisuport_wpplugin-oldpost-enddate" class="ofisuport_wpplugin-oldpost-enddate">
      <label for="ofisuport_wpplugin-oldpost-enddate">Start date:</label>
      <input type="date" id="ofisuport_wpplugin-oldpost-enddate" name="ofisuport_wpplugin-oldpost-endate" value="">
    </form>

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

<?php
}
}

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

// top level menu: callback functions
if( ! function_exists('ofisuport_wpplugin_options_page_html') ){
  function ofisuport_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.', 'ofisuport-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( 'ofisuport_wpplugin_messages', 'ofisuport_wpplugin_message', __( 'Settings Saved', 'ofisuport-wpplugin-textdomain' ), 'updated' );
    }

    // show error/update messages
    settings_errors( 'ofisuport_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 "ofisuport_wpplugin"
        settings_fields( 'ofisuport_wpplugin_settings' );
        // output setting sections and their fields
        // (sections are registered for "ofisuport_wpplugin", each field is registered to a specific section)
        do_settings_sections( 'ofisuport_wpplugin_settings' );
        // output save settings button
        submit_button( 'Save Settings' );
        ?>
      </form>
    </div>
    <?php
  }
}