jQuery(document).ready(function($){mf_use_new_image_gallery();});"); } } /** * Adding the metaboxes */ function mf_post_add_metaboxes() { global $post,$mf_post_values; //if the user are going to add a new link //the var $post is not defined and we do nothing if(!isset($post)) { return false; } $mf_post_values = $this->mf_get_post_values($post->ID); //Getting the post types $post_types = $this->mf_get_post_types( array(), 'names' ); foreach ( $post_types as $post_type ){ if ( post_type_supports($post_type, 'page-attributes') && $post_type != 'page' ) { // If the post type has page-attributes we are going to add // the meta box for choice a template by hand // this is because wordpress don't let choice a template // for any non-page post type add_meta_box( 'mf_template_attribute', __('Template'), array( &$this, 'mf_metabox_template' ), $post_type, 'side', 'default' ); } if( !mf_custom_fields::has_fields($post_type) ) { continue; } //getting the groups (each group is a metabox) $groups = $this->get_groups_by_post_type($post_type); //creating the metaboxes foreach( $groups as $group ) { if( $this->group_has_fields($group['id'] ) ) { add_meta_box( 'mf_'.$group['id'], $group['label'], array( &$this, 'mf_metabox_content' ), $post_type, 'normal', 'default', array( 'group_info' => $group) ); } } } } /** * Fill a metabox with custom fields */ function mf_metabox_content( $post, $metabox ) { global $mf_domain, $mf_post_values; //Getting the custom fields for this metabox $custom_fields = $this->get_custom_fields_by_group($metabox['args']['group_info']['id']); $group_id = $metabox['args']['group_info']['id']; //default markup ?>
mf_get_duplicated_groups( $post->ID, $group_id ); } else { $repeated_groups = 1; } for( $group_index = 1; $group_index <= $repeated_groups; $group_index++ ){ $only = ($repeated_groups == 1)? TRUE : FALSE; $this->mf_draw_group($metabox,$extraclass,$group_index,$custom_fields,$mf_post_values,$only); } printf('',$repeated_groups,$group_id); ?>
ID) ){ $repeated_field = $this->mf_get_duplicated_fields_by_group($post->ID, $field['name'],$group_id,$group_index); }else{ $repeated_field = 1; } $group_field = sprintf('mf_group_field_%d_%d_%d',$group_id,$group_index,$field['id']); print '
'; for( $field_index = 1; $field_index <= $repeated_field; $field_index++ ){ $only = ($repeated_field == 1)? TRUE : FALSE; $this->mf_draw_field($field,$group_id,$group_index,$field_index,$mf_post_values, $only); } printf('',$repeated_field,$group_id,$group_index,$field['id']); print '
'; ?>
  >
%s%s',__( 'What\'s this?', $mf_domain ),'%s'); $help = ($field['description'])? sprintf($tool,$field['description']) : ''; $required = ($field['required_field'])? ' *' : ''; $value = (!empty($mf_post_values[$field['name']][$group_index][$field_index])) ? $mf_post_values[$field['name']][$group_index][$field_index] : ''; ?>
(%d) ',$field_style,$field_index); print sprintf('
',$field['label'],$field_num,$required,$help); $f = $field['type'].'_field'; $f = new $f(); print '
'; $field['input_name'] = sprintf("magicfields[%s][%d][%d]",$field['name'],$group_index,$field_index); $field['input_id'] = sprintf("%s_%s_%s",$field['name'],$group_index,$field_index); $field['input_value'] = $value; $field['options'] = unserialize($field['options']); $field['input_validate'] = ($field['required_field']) ? 'validate="required:true"' : ''; print $f->display_field( $field,$group_index, $field_index); print '
'; if( $field['required_field'] ){ $id_validate = array('image_media','image','audio','file','color_picker','datepicker','markdown_editor','multiline'); $validate_name = ( in_array($field['type'],$id_validate) )? $field['input_id'] : $field['input_name']; if($field['type'] == 'color_picker') $validate_name = 'colorpicker_value_'.$validate_name; if($field['type'] == 'datepicker' ){ $validate_name = 'date_field_' . $validate_name; } if( isset($field['options']['options']) && $field['type'] != 'radiobutton_list' ) $validate_name .= '[]'; print '
'; } ?>
class="delete_duplicate_field">
get_custom_field($field_id); $this->mf_draw_field($field,$group_id,$group_index,$field_index); } public function mf_ajax_duplicate_group($group_id,$group_index){ $group = $this->get_group($group_id); $metabox = array( 'args' => array( 'group_info' => $group ) ); $custom_fields = $this->get_custom_fields_by_group($group_id); $this->mf_draw_group($metabox,'mf_duplicate_group',$group_index,$custom_fields); } /** When the post is saved, saves our custom data **/ function mf_save_post_data( $post_id ) { global $wpdb; //@todo hay que ponerle nonce a una de las metaboxes /*if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) ) {*/ //return $post_id; /*}*/ if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; //just in case if the post_id is a post revision and not the post inself if ( $the_post = wp_is_post_revision( $post_id ) ) { $post_id = $the_post; } // Check if the post_type has page attributes // if is the case is necessary need save the page_template if ( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] != 'page' && isset($_REQUEST['page_template'])) { add_post_meta($post_id, '_wp_mf_page_template', $_POST['page_template'], true) or update_post_meta($post_id, '_wp_mf_page_template', $_POST['page_template']); } if (!empty($_POST['magicfields'])) { $customfields = $_POST['magicfields']; /** Deleting the old values **/ $sql_delete = $wpdb->prepare( "DELETE FROM ".MF_TABLE_POST_META." WHERE post_id = %s",$post_id ); $wpdb->query($sql_delete); foreach ( $customfields as $field_name => $field ) { delete_post_meta($post_id, $field_name); } /** / Deleting the old values **/ //creating the new values foreach( $customfields as $field_name => $groups ) { $group_count = 1; foreach( $groups as $fields ) { $field_count = 1; foreach( $fields as $value ) { //here if the value of the field needs a process before to be saved //should be trigger that method here //$value = mf_process_value_by_type($field_name,$value); // Adding field value meta data add_post_meta($post_id, "{$field_name}", $value); $meta_id = $wpdb->insert_id; $sql_insert = $wpdb->prepare( "INSERT INTO " . MF_TABLE_POST_META . " ( meta_id, field_name, field_count, group_count, post_id ) " . " VALUES " . " (%s,'%s',%s,%s,%s) ", $meta_id, $field_name, $field_count, $group_count, $post_id ); $wpdb->query($sql_insert); $field_count++; } $group_count++; } } } } /** * * @param int $post_id the post id * @param int $group_id the group_id * @return int */ function mf_get_duplicated_groups( $post_id, $group_id ) { global $wpdb; $group_count = $wpdb->get_var( "SELECT mfpm.group_count FROM ".MF_TABLE_POST_META." AS mfpm LEFT JOIN ".MF_TABLE_CUSTOM_FIELDS." AS mfcf ON ( mfpm.field_name = mfcf.name) WHERE mfpm.post_id = {$post_id} AND custom_group_id = {$group_id} ORDER BY group_count DESC LIMIT 1" ); return ($group_count > 1) ? $group_count : 1; } /** * * @param int $post_id the post id * @param int $group_id the group_id * @param int $group_index (group_count) * @return int */ function mf_get_duplicated_fields_by_group( $post_id,$field_name, $group_id , $group_index ) { global $wpdb; $field_count = $wpdb->get_var( "SELECT mfpm.field_count FROM ".MF_TABLE_POST_META." AS mfpm LEFT JOIN ".MF_TABLE_CUSTOM_FIELDS." AS mfcf ON ( mfpm.field_name = mfcf.name) WHERE mfpm.post_id = {$post_id} AND custom_group_id = {$group_id} AND mfpm.group_count = {$group_index} AND mfpm.field_name = '{$field_name}' ORDER BY field_count DESC LIMIT 1" ); return ($field_count > 1) ? $field_count : 1; } /** * retrieve the custom fields values of a certain post */ function mf_get_post_values( $post_id ) { global $wpdb; $raw = $wpdb->get_results( "SELECT mfpm.meta_id, mfpm.field_name, mfpm.field_count, mfpm.group_count, pm.meta_value FROM ".MF_TABLE_POST_META." as mfpm LEFT JOIN ".$wpdb->postmeta." as pm ON ( mfpm.meta_id = pm.meta_id ) WHERE mfpm.post_id = ".$post_id ); $data = array(); foreach( $raw as $key => $field ){ $data[$field->field_name][$field->group_count][$field->field_count] = $field->meta_value; } return $data; } /* enqueue css and js base for post area*/ public function load_js_css_base(){ global $mf_domain; wp_enqueue_style( 'mf_field_base', MF_BASENAME.'css/mf_field_base.css' ); wp_enqueue_script( 'tmpl', MF_BASENAME.'js/third_party/jquery.tmpl.js'); wp_enqueue_script( 'mf_field_base', MF_BASENAME.'js/mf_field_base.js'); wp_enqueue_script( 'mf_sortable_groups', MF_BASENAME.'js/mf_sortable_groups.js', array( 'jquery-ui-sortable' ) ); $mceAddString = "mceAddControl"; $mceRemoveString = "mceRemoveControl"; if(is_wp39()){ $mceAddString = "mceAddEditor"; $mceRemoveString = "mceRemoveEditor"; } //global mf js $js_vars = array( 'mf_url' => MF_BASENAME, 'mf_player_url' => MF_BASENAME . 'js/singlemp3player.swf', 'mf_validation_error_msg' => __('Sorry, some required fields are missing. Please provide values for any highlighted fields and try again.',$mf_domain), 'mf_image_media_set' => __('Insert into field',$mf_domain), 'mf_mceAddString' => $mceAddString, 'mf_mceRemoveString' => $mceRemoveString ); wp_localize_script( 'mf_field_base', 'mf_js', $js_vars ); } /* enqueue css and js of fields */ public function load_js_css_fields(){ //Loading any custom field if is required if( !empty( $_GET['post']) && is_numeric( $_GET['post'] ) ) {//when the post already exists $post_type = get_post_type($_GET['post']); }else{ //Creating a new post $post_type = (!empty($_GET['post_type'])) ? $_GET['post_type'] : 'post'; } $fields = $this->get_unique_custom_fields_by_post_type($post_type); /* add tiny_mce script */ /* only add of editor support no exits for the post type*/ if( (in_array('multiline',$fields) || in_array('image_media',$fields) ) && !post_type_supports($post_type,'editor' ) ){ /*add_thickbox(); wp_enqueue_script('media-upload'); wp_enqueue_script('editor'); // load admin/mf_editor.js (switchEditor) mf_autoload('mf_tiny_mce'); // load admin/mf_tiny_mce.php (tinyMCE) add_action( 'admin_print_footer_scripts', 'mf_tiny_mce', 25 ); // embed tinyMCE add_action( 'admin_print_footer_scripts', array($this, 'media_buttons_add_mf'), 51 );*/ } foreach($fields as $field) { //todo: Este método debería también de buscar en los paths donde los usuarios ponen sus custom fields $type = $field."_field"; $type = new $type(); $properties = $type->get_properties(); if ( $properties['js'] ) { wp_enqueue_script( 'mf_field_'.$field, MF_BASENAME.'field_types/'.$field.'_field/'.$field.'_field.js', $properties['js_dependencies'], null, true ); /* idear forma por si se necesita mas de dos js*/ if( isset($properties['js_internal']) ){ wp_enqueue_script( 'mf_field_'. preg_replace('/\./','_',$properties['js_internal']), MF_BASENAME.'field_types/'.$field.'_field/'.$properties['js_internal'], $properties['js_internal_dependencies'], null, true ); } } if ( $properties['css'] ) { wp_enqueue_style( 'mf_field_'.$field, MF_BASENAME.'field_types/'.$field.'_field/'.$field.'_field.css' ); } if ( !empty($properties['css_dependencies'] )) { foreach($properties['css_dependencies'] as $css_script) { wp_enqueue_style($css_script); } } /* load css internal */ if(isset($properties['css_internal'])){ wp_enqueue_style( 'mf_field_'.preg_replace('/\./','_',$properties['css_internal']), MF_BASENAME.'field_types/'.$field.'_field/'.$properties['css_internal'] ); } } } public function check_exist_visual_editor(){ if( strstr( $_SERVER['REQUEST_URI'], 'post-new.php' ) !== FALSE || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post.php') !== FALSE ) { if( isset($_GET['action']) && $_GET['action'] == 'trash' ) {//when the post already exists return; } if( !empty( $_GET['post']) && is_numeric( $_GET['post'] ) ) {//when the post already exists $post_type = get_post_type($_GET['post']); }else{ //Creating a new post $post_type = (!empty($_GET['post_type'])) ? $_GET['post_type'] : 'post'; } $mf_posttype = new mf_posttype(); $pt = $mf_posttype->get_post_type($post_type); if ($pt && !isset($pt['support']['editor'])) { echo ""; } } } public function media_buttons_add_mf(){ print '
'; do_action( 'media_buttons' ); print '
'; } public function register_media_button($buttons) { array_push($buttons, "separator","add_image","add_video","add_audio","add_media"); return $buttons; } public function tmce_not_remove_p_and_br(){ ?> postmeta." WHERE meta_key='".$post_type_key."' "; $check = $wpdb->get_row($sql); if ($check) { $cata = $check->meta_value; $assignedCategoryIds = maybe_unserialize($cata); } } ?> ID, '_wp_mf_page_template', TRUE); $template = ($template != '') ? $template : false; ?>