Untitled diff

Created Diff never expires
9 removals
631 lines
9 additions
631 lines
<?php
<?php
/*
/*
Plugin Name: Custom Field Template
Plugin Name: Custom Field Template
Plugin URI: https://wpgogo.com/development/custom-field-template.html
Plugin URI: https://wpgogo.com/development/custom-field-template.html
Description: This plugin adds the default custom fields on the Write Post/Page.
Description: This plugin adds the default custom fields on the Write Post/Page.
Author: Hiroaki Miyashita
Author: Hiroaki Miyashita
Author URI: https://wpgogo.com/
Author URI: https://wpgogo.com/
Version: 2.4.4
Version: 2.4.4
Text Domain: custom-field-template
Text Domain: custom-field-template
Domain Path: /
Domain Path: /
*/
*/


/*
/*
This program is based on the rc:custom_field_gui plugin written by Joshua Sigar.
This program is based on the rc:custom_field_gui plugin written by Joshua Sigar.
I appreciate your efforts, Joshua.
I appreciate your efforts, Joshua.
*/
*/


/* Copyright 2008 -2018 Hiroaki Miyashita
/* Copyright 2008 -2018 Hiroaki Miyashita


This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.


This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
GNU General Public License for more details.


You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
*/


define( 'CFT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CFT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );

class custom_field_template {
class custom_field_template {
var $is_excerpt, $format_post_id;
var $is_excerpt, $format_post_id;
private $replace_val;
private $replace_val;


function __construct() {
function __construct() {
add_action( 'plugins_loaded', array( &$this, 'custom_field_template_plugins_loaded' ) );
add_action( 'plugins_loaded', array( &$this, 'custom_field_template_plugins_loaded' ) );
add_action( 'init', array( &$this, 'custom_field_template_init' ), 100 );
add_action( 'init', array( &$this, 'custom_field_template_init' ), 100 );
add_action( 'admin_init', array( &$this, 'custom_field_template_admin_init' ) );
add_action( 'admin_init', array( &$this, 'custom_field_template_admin_init' ) );
add_action( 'admin_menu', array( &$this, 'custom_field_template_admin_menu' ) );
add_action( 'admin_menu', array( &$this, 'custom_field_template_admin_menu' ) );
add_action( 'admin_print_scripts', array( &$this, 'custom_field_template_admin_scripts' ) );
add_action( 'admin_print_scripts', array( &$this, 'custom_field_template_admin_scripts' ) );
add_action( 'admin_head', array( &$this, 'custom_field_template_admin_head' ), 100 );
add_action( 'admin_head', array( &$this, 'custom_field_template_admin_head' ), 100 );
add_action( 'add_meta_boxes', array( &$this, 'custom_field_template_add_meta_boxes' ) );
add_action( 'add_meta_boxes', array( &$this, 'custom_field_template_add_meta_boxes' ) );
add_action( 'edit_form_advanced', array( &$this, 'custom_field_template_edit_form_advanced' ) );
add_action( 'edit_form_advanced', array( &$this, 'custom_field_template_edit_form_advanced' ) );
add_action( 'edit_page_form', array( &$this, 'custom_field_template_edit_form_advanced' ) );
add_action( 'edit_page_form', array( &$this, 'custom_field_template_edit_form_advanced' ) );


//add_action( 'edit_post', array(&$this, 'edit_meta_value'), 100 );
//add_action( 'edit_post', array(&$this, 'edit_meta_value'), 100 );
add_action( 'save_post', array( &$this, 'edit_meta_value' ), 100, 2 );
add_action( 'save_post', array( &$this, 'edit_meta_value' ), 100, 2 );
//add_action( 'publish_post', array(&$this, 'edit_meta_value'), 100 );
//add_action( 'publish_post', array(&$this, 'edit_meta_value'), 100 );


add_action( 'delete_post', array( &$this, 'custom_field_template_delete_post' ), 100 );
add_action( 'delete_post', array( &$this, 'custom_field_template_delete_post' ), 100 );


add_filter( 'media_send_to_editor', array( &$this, 'media_send_to_custom_field' ), 15 );
add_filter( 'media_send_to_editor', array( &$this, 'media_send_to_custom_field' ), 15 );
add_filter( 'plugin_action_links', array( &$this, 'wpaq_filter_plugin_actions' ), 100, 2 );
add_filter( 'plugin_action_links', array( &$this, 'wpaq_filter_plugin_actions' ), 100, 2 );


add_filter( 'get_the_excerpt', array( &$this, 'custom_field_template_get_the_excerpt' ), 1 );
add_filter( 'get_the_excerpt', array( &$this, 'custom_field_template_get_the_excerpt' ), 1 );
add_filter( 'the_content', array( &$this, 'custom_field_template_the_content' ) );
add_filter( 'the_content', array( &$this, 'custom_field_template_the_content' ) );
add_filter( 'the_content_rss', array( &$this, 'custom_field_template_the_content' ) );
add_filter( 'the_content_rss', array( &$this, 'custom_field_template_the_content' ) );


add_filter( 'attachment_fields_to_edit', array(
add_filter( 'attachment_fields_to_edit', array(
&$this,
&$this,
'custom_field_template_attachment_fields_to_edit'
'custom_field_template_attachment_fields_to_edit'
), 10, 2 );
), 10, 2 );
add_filter( '_wp_post_revision_fields', array( &$this, 'custom_field_template_wp_post_revision_fields' ), 1 );
add_filter( '_wp_post_revision_fields', array( &$this, 'custom_field_template_wp_post_revision_fields' ), 1 );
add_filter( 'edit_form_after_title', array( &$this, 'custom_field_template_edit_form_after_title' ) );
add_filter( 'edit_form_after_title', array( &$this, 'custom_field_template_edit_form_after_title' ) );


if ( isset( $_REQUEST['cftsearch_submit'] ) ) :
if ( isset( $_REQUEST['cftsearch_submit'] ) ) :
add_action( 'post_limits', array( &$this, 'custom_field_template_post_limits' ), 100 );
add_action( 'post_limits', array( &$this, 'custom_field_template_post_limits' ), 100 );
add_filter( 'posts_join', array( &$this, 'custom_field_template_posts_join' ), 100 );
add_filter( 'posts_join', array( &$this, 'custom_field_template_posts_join' ), 100 );
add_filter( 'posts_where', array( &$this, 'custom_field_template_posts_where' ), 100 );
add_filter( 'posts_where', array( &$this, 'custom_field_template_posts_where' ), 100 );
add_filter( 'posts_orderby', array( &$this, 'custom_field_template_posts_orderby' ), 100 );
add_filter( 'posts_orderby', array( &$this, 'custom_field_template_posts_orderby' ), 100 );
endif;
endif;


if ( function_exists( 'add_shortcode' ) ) :
if ( function_exists( 'add_shortcode' ) ) :
add_shortcode( 'cft', array( &$this, 'output_custom_field_values' ) );
add_shortcode( 'cft', array( &$this, 'output_custom_field_values' ) );
add_shortcode( 'cftsearch', array( &$this, 'search_custom_field_values' ) );
add_shortcode( 'cftsearch', array( &$this, 'search_custom_field_values' ) );
endif;
endif;


add_filter( 'get_post_metadata', array( &$this, 'get_preview_postmeta' ), 10, 4 );
add_filter( 'get_post_metadata', array( &$this, 'get_preview_postmeta' ), 10, 4 );
}
}


function custom_field_template_plugins_loaded() {
function custom_field_template_plugins_loaded() {
load_plugin_textdomain( 'custom-field-template', false, plugin_basename( dirname( __FILE__ ) ) );
load_plugin_textdomain( 'custom-field-template', false, plugin_basename( dirname( __FILE__ ) ) );
}
}


function custom_field_template_init() {
function custom_field_template_init() {
global $wp_version;
global $wp_version;
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();


if ( is_user_logged_in() && isset( $_REQUEST['post'] ) && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'custom-field-template/custom-field-template.php' && $_REQUEST['cft_mode'] == 'selectbox' ) {
if ( is_user_logged_in() && isset( $_REQUEST['post'] ) && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'custom-field-template/custom-field-template.php' && $_REQUEST['cft_mode'] == 'selectbox' ) {
echo $this->custom_field_template_selectbox();
echo $this->custom_field_template_selectbox();
exit();
exit();
}
}


if ( is_user_logged_in() && isset( $_REQUEST['post'] ) && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'custom-field-template/custom-field-template.php' && $_REQUEST['cft_mode'] == 'ajaxsave' ) {
if ( is_user_logged_in() && isset( $_REQUEST['post'] ) && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'custom-field-template/custom-field-template.php' && $_REQUEST['cft_mode'] == 'ajaxsave' ) {
if ( $_REQUEST['post'] > 0 ) {
if ( $_REQUEST['post'] > 0 ) {
$this->edit_meta_value( $_REQUEST['post'], '' );
$this->edit_meta_value( $_REQUEST['post'], '' );
}
}
exit();
exit();
}
}


if ( is_user_logged_in() && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'custom-field-template/custom-field-template.php' && $_REQUEST['cft_mode'] == 'ajaxload' ) {
if ( is_user_logged_in() && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'custom-field-template/custom-field-template.php' && $_REQUEST['cft_mode'] == 'ajaxload' ) {
if ( isset( $_REQUEST['id'] ) ) :
if ( isset( $_REQUEST['id'] ) ) :
$id = $_REQUEST['id'];
$id = $_REQUEST['id'];
elseif ( isset( $options['posts'][ $_REQUEST['post'] ] ) ) :
elseif ( isset( $options['posts'][ $_REQUEST['post'] ] ) ) :
$id = $options['posts'][ $_REQUEST['post'] ];
$id = $options['posts'][ $_REQUEST['post'] ];
else :
else :
$filtered_cfts = $this->custom_field_template_filter();
$filtered_cfts = $this->custom_field_template_filter();
if ( count( $filtered_cfts ) > 0 ) :
if ( count( $filtered_cfts ) > 0 ) :
$id = $filtered_cfts[0]['id'];
$id = $filtered_cfts[0]['id'];
else :
else :
$id = 0;
$id = 0;
endif;
endif;
endif;
endif;
list( $body, $init_id ) = $this->load_custom_field( $id );
list( $body, $init_id ) = $this->load_custom_field( $id );
echo $body;
echo $body;
exit();
exit();
}
}


if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/plugins.php' ) && ( ( isset( $_GET['activate'] ) && $_GET['activate'] == 'true' ) || ( isset( $_GET['activate-multi'] ) && $_GET['activate-multi'] == 'true' ) ) ) {
if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/plugins.php' ) && ( ( isset( $_GET['activate'] ) && $_GET['activate'] == 'true' ) || ( isset( $_GET['activate-multi'] ) && $_GET['activate-multi'] == 'true' ) ) ) {
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();
if ( ! $options ) {
if ( ! $options ) {
$this->install_custom_field_template_data();
$this->install_custom_field_template_data();
$this->install_custom_field_template_css();
$this->install_custom_field_template_css();
}
}
}
}


if ( function_exists( 'current_user_can' ) && current_user_can( 'edit_plugins' ) ) :
if ( function_exists( 'current_user_can' ) && current_user_can( 'edit_plugins' ) ) :
if ( isset( $_POST['custom_field_template_export_options_submit'] ) ) :
if ( isset( $_POST['custom_field_template_export_options_submit'] ) ) :
$filename = "cft" . date( 'Ymd' );
$filename = "cft" . date( 'Ymd' );
header( "Accept-Ranges: none" );
header( "Accept-Ranges: none" );
header( "Content-Disposition: attachment; filename=$filename" );
header( "Content-Disposition: attachment; filename=$filename" );
header( 'Content-Type: application/octet-stream' );
header( 'Content-Type: application/octet-stream' );
echo maybe_serialize( $options );
echo maybe_serialize( $options );
exit();
exit();
endif;
endif;
endif;
endif;


if ( ! empty( $options['custom_field_template_widget_shortcode'] ) ) {
if ( ! empty( $options['custom_field_template_widget_shortcode'] ) ) {
add_filter( 'widget_text', 'do_shortcode' );
add_filter( 'widget_text', 'do_shortcode' );
}
}


if ( substr( $wp_version, 0, 3 ) >= '2.7' ) {
if ( substr( $wp_version, 0, 3 ) >= '2.7' ) {
if ( empty( $options['custom_field_template_disable_custom_field_column'] ) ) :
if ( empty( $options['custom_field_template_disable_custom_field_column'] ) ) :
add_action( 'manage_posts_custom_column', array( &$this, 'add_manage_posts_custom_column' ), 10, 2 );
add_action( 'manage_posts_custom_column', array( &$this, 'add_manage_posts_custom_column' ), 10, 2 );
add_filter( 'manage_posts_columns', array( &$this, 'add_manage_posts_columns' ) );
add_filter( 'manage_posts_columns', array( &$this, 'add_manage_posts_columns' ) );
add_action( 'manage_pages_custom_column', array( &$this, 'add_manage_posts_custom_column' ), 10, 2 );
add_action( 'manage_pages_custom_column', array( &$this, 'add_manage_posts_custom_column' ), 10, 2 );
add_filter( 'manage_pages_columns', array( &$this, 'add_manage_pages_columns' ) );
add_filter( 'manage_pages_columns', array( &$this, 'add_manage_pages_columns' ) );
endif;
endif;
if ( empty( $options['custom_field_template_disable_quick_edit'] ) ) {
if ( empty( $options['custom_field_template_disable_quick_edit'] ) ) {
add_action( 'quick_edit_custom_box', array( &$this, 'add_quick_edit_custom_box' ), 10, 2 );
add_action( 'quick_edit_custom_box', array( &$this, 'add_quick_edit_custom_box' ), 10, 2 );
}
}
}
}


if ( substr( $wp_version, 0, 3 ) < '2.5' ) {
if ( substr( $wp_version, 0, 3 ) < '2.5' ) {
add_action( 'simple_edit_form', array( &$this, 'insert_custom_field' ), 1 );
add_action( 'simple_edit_form', array( &$this, 'insert_custom_field' ), 1 );
add_action( 'edit_form_advanced', array( &$this, 'insert_custom_field' ), 1 );
add_action( 'edit_form_advanced', array( &$this, 'insert_custom_field' ), 1 );
add_action( 'edit_page_form', array( &$this, 'insert_custom_field' ), 1 );
add_action( 'edit_page_form', array( &$this, 'insert_custom_field' ), 1 );
}
}


if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page.php' ) ) :
if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page.php' ) ) :
add_action( 'admin_head', array( &$this, 'custom_field_template_admin_head_buffer' ) );
add_action( 'admin_head', array( &$this, 'custom_field_template_admin_head_buffer' ) );
add_action( 'admin_footer', array( &$this, 'custom_field_template_admin_footer_buffer' ) );
add_action( 'admin_footer', array( &$this, 'custom_field_template_admin_footer_buffer' ) );
endif;
endif;
}
}


function custom_field_template_admin_init() {
function custom_field_template_admin_init() {
add_thickbox();
add_thickbox();
}
}


function custom_field_template_add_meta_boxes() {
function custom_field_template_add_meta_boxes() {
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();


if ( function_exists( 'remove_meta_box' ) && ! empty( $options['custom_field_template_disable_default_custom_fields'] ) ) :
if ( function_exists( 'remove_meta_box' ) && ! empty( $options['custom_field_template_disable_default_custom_fields'] ) ) :
remove_meta_box( 'postcustom', 'post', 'normal' );
remove_meta_box( 'postcustom', 'post', 'normal' );
remove_meta_box( 'postcustom', 'page', 'normal' );
remove_meta_box( 'postcustom', 'page', 'normal' );
remove_meta_box( 'pagecustomdiv', 'page', 'normal' );
remove_meta_box( 'pagecustomdiv', 'page', 'normal' );
endif;
endif;


if ( ! empty( $options['custom_field_template_deploy_box'] ) ) :
if ( ! empty( $options['custom_field_template_deploy_box'] ) ) :
if ( ! empty( $options['custom_fields'] ) ) :
if ( ! empty( $options['custom_fields'] ) ) :
$i = 0;
$i = 0;
foreach ( $options['custom_fields'] as $key => $val ) :
foreach ( $options['custom_fields'] as $key => $val ) :
if ( empty( $options['custom_field_template_replace_the_title'] ) ) {
if ( empty( $options['custom_field_template_replace_the_title'] ) ) {
$title = __( 'Custom Field Template', 'custom-field-template' );
$title = __( 'Custom Field Template', 'custom-field-template' );
} else {
} else {
$title = $options['custom_fields'][ $key ]['title'];
$title = $options['custom_fields'][ $key ]['title'];
}
}
if ( empty( $options['custom_fields'][ $key ]['custom_post_type'] ) ) :
if ( empty( $options['custom_fields'][ $key ]['custom_post_type'] ) ) :
if ( empty( $options['custom_fields'][ $key ]['post_type'] ) ) :
if ( empty( $options['custom_fields'][ $key ]['post_type'] ) ) :
add_meta_box( 'cftdiv' . $i, $title, array(
add_meta_box( 'cftdiv' . $i, $title, array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), 'post', 'normal', 'core', array( 'cft_id' => $key ) );
), 'post', 'normal', 'core', array( 'cft_id' => $key ) );
add_meta_box( 'cftdiv' . $i, $title, array(
add_meta_box( 'cftdiv' . $i, $title, array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), 'page', 'normal', 'core', array( 'cft_id' => $key ) );
), 'page', 'normal', 'core', array( 'cft_id' => $key ) );
elseif ( $options['custom_fields'][ $key ]['post_type'] == 'post' ) :
elseif ( $options['custom_fields'][ $key ]['post_type'] == 'post' ) :
add_meta_box( 'cftdiv' . $i, $title, array(
add_meta_box( 'cftdiv' . $i, $title, array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), 'post', 'normal', 'core', array( 'cft_id' => $key ) );
), 'post', 'normal', 'core', array( 'cft_id' => $key ) );
elseif ( $options['custom_fields'][ $key ]['post_type'] == 'page' ) :
elseif ( $options['custom_fields'][ $key ]['post_type'] == 'page' ) :
add_meta_box( 'cftdiv' . $i, $title, array(
add_meta_box( 'cftdiv' . $i, $title, array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), 'page', 'normal', 'core', array( 'cft_id' => $key ) );
), 'page', 'normal', 'core', array( 'cft_id' => $key ) );
endif;
endif;
else :
else :
$tmp_custom_post_type = explode( ',', $options['custom_fields'][ $key ]['custom_post_type'] );
$tmp_custom_post_type = explode( ',', $options['custom_fields'][ $key ]['custom_post_type'] );
$tmp_custom_post_type = array_filter( $tmp_custom_post_type );
$tmp_custom_post_type = array_filter( $tmp_custom_post_type );
$tmp_custom_post_type = array_unique( array_filter( array_map( 'trim', $tmp_custom_post_type ) ) );
$tmp_custom_post_type = array_unique( array_filter( array_map( 'trim', $tmp_custom_post_type ) ) );
foreach ( $tmp_custom_post_type as $type ) :
foreach ( $tmp_custom_post_type as $type ) :
add_meta_box( 'cftdiv' . $i, $title, array(
add_meta_box( 'cftdiv' . $i, $title, array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), $type, 'normal', 'core', array( 'cft_id' => $key ) );
), $type, 'normal', 'core', array( 'cft_id' => $key ) );
endforeach;
endforeach;
endif;
endif;
$i ++;
$i ++;
endforeach;
endforeach;
endif;
endif;
else :
else :
add_meta_box( 'cftdiv', __( 'Custom Field Template', 'custom-field-template' ), array(
add_meta_box( 'cftdiv', __( 'Custom Field Template', 'custom-field-template' ), array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), 'post', 'normal', 'core' );
), 'post', 'normal', 'core' );
add_meta_box( 'cftdiv', __( 'Custom Field Template', 'custom-field-template' ), array(
add_meta_box( 'cftdiv', __( 'Custom Field Template', 'custom-field-template' ), array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), 'page', 'normal', 'core' );
), 'page', 'normal', 'core' );
endif;
endif;


if ( empty( $options['custom_field_template_deploy_box'] ) && is_array( $options['custom_fields'] ) ) :
if ( empty( $options['custom_field_template_deploy_box'] ) && is_array( $options['custom_fields'] ) ) :
$custom_post_type = array();
$custom_post_type = array();
foreach ( $options['custom_fields'] as $key => $val ) :
foreach ( $options['custom_fields'] as $key => $val ) :
if ( isset( $options['custom_fields'][ $key ]['custom_post_type'] ) ) :
if ( isset( $options['custom_fields'][ $key ]['custom_post_type'] ) ) :
$tmp_custom_post_type = explode( ',', $options['custom_fields'][ $key ]['custom_post_type'] );
$tmp_custom_post_type = explode( ',', $options['custom_fields'][ $key ]['custom_post_type'] );
$tmp_custom_post_type = array_filter( $tmp_custom_post_type );
$tmp_custom_post_type = array_filter( $tmp_custom_post_type );
$tmp_custom_post_type = array_unique( array_filter( array_map( 'trim', $tmp_custom_post_type ) ) );
$tmp_custom_post_type = array_unique( array_filter( array_map( 'trim', $tmp_custom_post_type ) ) );
$custom_post_type = array_merge( $custom_post_type, $tmp_custom_post_type );
$custom_post_type = array_merge( $custom_post_type, $tmp_custom_post_type );
endif;
endif;
endforeach;
endforeach;
if ( isset( $custom_post_type ) && is_array( $custom_post_type ) ) :
if ( isset( $custom_post_type ) && is_array( $custom_post_type ) ) :
foreach ( $custom_post_type as $val ) :
foreach ( $custom_post_type as $val ) :
if ( function_exists( 'remove_meta_box' ) && ! empty( $options['custom_field_template_disable_default_custom_fields'] ) ) :
if ( function_exists( 'remove_meta_box' ) && ! empty( $options['custom_field_template_disable_default_custom_fields'] ) ) :
remove_meta_box( 'postcustom', $val, 'normal' );
remove_meta_box( 'postcustom', $val, 'normal' );
endif;
endif;
add_meta_box( 'cftdiv', __( 'Custom Field Template', 'custom-field-template' ), array(
add_meta_box( 'cftdiv', __( 'Custom Field Template', 'custom-field-template' ), array(
&$this,
&$this,
'insert_custom_field'
'insert_custom_field'
), $val, 'normal', 'core' );
), $val, 'normal', 'core' );
if ( empty( $options['custom_field_template_disable_custom_field_column'] ) ) :
if ( empty( $options['custom_field_template_disable_custom_field_column'] ) ) :
add_filter( 'manage_' . $val . '_posts_columns', array( &$this, 'add_manage_pages_columns' ) );
add_filter( 'manage_' . $val . '_posts_columns', array( &$this, 'add_manage_pages_columns' ) );
endif;
endif;
endforeach;
endforeach;
endif;
endif;
endif;
endif;
}
}


function custom_field_template_attachment_fields_to_edit( $form_fields, $post ) {
function custom_field_template_attachment_fields_to_edit( $form_fields, $post ) {
$form_fields["custom_field_template"]["label"] = __( 'Media Picker', 'custom-field-template' );
$form_fields["custom_field_template"]["label"] = __( 'Media Picker', 'custom-field-template' );
$form_fields["custom_field_template"]["input"] = "html";
$form_fields["custom_field_template"]["input"] = "html";
$form_fields["custom_field_template"]["html"] = '<a href="javascript:void(0);" onclick="var win = window.dialogArguments || opener || parent || top;win.cft_use_this(' . $post->ID . ');return false;">' . __( 'Use this', 'custom-field-template' ) . '</a>';
$form_fields["custom_field_template"]["html"] = '<a href="javascript:void(0);" onclick="var win = window.dialogArguments || opener || parent || top;win.cft_use_this(' . $post->ID . ');return false;">' . __( 'Use this', 'custom-field-template' ) . '</a>';


return $form_fields;
return $form_fields;
}
}


function custom_field_template_add_enctype( $buffer ) {
function custom_field_template_add_enctype( $buffer ) {
$buffer = preg_replace( '/<form name="post"/', '<form enctype="multipart/form-data" name="post"', $buffer );
$buffer = preg_replace( '/<form name="post"/', '<form enctype="multipart/form-data" name="post"', $buffer );


return $buffer;
return $buffer;
}
}


function custom_field_template_admin_head_buffer() {
function custom_field_template_admin_head_buffer() {
ob_start( array( &$this, 'custom_field_template_add_enctype' ) );
ob_start( array( &$this, 'custom_field_template_add_enctype' ) );
}
}


function custom_field_template_admin_footer_buffer() {
function custom_field_template_admin_footer_buffer() {
ob_end_flush();
ob_end_flush();
}
}


function has_meta( $postid ) {
function has_meta( $postid ) {
global $wpdb;
global $wpdb;


return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, post_id FROM $wpdb->postmeta WHERE post_id = %d ORDER BY meta_key,meta_id", $postid ), ARRAY_A );
return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, post_id FROM $wpdb->postmeta WHERE post_id = %d ORDER BY meta_key,meta_id", $postid ), ARRAY_A );
}
}


function get_post_meta( $post_id, $key = '', $single = false ) {
function get_post_meta( $post_id, $key = '', $single = false ) {
if ( ! $post_id ) {
if ( ! $post_id ) {
return '';
return '';
}
}


if ( $preview_id = $this->get_preview_id( $post_id ) ) {
if ( $preview_id = $this->get_preview_id( $post_id ) ) {
$post_id = $preview_id;
$post_id = $preview_id;
}
}


$post_id = (int) $post_id;
$post_id = (int) $post_id;


$meta_cache = wp_cache_get( $post_id, 'cft_post_meta' );
$meta_cache = wp_cache_get( $post_id, 'cft_post_meta' );


if ( ! $meta_cache ) {
if ( ! $meta_cache ) {
if ( $meta_list = $this->has_meta( $post_id ) ) {
if ( $meta_list = $this->has_meta( $post_id ) ) {
foreach ( (array) $meta_list as $metarow ) {
foreach ( (array) $meta_list as $metarow ) {
$mpid = (int) $metarow['post_id'];
$mpid = (int) $metarow['post_id'];
$mkey = $metarow['meta_key'];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];
$mval = $metarow['meta_value'];


if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
$cache[ $mpid ] = array();
$cache[ $mpid ] = array();
}
}
if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {
if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {
$cache[ $mpid ][ $mkey ] = array();
$cache[ $mpid ][ $mkey ] = array();
}
}


$cache[ $mpid ][ $mkey ][] = $mval;
$cache[ $mpid ][ $mkey ][] = $mval;
}
}
}
}


if ( ! empty( $cache ) && is_array( $cache ) ) :
if ( ! empty( $cache ) && is_array( $cache ) ) :
foreach ( (array) array_keys( $cache ) as $post ) {
foreach ( (array) array_keys( $cache ) as $post ) {
wp_cache_set( $post, $cache[ $post ], 'cft_post_meta' );
wp_cache_set( $post, $cache[ $post ], 'cft_post_meta' );
}
}


$meta_cache = wp_cache_get( $post_id, 'cft_post_meta' );
$meta_cache = wp_cache_get( $post_id, 'cft_post_meta' );
endif;
endif;
}
}


if ( $key ) :
if ( $key ) :
if ( $single && isset( $meta_cache[ $key ][0] ) ) :
if ( $single && isset( $meta_cache[ $key ][0] ) ) :
return maybe_unserialize( $meta_cache[ $key ][0] );
return maybe_unserialize( $meta_cache[ $key ][0] );
else :
else :
if ( isset( $meta_cache[ $key ] ) ) :
if ( isset( $meta_cache[ $key ] ) ) :
if ( is_array( $meta_cache[ $key ] ) ) :
if ( is_array( $meta_cache[ $key ] ) ) :
return array_map( 'maybe_unserialize', $meta_cache[ $key ] );
return array_map( 'maybe_unserialize', $meta_cache[ $key ] );
else :
else :
return $meta_cache[ $key ];
return $meta_cache[ $key ];
endif;
endif;
endif;
endif;
endif;
endif;
else :
else :
if ( is_array( $meta_cache ) ) :
if ( is_array( $meta_cache ) ) :
return array_map( 'maybe_unserialize', $meta_cache );
return array_map( 'maybe_unserialize', $meta_cache );
endif;
endif;
endif;
endif;


return '';
return '';
}
}


function add_quick_edit_custom_box( $column_name, $type ) {
function add_quick_edit_custom_box( $column_name, $type ) {
if ( $column_name == 'custom-fields' ) :
if ( $column_name == 'custom-fields' ) :
global $wp_version;
global $wp_version;
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();


if ( $options == null ) {
if ( $options == null ) {
return;
return;
}
}


if ( ! $options['css'] ) {
if ( ! $options['css'] ) {
$this->install_custom_field_template_css();
$this->install_custom_field_template_css();
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();
}
}


$out = '';
$out = '';
$out .= '<fieldset style="clear:both;">' . "\n";
$out .= '<fieldset style="clear:both;">' . "\n";
$out .= '<div class="inline-edit-group">';
$out .= '<div class="inline-edit-group">';
$out .= '<style type="text/css">' . "\n" .
$out .= '<style type="text/css">' . "\n" .
'<!--' . "\n";
'<!--' . "\n";
$out .= $options['css'] . "\n";
$out .= $options['css'] . "\n";
$out .= '-->' . "\n" .
$out .= '-->' . "\n" .
'</style>';
'</style>';


if ( count( $options['custom_fields'] ) > 1 ) {
if ( count( $options['custom_fields'] ) > 1 ) {
$out .= '<select id="custom_field_template_select">';
$out .= '<select id="custom_field_template_select">';
for ( $i = 0; $i < count( $options['custom_fields'] ); $i ++ ) {
for ( $i = 0; $i < count( $options['custom_fields'] ); $i ++ ) {
if ( isset( $_REQUEST['post'] ) && isset( $options['posts'][ $_REQUEST['post'] ] ) && $i == $options['posts'][ $_REQUEST['post'] ] ) {
if ( isset( $_REQUEST['post'] ) && isset( $options['posts'][ $_REQUEST['post'] ] ) && $i == $options['posts'][ $_REQUEST['post'] ] ) {
$out .= '<option value="' . $i . '" selected="selected">' . stripcslashes( $options['custom_fields'][ $i ]['title'] ) . '</option>';
$out .= '<option value="' . $i . '" selected="selected">' . stripcslashes( $options['custom_fields'][ $i ]['title'] ) . '</option>';
} else {
} else {
$out .= '<option value="' . $i . '">' . stripcslashes( $options['custom_fields'][ $i ]['title'] ) . '</option>';
$out .= '<option value="' . $i . '">' . stripcslashes( $options['custom_fields'][ $i ]['title'] ) . '</option>';
}
}
}
}
$out .= '</select>';
$out .= '</select>';
$out .= '<input type="button" class="button" value="' . __( 'Load', 'custom-field-template' ) . '" onclick="var post = jQuery(this).parent().parent().parent().parent().attr(\'id\').replace(\'edit-\',\'\'); var cftloading_select = function() {jQuery.ajax({type: \'GET\', url: \'?page=custom-field-template/custom-field-template.php&cft_mode=ajaxload&id=\'+jQuery(\'#custom_field_template_select\').val()+\'&post=\'+post, success: function(html) {jQuery(\'#cft\').html(html);}});};cftloading_select(post);" />';
$out .= '<input type="button" class="button" value="' . __( 'Load', 'custom-field-template' ) . '" onclick="var post = jQuery(this).parent().parent().parent().parent().attr(\'id\').replace(\'edit-\',\'\'); var cftloading_select = function() {jQuery.ajax({type: \'GET\', url: \'?page=custom-field-template/custom-field-template.php&cft_mode=ajaxload&id=\'+jQuery(\'#custom_field_template_select\').val()+\'&post=\'+post, success: function(html) {jQuery(\'#cft\').html(html);}});};cftloading_select(post);" />';
}
}


$out .= '<input type="hidden" name="custom-field-template-verify-key" id="custom-field-template-verify-key" value="' . wp_create_nonce( 'custom-field-template' ) . '" />';
$out .= '<input type="hidden" name="custom-field-template-verify-key" id="custom-field-template-verify-key" value="' . wp_create_nonce( 'custom-field-template' ) . '" />';
$out .= '<div id="cft" class="cft">';
$out .= '<div id="cft" class="cft">';
$out .= '</div>';
$out .= '</div>';


$out .= '</div>' . "\n";
$out .= '</div>' . "\n";
$out .= '</fieldset>' . "\n";
$out .= '</fieldset>' . "\n";


echo $out;
echo $out;
endif;
endif;
}
}


function custom_field_template_admin_head() {
function custom_field_template_admin_head() {
global $wp_version, $post;
global $wp_version, $post;
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();


if ( ! defined( 'WP_PLUGIN_DIR' ) ) {
echo '<link rel="stylesheet" type="text/css" href="' . CFT_PLUGIN_URL . '/js/datePicker.css" />' . "\n";
$plugin_dir = str_replace( ABSPATH, '', dirname( __FILE__ ) );
} else {
$plugin_dir = dirname( plugin_basename( __FILE__ ) );
}

echo '<link rel="stylesheet" type="text/css" href="' . wp_guess_url() . '/' . PLUGINDIR . '/' . $plugin_dir . '/js/datePicker.css" />' . "\n";


if ( ! empty( $options['custom_field_template_use_validation'] ) ) :
if ( ! empty( $options['custom_field_template_use_validation'] ) ) :
if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) || ( is_object( $post ) && $post->post_type == 'page' ) ) :
if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page-new.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/page.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) || ( is_object( $post ) && $post->post_type == 'page' ) ) :
?>
?>
<script type="text/javascript">
<script type="text/javascript">
// <![CDATA[
// <![CDATA[
jQuery(document).ready(function () {
jQuery(document).ready(function () {
jQuery("#post").validate();
jQuery("#post").validate();
});
});
//-->
//-->
</script>
</script>
<style type="text/css">
<style type="text/css">
<!--
<!--
label.error {
label.error {
color: #FF0000;
color: #FF0000;
}
}


-->
-->
</style>
</style>


<?php
<?php
endif;
endif;
endif;
endif;


if ( substr( $wp_version, 0, 3 ) >= '2.7' && is_user_logged_in() && ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit-pages.php' ) ) && ! strstr( $_SERVER['REQUEST_URI'], 'page=' ) ) {
if ( substr( $wp_version, 0, 3 ) >= '2.7' && is_user_logged_in() && ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit-pages.php' ) ) && ! strstr( $_SERVER['REQUEST_URI'], 'page=' ) ) {
?>
?>
<script type="text/javascript">
<script type="text/javascript">
// <![CDATA[
// <![CDATA[
jQuery(document).ready(function () {
jQuery(document).ready(function () {
jQuery('.hide-if-no-js-cft').show();
jQuery('.hide-if-no-js-cft').show();
jQuery('.hide-if-js-cft').hide();
jQuery('.hide-if-js-cft').hide();


inlineEditPost.addEvents = function (r) {
inlineEditPost.addEvents = function (r) {
r.each(function () {
r.each(function () {
var row = jQuery(this);
var row = jQuery(this);
jQuery('a.editinline', row).click(function () {
jQuery('a.editinline', row).click(function () {
inlineEditPost.edit(this);
inlineEditPost.edit(this);
post_id = jQuery(this).parent().parent().parent().parent().attr('id').replace('post-', '');
post_id = jQuery(this).parent().parent().parent().parent().attr('id').replace('post-', '');
inlineEditPost.cft_load(post_id);
inlineEditPost.cft_load(post_id);
return false;
return false;
});
});
});
});
}
}


inlineEditPost.save = function (id) {
inlineEditPost.save = function (id) {
if (typeof(id) == 'object')
if (typeof(id) == 'object')
id = this.getId(id);
id = this.getId(id);


jQuery('table.widefat .inline-edit-save .waiting').show();
jQuery('table.widefat .inline-edit-save .waiting').show();


var params = {
var params = {
action: 'inline-save',
action: 'inline-save',
post_type: <?php if ( substr( $wp_version, 0, 3 ) >= '3.0' ) {
post_type: <?php if ( substr( $wp_version, 0, 3 ) >= '3.0' ) {
echo 'typenow';
echo 'typenow';
} else {
} else {
echo 'this.type';
echo 'this.type';
} ?>,
} ?>,
post_ID: id,
post_ID: id,
edit_date: 'true'
edit_date: 'true'
};
};


var fields = jQuery('#edit-' + id + ' :input').fieldSerialize();
var fields = jQuery('#edit-' + id + ' :input').fieldSerialize();
params = fields + '&' + jQuery.param(params);
params = fields + '&' + jQuery.param(params);


// make ajax request
// make ajax request
jQuery.post('admin-ajax.php', params,
jQuery.post('admin-ajax.php', params,
function (r) {
function (r) {
jQuery('table.widefat .inline-edit-save .waiting').hide();
jQuery('table.widefat .inline-edit-save .waiting').hide();


if (r) {
if (r) {
if (-1 != r.indexOf('<tr')) {
if (-1 != r.indexOf('<tr')) {
jQuery(inlineEditPost.what + id).remove();
jQuery(inlineEditPost.what + id).remove();
jQuery('#edit-' + id).before(r).remove();
jQuery('#edit-' + id).before(r).remove();


var row = jQuery(inlineEditPost.what + id);
var row = jQuery(inlineEditPost.what + id);
row.hide();
row.hide();


if ('draft' == jQuery('input[name="post_status"]').val())
if ('draft' == jQuery('input[name="post_status"]').val())
row.find('td.column-comments').hide();
row.find('td.column-comments').hide();


row.find('.hide-if-no-js').removeClass('hide-if-no-js');
row.find('.hide-if-no-js').removeClass('hide-if-no-js');
jQuery('.hide-if-no-js-cft').show();
jQuery('.hide-if-no-js-cft').show();
jQuery('.hide-if-js-cft').hide();
jQuery('.hide-if-js-cft').hide();


inlineEditPost.addEvents(row);
inlineEditPost.addEvents(row);
row.fadeIn();
row.fadeIn();
} else {
} else {
r = r.replace(/<.[^<>]*?>/g, '');
r = r.replace(/<.[^<>]*?>/g, '');
jQuery('#edit-' + id + ' .inline-edit-save').append('<span class="error">' + r + '</span>');
jQuery('#edit-' + id + ' .inline-edit-save').append('<span class="error">' + r + '</span>');
}
}
} else {
} else {
jQuery('#edit-' + id + ' .inline-edit-save').append('<span class="error">' + inlineEditL10n.error + '</span>');
jQuery('#edit-' + id + ' .inline-edit-save').append('<span class="error">' + inlineEditL10n.error + '</span>');
}
}
}
}
, 'html');
, 'html');
return false;
return false;
}
}


jQuery('.editinline').click(function () {
jQuery('.editinline').click(function () {
post_id = jQuery(this).parent().parent().parent().parent().attr('id').replace('post-', '');
post_id = jQuery(this).parent().parent().parent().parent().attr('id').replace('post-', '');
inlineEditPost.cft_load(post_id);
inlineEditPost.cft_load(post_id);
});
});
inlineEditPost.cft_load = function (post_id) {
inlineEditPost.cft_load = function (post_id) {
jQuery.ajax({
jQuery.ajax({
type: 'GET',
type: 'GET',
url: '?page=custom-field-template/custom-field-template.php&cft_mode=ajaxload&post=' + post_id,
url: '?page=custom-field-template/custom-field-template.php&cft_mode=ajaxload&post=' + post_id,
success: function (html) {
success: function (html) {
jQuery('#cft').html(html);
jQuery('#cft').html(html);
}
}
});
});
};
};
});
});
//-->
//-->
</script>
</script>
<style type="text/css">
<style type="text/css">
<!--
<!--
div.cft_list p.key {
div.cft_list p.key {
font-weight: bold;
font-weight: bold;
margin: 0;
margin: 0;
}
}


div.cft_list p.value {
div.cft_list p.value {
margin: 0 0 0 10px;
margin: 0 0 0 10px;
}
}


.cft-actions {
.cft-actions {
visibility: hidden;
visibility: hidden;
padding: 2px 0 0;
padding: 2px 0 0;
}
}


tr:hover .cft-actions {
tr:hover .cft-actions {
visibility: visible;
visibility: visible;
}
}


.inline-edit-row fieldset label {
.inline-edit-row fieldset label {
display: inline;
display: inline;
}
}


label.error {
label.error {
color: #FF0000;
color: #FF0000;
}
}


-->
-->
</style>
</style>
<?php
<?php
}
}
}
}


function custom_field_template_edit_form_advanced() {
function custom_field_template_edit_form_advanced() {
global $wp_version;
global $wp_version;
$options = $this->get_custom_field_template_data();
$options = $this->get_custom_field_template_data();


if ( ! empty( $options['custom_field_template_deploy_box'] ) ) :
if ( ! empty( $options['custom_field_template_deploy_box'] ) ) :
$suffix = '"+win.jQuery("#cft_current_template").val()+"';
$suffix = '"+win.jQuery("#cft_current_template").val()+"';
else :
else :
$suffix = '';
$suffix = '';
endif;
endif;


$out = '';
$out = '';
$out .= '<script type="text/javascript">' . "\n" .
$out .= '<script type="text/javascript">' . "\n" .
'// <![CDATA[' . "\n";
'// <![CDATA[' . "\n";
$out .= 'function cft_use_this(file_id) {
$out .= 'function cft_use_this(file_id) {
var win = window.dialogArguments || opener || parent || top;
var win = window.dialogArguments || opener || parent || top;
win.jQuery("#"+win.jQuery("#cft_clicked_id").val()+"_hide").val(file_id);
win.jQuery("#"+win.jQuery("#cft_clicked_id").val()+"_hide").val(file_id);
var fields = win.jQuery("#cft' . $suffix . ' :input").fieldSerialize();
var fields = win.jQuery("#cft' . $suffix . ' :input").fieldSerialize();
win.jQuery.ajax({type: "POST", url: "?page=custom-field-template/custom-field-template.php&cft_mode=ajaxsave&post="+win.jQuery(\'#post_ID\').val()+"&custom-field-template-verify-key="+win.jQuery("#custom-field-template-verify-key").val(), data: fields, success: function() {win.jQuery.ajax({type: "GET", url: "?page=custom-field-template/custom-field-template.php&cft_mode=ajaxload&id="+win.jQuery("#cft_current_template").val()+"&post="+win.jQuery(\'#post_ID\').val(), success: function(html) {win.jQuery("#cft' . $suffix . '").html(html);win.tb_remove();}});}});
win.jQuery.ajax({type: "POST", url: "?page=custom-field-template/custom-field-template.php&cft_mode=ajaxsave&post="+win.jQuery(\'#post_ID\').val()+"&custom-field-template-verify-key="+win.jQuery("#custom-field-template-verify-key").val(), data: fields, success: function() {win.jQuery.ajax({type: "GET", url: "?page=custom-field-template/custom-field-template.php&cft_mode=ajaxload&id="+win.jQuery("#cft_current_template").val()+"&post="+win.jQuery(\'#post_ID\').val(), success: function(html) {win.jQuery("#cft' . $suffix . '").html(html);win.tb_remove();}});}});
}';
}';


$out .= 'function qt_set(new_id) { eval("qt_"+new_id+" = new QTags(\'qt_"+new_id+"\', \'"+new_id+"\', \'editorcontainer_"+new_id+"\', \'more\');");}';
$out .= 'function qt_set(new_id) { eval("qt_"+new_id+" = new QTags(\'qt_"+new_id+"\', \'"+new_id+"\', \'editorcontainer_"+new_id+"\', \'more\');");}';


$out .= 'function _edInsertContent(myField, myValue) {
$out .= 'function _edInsertContent(myField, myValue) {
var sel, startPos, endPos, scrollTop;
var sel, startPos, endPos, scrollTop;


//IE support
//IE support
if (document.selection) {
if (document.selection) {
myField.focus();
myField.focus();
sel = document.selection.createRange();
sel = document.selection.createRange();
sel.text = myValue;
sel.text = myValue;
myField.focus();
myField.focus();
}
}
//MOZILLA/NETSCAPE support
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == "0") {
else if (myField.selectionStart || myField.selectionStart == "0") {
startPos = myField.selectionStart;
startPos = myField.selectionStart;
endPos = myField.selectionEnd;
endPos = myField.selectionEnd;
scrollTop = myField.scrollTop;
scrollTop = myField.scrollTop;
myField.value = myField.value.substring(0, startPos)
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myValue
+ myField.value.substring(endPos, myField.value.length);
+ myField.value.substring(endPos, myField.value.length);
myField.focus();
myField.focus();
myField.selectionStart = startPos + myValue.length;
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.scrollTop = scrollTop;
myField.scrollTop = scrollTop;
} else {
} else {
myField.value += myValue;
myField.value += myValue;
myField.focus();
myField.focus();
}
}
}';
}';


$out .= 'function send_to_custom_field(h) {' . "\n" .
$out .= 'function send_to_custom_field(h) {' . "\n" .
' if ( tmpFocus ) ed = tmpFocus;' . "\n" .
' if ( tmpFocus ) ed = tmpFocus;' . "\n" .
' else if ( typeof tinyMCE == "undefined" ) ed = document.getElementById("content");' . "\n" .
' else if ( typeof tinyMCE == "undefined" ) ed = document.getElementById("content");' . "\n" .
' else { ed = tinyMCE.get("content"); if(ed) {if(!ed.isHidden()) isTinyMCE = true;}}' . "\n" .
' else { ed = tinyMCE.get("content"); if(ed) {if(!ed.isHidden()) isTinyMCE = true;}}' . "\n" .
' if ( typeof tinyMCE != "undefined" && isTinyMCE && !ed.isHidden() ) {' . "\n" .
' if ( typeof tinyMCE != "undefined" && isTinyMCE && !ed.isHidden() ) {' . "\n" .
' ed.focus();' . "\n" .
' ed.focus();' . "\n" .
' if ( tinymce.isIE && ed.windowManager.insertimagebookmark )' . "\n" .
' if ( tinymce.isIE && ed.windowManager.insertimagebookmark )' . "\n" .
' ed.selection.moveToBookmark(ed.windowManager.insertimagebookmark);' . "\n" .
' ed.selection.moveToBookmark(ed.windowManager.insertimagebookmark);' . "\n" .
' if ( h.indexOf("[caption") === 0 ) {' . "\n" .
' if ( h.indexOf("[caption") === 0 ) {' . "\n" .
' if ( ed.plugins.wpeditimage )' . "\n" .
' if ( ed.plugins.wpeditimage )' . "\n" .
' h = ed.plugins.wpeditimage._do_shcode(h);' . "\n" .
' h = ed.plugins.wpeditimage._do_shcode(h);' . "\n" .
' } else if ( h.indexOf("[gallery") === 0 ) {' . "\n" .
' } else if ( h.indexOf("[gallery") === 0 ) {' . "\n" .
' if ( ed.plugins.wpgallery )' . "\n" .
' if ( ed.plugins.wpgallery )' . "\n" .
' h = ed.plugins.wpgallery._do_gallery(h);' . "\n" .
' h = ed.plugins.wpgallery._do_gallery(h);' . "\n" .
' } else if ( h.indexOf("[embed") === 0 ) {' . "\n" .
' } else if ( h.indexOf("[embed") === 0 ) {' . "\n" .
' if ( ed.plugins.wordpress )' . "\n" .
' if ( ed.plugins.wordpress )' . "\n" .
' h = ed.plugins.wordpress._setEmbed(h);' . "\n" .
' h = ed.plugins.wordpress._setEmbed(h);' . "\n" .
' }' . "\n" .
' }' . "\n" .
' ed.execCommand("mceInsertContent", false, h);' . "\n" .
' ed.execCommand("mceInsertContent", false, h);' . "\n" .
' } else {' . "\n" .
' } else {' . "\n" .
' if ( tmpFocus ) _edInsertContent(tmpFocus, h);' . "\n" .
' if ( tmpFocus ) _edInsertContent(tmpFocus, h);' . "\n" .
' else edInsertContent(edCanvas, h);' . "\n" .
' }' . "\n";

if ( empty( $optio