<?php

/*
Plugin Name: The Green Balloon - website options
Plugin URI: https://thegreenBalloon.net/
Description: All the <strong>options</strong> for <strong>The Green Balloon website</strong> wich do not come by default with wordpress such as: selector for showing post or pages in homepage, the text area to show summary for post and pages, the map archive showing tags from posts.
Author: jorge - vitrubio.net
Date: 2020 01 10
Version: 0.3
Author URI: https://vitrubio.net/
License: GPL 3.0
License URI:https://www.gnu.org/licenses/gpl-3.0.html
*/


/* to use in the template
*  include this code in post/page view
*
//   // Detect plugin. For use on Front End only.
//   include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
//   // check for plugin using plugin name
//   if ( is_plugin_active( 'hangar-wp-plugin/hangar-wp-plugin.php' ) ) {
//     //plugin is activated do
//     hangar_show_extra_content();
//   }
*/

/*
if ever read this never forget to check
howto write a pluggin by Wordpress.org
https://codex.wordpress.org/Writing_a_Plugin
and the best practices
https://developer.wordpress.org/plugins/plugin-basics/best-practices/
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );


/* * * * * * * * * * * * * * * * * * * * * * * * * * */
/*   begin pluggin for SHORT CODES IN PAGE OR POST  */
/*
 https://themefoundation.com/wordpress-meta-boxes-guide/
*/


// Adds a meta box to the post editing screen
if ( ! function_exists( 'hangar_extra_content_add_meta_box' ) ) :
  function hangar_extra_content_add_meta_box() {
    add_meta_box(
      'hangar_extra_content', //$id
      'Hangar: contingut extra', //$title
      'hangar_extra_content_callback', //$callback
      array('post', 'page'), // $screen
      'normal', // $context // normal,side,advanced
      'high' // $priority //high, core, default,low
    );
  }
  add_action( 'add_meta_boxes', 'hangar_extra_content_add_meta_box');
endif;

// Outputs the content of the meta box
if ( ! function_exists( 'hangar_extra_content_callback' ) ) :
  function hangar_extra_content_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'hangar_extra_content_nonce' );
    $hangar_extra_content_stored_meta = get_post_meta( $post->ID );
    ?>
    <p>
      <span class="row hangar-title"><?php _e( 'Estas son las opciones de cada <em>entrada</em> o <em>página</em> de la web.', 'hangar-wpplugin-textdomain' )?></span>
      <span class="row hangar-title"><?php _e( 'Para mostrar las entradas o páginas debes poner el <code>[shortcode]</code> que genera el icono <img src="' . HANGAR_WPPLUGIN_URL . 'assets/img/PFC-icon.png" alt="PFC icon" style="margin-bottom:-8px;"/> que ves en la barra de edición de texto.', 'hangar-wpplugin-textdomain' )?></span>
      <div class="row hangar-content">
      </div>
    </p>
    <p>
      <label for="hangar_extra_content-summary" class="hangar_extra_content-summary-title"><?php _e( 'Escribe el texto que se muestra como resumen en la página principal y en los cuadrados.', 'hangar-wpplugin-textdomain' )?></label>
      <div id="hangar_extra_content_wysiwyg_summary">
        <?php
        // https://codex.wordpress.org/Function_Reference/wp_editor
        $content = get_post_meta( get_the_ID(), 'hangar_extra_content_wysiwyg_summary', 'edit' );
        $editor_id = 'hangar_extra_content_wysiwygsummary_ID';
        $settings = array(
          'textarea_name' =>  'hangar_extra_content_wysiwyg_summary',
          'media_buttons' =>  true,
          'textarea_rows' =>  10,
          // https://www.kevinleary.net/customizing-tinymce-wysiwyg-editor-wordpress/
          // 'tinymce'       =>  array(
          //   'toolbar1'      => 'formatselect,bold,italic,link,unlink,charmap,removeformat,wp_more',
          //   'toolbar2'      => '',
          //   'toolbar3'      => '',
          // ),
        );
        wp_editor( $content,$editor_id,$settings); ?>
      </div>
    </p>
    <?php
  }
endif;

// Saves the custom meta input
if ( ! function_exists( 'hangar_extra_content_meta_save' ) ) :
  function hangar_extra_content_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'hangar_extra_content_nonce' ] ) && wp_verify_nonce( $_POST[ 'hangar_extra_content_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
      return;
    }

    // saves the hangar_extra_content_wysiwyg_summary
    if( isset( $_POST[ 'hangar_extra_content_wysiwyg_summary' ] ) ) {
      $dattaout=($_POST['hangar_extra_content_wysiwyg_summary']);
      update_post_meta( $post_id, 'hangar_extra_content_wysiwyg_summary', $dattaout );
    }else {
      update_post_meta( $post_id, 'hangar_extra_content_wysiwyg_summary', $_POST[ '' ] );
    }
  }
  add_action( 'save_post', 'hangar_extra_content_meta_save' );
endif;


// outputs this archive_map in front end
if ( ! function_exists( 'hangar_show_extra_content' ) ) :
	function hangar_show_extra_content(){
		// Detect plugin. For use on Front End only.
		include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
		// check for plugin using plugin name
    // https://wordpress.org/plugins/posts-from-category/
		if ( is_plugin_active( 'posts-from-category/post-from-category.php' ) ) {

      // Retrieves the stored value from the database
      $meta_value = get_post_meta( get_the_ID(), 'hangar_extra_content_wysiwyg_summary', true );
      // Checks and displays the retrieved value
      if( !empty( $meta_value ) ) {
      	echo '<div class="hangar-extra-content">';
        // to do shortcodes: https://developer.wordpress.org/reference/functions/apply_filters/
      	echo apply_filters( 'the_content', $meta_value);
      	echo '</div>';
      }
    }

    } // end function hangar_show_extra_content
  endif;
/* * * * * * * *


/* * * * * * * * *

/*   end pluggin for SHORT CODES IN PAGE OR POST  */
/* * * * * * * * * * * * * * * * * * * * * * * * */