<?php
/**
 * Theme custom metaboxes
 **/

// Use "Featured Image" box as a custom box for parralax image
function pages_image_box() {

    remove_meta_box('postimagediv', 'page', 'side');
    
    add_meta_box('postimagediv', __('Parallax background for this section (use large image for good results, 1600px+ width recommended)', 'inspiredise'), 'post_thumbnail_meta_box', 'page', 'normal', 'high');

}
add_action('do_meta_boxes', 'pages_image_box');

// Custom metabox for pages title
function pages_settings_box() {

    $screens = array( 'page' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            'pages_settings_box',
            __( 'Page settings', 'inspiredise' ),
            'pages_settings_inner_box',
            $screen,
            'normal',
            'high'
        );
    }
}
add_action( 'add_meta_boxes', 'pages_settings_box' );

function pages_settings_inner_box( $post ) {

  // Add an nonce field so we can check for it later.
  wp_nonce_field( 'pages_settings_inner_box', 'pages_settings_inner_box_nonce' );

  /*
   * Use get_post_meta() to retrieve an existing value
   * from the database and use the value for the form.
   */
  $value_page_subtitle = get_post_meta( $post->ID, '_page_subtitle_value', true );
  $value_page_class = get_post_meta( $post->ID, '_page_class_value', true );
  $value_page_openinnewwindow = get_post_meta( $post->ID, '_page_openinnewwindow_value', true );

  echo '<p><label for="page_subtitle" style="width: 130px; display:inline-block;">';
       _e( "Page subtitle: ", 'inspiredise' );
  echo '</label> ';
  echo '<input type="text" id="page_subtitle" name="page_subtitle" value="' . esc_attr( $value_page_subtitle ) . '" size="100%" /></p>';
  echo '<label for="page_class" style="width: 130px; display:inline-block;">';
       _e( "Page CSS class: ", 'inspiredise' );
  echo '</label> ';
  echo '<input type="text" id="page_class" name="page_class" value="' . esc_attr( $value_page_class ) . '" size="100%" />';
  $checked = '';
  if( $value_page_openinnewwindow == true ) { 
    $checked = 'checked = "checked"';
  }
  echo '<p><input type="checkbox" id="page_openinnewwindow" name="page_openinnewwindow" '.$checked.' /> <label for="page_openinnewwindow">'.__( "Open this page link in menu in new window (disable page from OnePage display)", 'inspiredise' ).'</label></p>';

}

function page_settings_save_postdata( $post_id ) {

  /*
   * We need to verify this came from the our screen and with proper authorization,
   * because save_post can be triggered at other times.
   */

  // Check if our nonce is set.
  if ( ! isset( $_POST['pages_settings_inner_box_nonce'] ) )
    return $post_id;

  $nonce = $_POST['pages_settings_inner_box_nonce'];

  // Verify that the nonce is valid.
  if ( ! wp_verify_nonce( $nonce, 'pages_settings_inner_box' ) )
      return $post_id;

  // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return $post_id;

  // Check the user's permissions.
  if ( 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;
  
  } else {

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  /* OK, its safe for us to save the data now. */

  // Sanitize user input.
  $mydata = sanitize_text_field( $_POST['page_subtitle'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, '_page_subtitle_value', $mydata );

  // Sanitize user input.
  $mydata = sanitize_text_field( $_POST['page_class'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, '_page_class_value', $mydata );

  // Sanitize user input.
  if(!isset($_POST['page_openinnewwindow'])) {
    $_POST['page_openinnewwindow'] = false;
  }
  $mydata = sanitize_text_field( $_POST['page_openinnewwindow'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, '_page_openinnewwindow_value', $mydata );
}
add_action( 'save_post', 'page_settings_save_postdata' );


function nspiredise_image_cpt(){
    $cpts = array('portfolio');
    return $cpts;
}
add_filter('images_cpt','nspiredise_image_cpt');

