text:
<?php

/*
 * Edit post page functions
 *
 *
 *
 * Core file with stable and working functions.
 * Please add hooks if adjustment needed, do not add any more new code here.
 *
 * Consider this file half-locked since Types 1.2
 */

// Include conditional field code
require_once WPCF_EMBEDDED_ABSPATH . '/includes/conditional-display.php';

/**
 * Init functions for post edit pages.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @param type $post
 */
function wpcf_admin_post_init( $post ) {
    add_action( 'admin_footer', 'wpcf_admin_fields_postfields_styles' );
    wpcf_admin_add_js_settings( 'wpcf_nonce_toggle_group',
            '\'' . wp_create_nonce( 'group_form_collapsed' ) . '\'' );
    wpcf_admin_add_js_settings( 'wpcf_nonce_toggle_fieldset',
            '\'' . wp_create_nonce( 'form_fieldset_toggle' ) . '\'' );

    // Get post_type
    $post_type = wpcf_admin_get_edited_post_type( $post );

    /*
     *
     * This is left to maintain compatibility with older versions
     * TODO Remove
     */
    // Add items to View dropdown
    if ( in_array( $post_type, array('view', 'view-template', 'cred-form', 'cred-user-form') ) ) {
        add_filter( 'editor_addon_menus_wpv-views',
                'wpcf_admin_post_editor_addon_menus_filter' );
        add_action( 'admin_footer', 'wpcf_admin_post_js_validation' );
        wpcf_enqueue_scripts();
        wp_enqueue_script( 'toolset-colorbox' );
        wp_enqueue_style( 'toolset-colorbox' );
    }
    // Never show on 'Views' and 'Content Templates'
    if ( in_array( $post_type, array('view', 'view-template') ) ) {
        return false;
    }

    /**
     * remove custom field WordPress metabox
     */
    if ( 'hide' == wpcf_get_settings('hide_standard_custom_fields_metabox') ) {
        foreach( array( 'normal', 'advanced', 'side' ) as $context) {
            remove_meta_box('postcustom', $post_type, $context );
        }
    }

    // Are Types active?
    $wpcf_active = false;

    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields( $post );

    foreach ( $groups as $group ) {

        $only_preview = '';

        //If Access plugin activated
        if ( function_exists( 'wpcf_access_register_caps' ) ) {
            //If user can't view own profile fields
            if ( !current_user_can( 'view_fields_in_edit_page_' . $group['slug'] ) ) {
                continue;
            }
            //If user can modify current group in own profile
            if ( !current_user_can( 'modify_fields_in_edit_page_' . $group['slug'] ) ) {
                $only_preview = 1;
            }
        }
        if ( !empty( $group['fields'] ) && empty( $only_preview ) ) {
            $wpcf_active = true;
            break;
        }
    }

    // Activate scripts
    if ( $wpcf_active ) {
        add_action( 'admin_head', 'wpcf_post_preview_warning' );
        wpcf_edit_post_screen_scripts();
    }

    // Add validation
    add_action( 'admin_footer', 'wpcf_admin_post_js_validation' );

    /*
     * TODO Review
     * This is forced because of various Child cases
     * and when field are rendered via AJAX but not registered yet.
     *
     * Basically all fields that require additional JS should be added here.
     *
     * This is a must for now.
     * These fields need init JS in various cases.
     */
    wpcf_field_enqueue_scripts( 'date' );
    wpcf_field_enqueue_scripts( 'image' );
    wpcf_field_enqueue_scripts( 'file' );
    wpcf_field_enqueue_scripts( 'skype' );
    wpcf_field_enqueue_scripts( 'numeric' );

    do_action( 'wpcf_admin_post_init', $post_type, $post, $groups, $wpcf_active );
}

/**
 * Add meta boxes.
 *
 * @param type $post_type
 * @param type $post
 * @return boolean
 */
function wpcf_add_meta_boxes( $post_type, $post )
{

    $post_types_without_meta_boxes = array(
        'view',
        'view-template',
        'acf-field-group',
        'acf'
    );

    /**
     * Do not add metaboxes
     *
     * Filter allow to add post types which are immune to wpcf_add_meta_boxes()
     * function.
     *
     * @since 1.9.0
     *
     * @param array $post_types array of post types slugs
     */
    $post_types_without_meta_boxes = apply_filters(
        'wpcf_exclude_meta_boxes_on_post_type',
        $post_types_without_meta_boxes
    );

    /** This action is documented in embedded/bootstrap.php */
    $post_types_without_meta_boxes = apply_filters(
        'toolset_filter_exclude_own_post_types',
        $post_types_without_meta_boxes
    );

    /**
     * check
     */
    if ( !empty($post_types_without_meta_boxes) && in_array( $post_type, $post_types_without_meta_boxes ) ) {
        return false;
    }

    //  Fix for empty $post (tabify)
    if ( empty( $post->ID ) ) {
        $post = get_default_post_to_edit( $post_type, false );
    }

    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields( $post );

    foreach ( $groups as $group ) {

        $only_preview = '';

        //If Access plugin activated
        if ( function_exists( 'wpcf_access_register_caps' ) ) {
            //If user can't view own profile fields
            if ( !current_user_can( 'view_fields_in_edit_page_' . $group['slug'] ) ) {
                continue;
            }
            //If user can modify current group in own profile
            if ( !current_user_can( 'modify_fields_in_edit_page_' . $group['slug'] ) ) {
                $only_preview = 1;
            }
        }

        // Process fields
        if ( !empty( $group['fields'] ) && empty( $only_preview ) ) {
            if ( defined( 'WPTOOLSET_FORMS_VERSION' ) ) {
                $errors = get_post_meta( $post->ID, '__wpcf-invalid-fields', true );
                $group['html'] = array();
                foreach ( $group['fields'] as $field ) {
                    wpcf_admin_post_add_to_editor( $field );
	                // Note that the $single argument defaults to false, ergo $meta will be allways an array of metadata,
	                // even for single fields.
                    $meta = get_post_meta( $post->ID, $field['meta_key'] );
                    $config = wptoolset_form_filter_types_field( $field, $post->ID );
                    if ( $errors ) {
                        $config['validate'] = true;
                    }
                    if ( $field['type'] == 'wysiwyg' ) {
                        $group['html'][] = array('config' => $config, 'meta' => $meta);
                    } else {
                        $group['html'][] = wptoolset_form_field( 'post',
                                $config, $meta );
                    }
                }
                // OLD
                delete_post_meta( $post->ID, 'wpcf-invalid-fields' );
                delete_post_meta( $post->ID, '__wpcf-invalid-fields' );
            } else {
                // Process fields
                $group['fields'] = wpcf_admin_post_process_fields( $post,
                        $group['fields'], true );
            }
        }

        // Check if hidden
        if ( !isset( $group['__show_meta_box'] ) || $group['__show_meta_box'] != false ) {

            $field_group = Types_Field_Group_Post_Factory::load( $group['slug'] );

            // Skip field groups that can't be loaded
            if( null !== $field_group ) {

                $group_wpml = new Types_Wpml_Field_Group( $field_group );

                // Add meta boxes
                if ( empty( $only_preview ) ) {
                    add_meta_box( "wpcf-group-{$group['slug']}",
                        $group_wpml->translate_name(),
                        'wpcf_admin_post_meta_box',
                        $post_type, $group['meta_box_context'], 'high', $group );
                } else {
                    add_meta_box( "wpcf-group-{$group['slug']}",
                        $group_wpml->translate_name(),
                        'wpcf_admin_post_meta_box_preview', $post_type,
                        $group['meta_box_context'], 'high', $group );
                }

            }
        }
    }
}

/**
 * Renders meta box content (preview).
 *
 * @param $post
 * @param array $group
 * @param string $echo
 *
 * @return string
 */
function wpcf_admin_post_meta_box_preview( $post, $group, $echo = '' ){

    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
    require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
    global $wpcf;

    // $group['args']['fields'] or $group['fields'] or array().
    $fields = wpcf_getnest(
        $group,
        array( 'args', 'fields' ),
        wpcf_getarr( $group, 'fields', array() )
    );

    $slug = wpcf_getarr( $group, 'slug', wpcf_getarr( $group, 'id' ) );
    $name = wpcf_getarr( $group, 'name', wpcf_getarr( $group, 'title' ) );

    $post = wpcf_admin_create_fake_post_if_need( $post );

    $group_output = '';
    if ( !empty( $echo ) ) {
        $group_output = '
This Preview generated for latest post "' . $post->post_title . '"
';
        $group_output .= '

' . $name . '

';
    }

    $group_output .= '

' . "\n\n";

    $content = $code = '';
    if( !empty( $fields ) ) {
        foreach ( $fields as $field ) {
            $html = '';
            $params['separator'] = ', ';
            if ( wpcf_admin_is_repetitive( $field ) ) {
                $wpcf->repeater->set( $post, $field );
                $_meta = $wpcf->repeater->_get_meta();
                if ( isset( $_meta['custom_order'] ) ) {
                    $meta = $_meta['custom_order'];
                } else {
                    $meta = array();
                }
                $content = $code = '';
                // Sometimes if meta is empty - array(0 => '') is returned
                if ( (count( $meta ) == 1 ) ) {
                    $meta_id = key( $meta );
                    $_temp = array_shift( $meta );
                    if (!is_array($_temp) && strval( $_temp ) == '' ) {

                    } else {
                        $params['field_value'] = $_temp;
                        if ( !empty( $params['field_value'] ) ) {
                            $html = types_render_field_single( $field, $params,
                                $content, $code, $meta_id );
                        }
                    }
                } else if ( !empty( $meta ) ) {
                    $output = '';

                    if ( isset( $params['index'] ) ) {
                        $index = $params['index'];
                    } else {
                        $index = '';
                    }

                    // Allow wpv-for-each shortcode to set the index
                    $index = apply_filters( 'wpv-for-each-index', $index );

                    if ( $index === '' ) {
                        $output = array();
                        foreach ( $meta as $temp_key => $temp_value ) {
                            $params['field_value'] = $temp_value;
                            if ( !empty( $params['field_value'] ) ) {
                                $temp_output = types_render_field_single( $field,
                                    $params, $content, $code, $temp_key );
                            }
                            if ( !empty( $temp_output ) ) {
                                $output[] = $temp_output;
                            }
                        }
                        if ( !empty( $output ) && isset( $params['separator'] ) ) {
                            $output = implode( html_entity_decode( $params['separator'] ),
                                $output );
                        } else if ( !empty( $output ) ) {
                            $output = implode( '', $output );
                        }
                    } else {
                        // Make sure indexed right
                        $_index = 0;
                        foreach ( $meta as $temp_key => $temp_value ) {
                            if ( $_index == $index ) {
                                $params['field_value'] = $temp_value;
                                if ( !empty( $params['field_value'] ) ) {
                                    $output = types_render_field_single( $field,
                                        $params, $content, $code, $temp_key );
                                }
                            }
                            $_index++;
                        }
                    }
                    $html = $output;
                }
            } else {
                $params['field_value'] = get_post_meta( $post->ID,
                    wpcf_types_get_meta_prefix( $field ) . $field['slug'], true );

                //  $html = types_render_field_single( $field, $params, $content, $code );
                if ( !empty( $params['field_value'] ) && $field['type'] != 'date' ) {
                    $html = types_render_field_single( $field, $params, $content,
                        $code );
                }
                if ( $field['type'] == 'date' && !empty( $params['field_value'] ) ) {
                    $html = types_render_field_single( $field, $params, $content,
                        $code );
                    if ( $field['data']['date_and_time'] == 'and_time' ) {
                        $html .= ' ' . date( "H", $params['field_value'] ) . ':' . date( "i",
                                $params['field_value'] );
                    }
                }
            }

            // API filter
            $wpcf->field->set( $post, $field );
            $field_value = $wpcf->field->html( $html, array() );
            $group_output .= '

	' . $field['name'] . '
    ' . $field_value . '

' . "\n\n";
        }
    }

    $group_output .= "\n\n

";
    if ( empty( $echo ) ) {
        echo $group_output;
    } else {
        $group_output .= "\n\n

";
        return $group_output;
    }

}

/**
 * Renders meta box content.
 *
 * @param $post
 * @param array $group
 * @param string $echo
 * @param bool $open_style_editor if true use code for open style editor when edit group
 *
 * @return string|void
 */
function wpcf_admin_post_meta_box( $post, $group, $echo = '', $open_style_editor = false )
{

    $field_group = Types_Field_Group_Post_Factory::load( wpcf_getnest( $group, array( 'args', 'slug' ) ) );
    // Field group will not exist when we're creating a new one.
    $group_wpml = ( null !== $field_group ) ? new Types_Wpml_Field_Group( $field_group ) : null;

    if (
        false === $open_style_editor
        && defined( 'WPTOOLSET_FORMS_VERSION' )
        && isset( $group['args']['html'] )
    ) {

        // Show group description if there's something to show.
        $description = wpcf_getnest( $group, array( 'args', 'description' ), '' );
        if ( ! empty( $description ) ) {
            $description = ( null !== $group_wpml ? $group_wpml->translate_description() : $description );

            echo '
';
            echo wpautop( $description );
            echo '
';
        }
        foreach ( $group['args']['html'] as $field ) {
            echo is_array( $field ) ? wptoolset_form_field( 'post',
                $field['config'], $field['meta'] ) : $field;
        }

        return;
    }

    global $wpcf;

    $post = wpcf_admin_create_fake_post_if_need( $post );

    static $nonce_added = false;
    $group_output = '';

    // A compatibility fix, maybe somehow related to user fields, maybe outdated.
    // Beware issues with non-existent (new) groups.
    if ( ! isset( $group['title'] ) ) {
        $temp = $group;
        $name = wpcf_getarr( $temp, 'name' );
        $group = array(
            'args' => $temp,
            'id' => (int) wpcf_getarr( $temp, 'slug' ),
            'title' => $name
        );
    }
    if ( !empty( $echo ) ) {
        $group_output = '
This Preview generated for latest post "' . $post->post_title . '"
' . "\n" .
            '' . "\n\n";
        $group_output .= '

' . $name . '

' . "\n";
    }

    /*
     * TODO Document where this is used
     */
    if ( !$nonce_added && empty( $echo ) ) {
        $nonce_action = 'update-' . $post->post_type . '_' . $post->ID;
        wp_nonce_field( $nonce_action, '_wpcf_post_wpnonce' );
        $nonce_added = true;
    }
    $group_output .= "\n\n" . '
' . "\n";
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if ( !empty( $group['args']['_conditional_display'] ) ) {
        if ( $group['args']['_conditional_display'] == 'failed' ) {
            $group_output .= '
';
        } else {
            $group_output .= '
';
        }
    }

    /*
     * TODO Move this into Field code
     * Process fields
     */
    if ( !empty( $group['args']['fields'] ) ) {
        // Display description
        if ( !empty( $group['args']['description'] ) ) {
            $group_output .= '
'
                . wpautop( $group_wpml->translate_description() ) . '
';
        }
        foreach ( $group['args']['fields'] as $field_slug => $field ) {
            if ( empty( $field ) || !is_array( $field ) ) {
                continue;
            }

            $field = $wpcf->field->_parse_cf_form_element( $field );

            if ( isset( $field['wpcf-type'] ) ) { // May be ignored
                $field = apply_filters( 'wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form_value_display',
                    $field );
            }

            if ( !isset( $field['#id'] ) ) {
                $field['#id'] = wpcf_unique_id( serialize( $field ) );
            }
            // Render form elements
            if (
                wpcf_compare_wp_version()
                && array_key_exists( '#type', $field )
                && 'wysiwyg' == $field['#type']
                && !isset( $field['#attributes']['disabled'] )
            ) {
                //                if ( isset( $field['#attributes']['disabled'] ) ) {
                //                    $field['#editor_settings']['tinymce'] = false;
                //                    $field['#editor_settings']['teeny'] = false;
                //                    $field['#editor_settings']['media_buttons'] = false;
                //                    $field['#editor_settings']['quicktags'] = false;
                //                    $field['#editor_settings']['dfw'] = false;
                //                }
                // Especially for WYSIWYG
                $group_output .= '
';
                $group_output .= '
';
                $group_output .= isset( $field['#before'] ) ? $field['#before'] : '';
                $group_output .= ''
                    . stripslashes( $field['#title'] ) . '';
                $group_output .= '

                    ' . wpautop( $field['#description'] ) . '
';
                ob_start();
                wp_editor( $field['#value'], $field['#id'],
                    $field['#editor_settings'] );
                $group_output .= ob_get_clean() . "\n\n";
                $field['slug'] = str_replace( WPCF_META_PREFIX . 'wysiwyg-', '',
                    $field_slug );
                $field['type'] = 'wysiwyg';
                $group_output .= '
';
                $group_output .= isset( $field['#after'] ) ? $field['#after'] : '';
                $group_output .= '
';
            } else {
                if (
                    array_key_exists( '#type', $field )
                    && 'wysiwyg' == $field['#type']
                ) {
                    $field['#type'] = 'textarea';
                }
                if ( !empty( $echo ) ) {
                    $field['#validate'] = '';
                }
                $group_output .= wpcf_form_simple( array($field['#id'] => $field) );
            }
            do_action( 'wpcf_fields_' . $field_slug . '_meta_box_form', $field );
            if ( isset( $field['wpcf-type'] ) ) { // May be ignored
                do_action( 'wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form', $field );
            }
        }
    }

    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if ( !empty( $group['args']['_conditional_display'] ) ) {
        $group_output .= '
';
    }

    $group_output .= '
';

    if ( !empty( $echo ) ) {
        $group_output .= "\n\n

";
        return $group_output;
    } else {
        echo $group_output;
    }
}

/**
 * The save_post hook.
 *
 * @param int $post_ID
 * @param WP_Post $post
 * @since unknown
 */
function wpcf_admin_post_save_post_hook( $post_ID, $post ) {

	// Apparently, some things are *slightly* different when saving a child post in the Post relationships metabox.
	// Ugly hack that will go away with m2m.
	//
	// Note: The types_updating_child_post filter is needed for a situation when user clicks on the Update button
	// for the parent post. In that case we don't get enough information to determine if a child post is being updated.
	$is_child_post_update = (
		( 'wpcf_ajax' == wpcf_getget( 'action' ) && 'pr_save_child_post' == wpcf_getget( 'wpcf_action' ) )
		|| apply_filters( 'types_updating_child_post', false )
	);

	global $wpcf;
	$errors = false;

	// Do not save post if is type of
	if ( in_array( $post->post_type,
		array(
			'revision',
			'view',
			'view-template',
			'cred-form',
			'cred-user-form',
			'nav_menu_item',
			'mediapage'
		) ) )
	{
		return;
	}

	$wpcf_form_data = wpcf_ensarr( wpcf_getarr( $_POST, 'wpcf' ) );

	// Check wpcf_adjust_form_input_for_checkboxlike_fields() for information about side effects.
	$wpcf_form_data = wpcf_adjust_form_input_for_checkboxlike_fields(
        $wpcf_form_data,
        wpcf_ensarr( wpcf_getarr( $_POST, '_wptoolset_checkbox' ) )
    );

	$wpcf_form_data = wpcf_adjust_form_input_for_checkboxlike_fields(
        $wpcf_form_data,
        wpcf_ensarr( wpcf_getarr( $_POST, '_wptoolset_radios' ) )
    );

	if ( count( $wpcf_form_data ) ) {
		$add_error_message = true;
		if ( isset( $_POST['post_id'] ) && $_POST['post_id'] != $post_ID ) {
			$add_error_message = false;
		}

		// check some attachment to delete
		$delete_attachments = apply_filters( 'wpcf_delete_attachments', true );
		$images_to_delete = array();
		if ( $delete_attachments && isset( $_POST['wpcf'] ) && array_key_exists( 'delete-image', $_POST['wpcf'] ) && is_array( $_POST['wpcf']['delete-image'] ) ) {
			foreach ( $_POST['wpcf']['delete-image'] as $image ) {
				$images_to_delete[ $image ] = 1;
			}
		}
		foreach ( $wpcf_form_data as $field_slug => $field_value ) {
			// Get field by slug
			$field = wpcf_fields_get_field_by_slug( $field_slug );
			if ( empty( $field ) ) {
				continue;
			}
			// Skip copied fields
			if ( isset( $_POST['wpcf_repetitive_copy'][ $field['slug'] ] ) ) {
				continue;
			}

			// This is (apparently) expected for repetitive fields...
			if ( $is_child_post_update && types_is_repetitive( $field ) ) {
				$field_value = array( $field_value );
			}

			$_field_value = ! types_is_repetitive( $field ) ? array( $field_value ) : $field_value;

			// Set config
			$config = wptoolset_form_filter_types_field( $field, $post_ID, $wpcf_form_data );

			// remove from images_to_delete if user add again
			if ( $delete_attachments && 'image' == $config['type'] ) {
				$images = $_field_value;
				if ( ! is_array( $images ) ) {
					$images = array( $images );
				}
				foreach ( $images as $image ) {
					if ( array_key_exists( $image, $images_to_delete ) ) {
						unset( $images_to_delete[ $image ] );
					}
				}
			}

			// add filter to remove field name from error message
			// This filter is toolset-common/toolset-forms/classes/class.validation.php
			add_filter( 'toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1 );
			foreach ( $_field_value as $_k => $_val ) {
				// Check if valid
				$validation = wptoolset_form_validate_field( 'post', $config, $_val );
				$conditional = wptoolset_form_conditional_check( $config );
				$not_valid = is_wp_error( $validation ) || ! $conditional;
				if ( $add_error_message && is_wp_error( $validation ) ) {
					$errors = true;
					$_errors = $validation->get_error_data();
					$_msg = sprintf( __( 'Field "%s" not updated:', 'wpcf' ), $field['name'] );
					wpcf_admin_message_store( $_msg . ' ' . implode( ', ', $_errors ), 'error' );
				}
				if ( $not_valid ) {
					if ( types_is_repetitive( $field ) ) {
						unset( $field_value[ $_k ] );
					} else {
						break;
					}
				}
			}
			remove_filter( 'toolset_common_validation_add_field_name_to_error', '__return_false', 1234 );

			// Save field
			if ( types_is_repetitive( $field ) ) {
				$wpcf->repeater->set( $post_ID, $field );
				$wpcf->repeater->save( $field_value );
			} else if ( ! $not_valid ) {
				$wpcf->field->set( $post_ID, $field );
				$wpcf->field->save( $field_value );
			}
			do_action( 'wpcf_post_field_saved', $post_ID, $field );

			// Note: Checkboxes fields used to be handled as a special case here, that was now moved
            // to wpcf_update_checkboxes_field() and is executed on *each* save_post by
            // wpcf_fields_checkbox_save_check().

		}

		// delete images
		if ( $delete_attachments && ! empty( $images_to_delete ) ) {
			$args = array(
				'post_parent' => $post_ID,
				'posts_per_page' => - 1,
			);
			$children_array = get_children( $args );
			foreach ( $children_array as $child ) {
				if ( ! array_key_exists( $child->guid, $images_to_delete ) ) {
					continue;
				}
				wp_delete_attachment( $child->ID );
			}
		}

	}
	if ( $errors ) {
		update_post_meta( $post_ID, '__wpcf-invalid-fields', true );
	}
	do_action( 'wpcf_post_saved', $post_ID );
}

/**
 *
 * Only for attachments, only default checkboxes!
 *
 * @internal breakpoint
 * @param type $post_ID
 * @param type $post
 */
function wpcf_admin_post_add_attachment_hook( $post_ID, $post )
{
    global $wpcf;
    /**
     * Basic check: only attachment
     */
    if ( 'attachment' !=  $post->post_type ) {
        return false;
    }

    /**
     * Get all groups connected to this $post
     */
    $groups = wpcf_admin_post_get_post_groups_fields( $post );
    if ( empty( $groups ) ) {
        return false;
    }
    $all_fields = array();
    $_not_valid = array();
    $_error = false;

    /**
     * Loop over each group
     *
     * TODO Document this
     * Connect 'wpcf-invalid-fields' with all fields
     */
    foreach ( $groups as $group ) {
        if ( isset( $group['fields'] ) ) {
            // Process fields
            $fields = wpcf_admin_post_process_fields( $post, $group['fields'],
                true, false, 'validation' );
            // Validate fields
            $form = wpcf_form_simple_validate( $fields );
            $all_fields = $all_fields + $fields;
            // Collect all not valid fields
            if ( $form->isError() ) {
                $_error = true; // Set error only to true
                $_not_valid = array_merge( $_not_valid,
                    (array) $form->get_not_valid() );
            }
        }
    }
    // Set fields
    foreach ( $all_fields as $k => $v ) {
        // only Types field
        if ( empty( $v['wpcf-id'] ) ) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set( $wpcf->post, $v['wpcf-id'] );
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ( $_not_valid as $k => $v ) {
        // only Types field
        if ( empty( $v['wpcf-id'] ) ) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set( $wpcf->post, $v['wpcf-id'] );
        $_not_valid[$k]['_field'] = $_temp;
    }

    /**
     * Process all checkbox fields
     */

    foreach ( $all_fields as $field ) {
        /**
         * only checkbox
         */
        if ( !isset( $field['#type'] ) || 'checkbox' != $field['#type'] ) {
            continue;
        }
        $field_data = wpcf_admin_fields_get_field( $field['wpcf-id'] );
        /**
         * check is checked for new!
         */
        $checked = array_key_exists( 'checked', $field_data['data'] ) && $field_data['data']['checked'];
        /**
         * do not save empty and not checked? fine, go away...
         */
        if ( 'no' == $field_data['data']['save_empty'] && !$checked ) {
            continue;
        }
        /**
         * all other just save...
         */
        $update_data = 0;
        if ( $checked ) {
            $update_data = $field_data['data']['set_value'];
        }
        add_post_meta( $post_ID, wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'], $update_data );
    }
    do_action( 'wpcf_attachement_add', $post_ID );
}
/**
 * Renders JS validation script.
 */
function wpcf_admin_post_js_validation() {
    wpcf_form_render_js_validation( '#post' );
}

/**
 * Creates form elements.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @param type $post
 * @param type $fields
 * @return type
 */
function wpcf_admin_post_process_fields( $post = false, $fields = array(),
        $use_cache = true, $add_to_editor = true, $context = 'group' ) {

    global $wpcf;

    $wpcf->field->use_cache = $use_cache;
    $wpcf->field->add_to_editor = $add_to_editor;
    $wpcf->repeater->use_cache = $use_cache;
    $wpcf->repeater->add_to_editor = $add_to_editor;

    // Get cached
    static $cache = array();
    $cache_key = !empty( $post->ID ) ? $post->ID . md5( serialize( $fields ) ) : false;
    if ( $use_cache && $cache_key && isset( $cache[$cache_key] ) ) {
        return $cache[$cache_key];
    }

    $fields_processed = array();

    // Get invalid fields (if submitted)
    $invalid_fields = array();
    if ( !empty( $post->ID ) ) {
        $invalid_fields = get_post_meta( $post->ID, 'wpcf-invalid-fields', true );
        delete_post_meta( $post->ID, 'wpcf-invalid-fields' );

        /*
         *
         * Add it to global $wpcf
         * From now on take it there.
         */
        $wpcf->field->invalid_fields = $invalid_fields;
    }

    // TODO WPML Get WPML original fields
    $original_cf = array();
    if ( function_exists( 'wpml_get_copied_fields_for_post_edit' ) && !wpcf_wpml_post_is_original( $post ) ) {
        $__fields_slugs = array();
        foreach ( $fields as $_f ) {
            $__fields_slugs[] = $_f['meta_key'];
        }
        $original_cf = wpml_get_copied_fields_for_post_edit( $__fields_slugs );
    }

    if( !empty( $fields ) ) {
        foreach( $fields as $field ) {

            // Repetitive fields
            if( wpcf_admin_is_repetitive( $field ) && $context != 'post_relationship' ) {
                // First check if repetitive fields are copied using WPML
                /*
				 * TODO All WPML specific code needs moving to
				 * /embedded/includes/wpml.php
				 *
				 * @since Types 1.2
				 */
                // TODO WPML move
                if( ! empty( $original_cf['fields'] ) && in_array( wpcf_types_get_meta_prefix( $field ) . $field['slug'],
                        $original_cf['fields'] )
                ) {
                    /*
					 * See if repeater can handle copied fields
					 */
                    $wpcf->repeater->set( get_post( $original_cf['original_post_id'] ),
                        $field );
                    $fields_processed = $fields_processed + $wpcf->repeater->get_fields_form();
                } else {
                    // Set repeater
                    /*
					 *
					 *
					 * @since Types 1.2
					 * Now we're using repeater class to handle repetitive forms.
					 * Main change is - use form from $field_meta_box_form() without
					 * re-processing form elements.
					 *
					 * Field should pass form as array:
					 * 'my_checkbox' => array('#type' => 'checkbox' ...),
					 * 'my_textfield' => array('#type' => 'textfield' ...),
					 *
					 * In form it should set values to be stored.
					 * Use hooks to adjust saved data.
					 */
                    $wpcf->repeater->set( $post, $field );
                    $fields_processed = $fields_processed + $wpcf->repeater->get_fields_form();
                }
                /*
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 *
				 * Non-repetitive fields
				 */
            } else {

                /*
				 * meta_form will be treated as normal form.
				 * See if any obstacles prevent us from using completed
				 * form from config files.
				 *
				 * Main change is - use form from $field_meta_box_form() without
				 * re-processing form elements.
				 *
				 * Field should pass form as array:
				 * 'my_checkbox' => array('#type' => 'checkbox' ...),
				 * 'my_textfield' => array('#type' => 'textfield' ...),
				 *
				 * In form it should set values to be stored.
				 * Use hooks to adjust saved data.
				 */
                $wpcf->field->set( $post, $field );

                // TODO WPML move Check if repetitive field is copied using WPML
                if( ! empty( $original_cf['fields'] ) ) {
                    if( in_array( $wpcf->field->slug, $original_cf['fields'] ) ) {
                        // Switch to parent post
                        $wpcf->field->set( get_post( $original_cf['original_post_id'] ),
                            $field );
                    }
                }
                /*
				 * From Types 1.2 use complete form setup
				 */
                $fields_processed = $fields_processed + $wpcf->field->_get_meta_form();
            }
        }
    }

    // Cache results
    if ( $cache_key ) {
        $cache[$cache_key] = $fields_processed;
    }

    return $fields_processed;
}

/**
 * Processes single field.
 *
 * Since Types 1.2 this function changed. It handles single form element.
 * Form element is already fetched, also meta values using class WPCF_Field.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @todo gradually remove usage of inherited fields
 * @todo Cleanup
 *
 * @staticvar array $repetitive_started
 * @param type $field_object
 * @return mixed boolean|array
 */
function wpcf_admin_post_process_field( $field_object ) {

    /*
     * Since Types 1.2
     * All data we need is stored in global $wpcf
     */
    global $wpcf;
    $post = $wpcf->post;
    $field = (array) $field_object->cf;
    $context = $field_object->context;
    $invalid_fields = $wpcf->field->invalid_fields;

    if ( !empty( $field ) ) {
        /*
         * Set Unique ID
         */
        $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-'
                . wpcf_unique_id( serialize( $field_object->__current_form_element ) );

        /*
         * Get inherited field
         *
         * TODO Deprecated
         *
         * Since Types 1.2 we encourage developers to completely define fields.
         */
        $inherited_field_data = false;
        if ( isset( $field_object->config->inherited_field_type ) ) {
            $_allowed = array(
                'image' => 'file',
                'numeric' => 'textfield',
                'email' => 'textfield',
                'phone' => 'textfield',
                'url' => 'textfield',
            );
            if ( !array_key_exists( $field_object->cf['type'], $_allowed ) ) {
//                _deprecated_argument( 'inherited_field_type', '1.2',
//                        'Since Types 1.2 we encourage developers to completely define fields' );
            }
            $inherited_field_data = wpcf_fields_type_action( $field_object->config->inherited_field_type );
        }

        /*
         * CHECKPOINT
         * APPLY FILTERS
         *
         *
         * Moved to WPCF_Field
         * Field value should be already filtered
         *
         * Explanation:
         * When WPCF_Field::set() is called, all these properties are set.
         * WPCF_Field::$cf['value']
         * WPCF_Field::$__meta (single value from DB)
         * WPCF_Field::$meta (single or multiple values if single/repetitive)
         *
         * TODO Make sure value is already filtered and not overwritten
         */

        /*
         * Set generic values
         *
         * FUTURE BREAKPOINT
         * Since Types 1.2 we do not encourage relying on generic field data.
         * Only core fields should use this.
         *
         * TODO Open 3rd party fields dir
         */
        $_element = array(
            '#type' => isset( $field_object->config->inherited_field_type ) ? $field_object->config->inherited_field_type : $field['type'],
            '#id' => $field_id,
            '#title' => $field['name'],
            '#name' => 'wpcf[' . $field['slug'] . ']',
            '#value' => isset( $field['value'] ) ? $field['value'] : '',
            'wpcf-id' => $field['id'],
            'wpcf-slug' => $field['slug'],
            'wpcf-type' => $field['type'],
        );

        /*
         * TODO Add explanation about creating duplicated fields
         *
         * NOT USED YET
         *
         * Explain users that fields are added if slug is changed
         */
        wpcf_admin_add_js_settings( 'wpcfFieldNewInstanceWarning',
                __( 'If you change slug, new field will be created', 'wpcf' ) );

        /*
         * Merge with default element
         *
         * Deprecated from Types 1.2
         * Only core fields use this.
         */
        $element = array_merge( $_element, $field_object->__current_form_element );

        /*
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         *
         * TODO From this point code should be simplified.
         */

        if ( isset( $field['description_extra'] ) ) {
            $element['#description'] .= wpautop( $field['description_extra'] );
        }

        // Set atributes #1
        if ( isset( $field['disable'] ) ) {
            $field['#disable'] = $field['disable'];
        }
        if ( !empty( $field['disable'] ) ) {
            $field['#attributes']['disabled'] = 'disabled';
        }
        if ( !empty( $field['readonly'] ) ) {
            $field['#attributes']['readonly'] = 'readonly';
        }

        // Format description
        if ( !empty( $element['#description'] ) ) {
            $element['#description'] = wpautop( $element['#description'] );
        }

        // Set validation element
        if ( isset( $field['data']['validate'] ) ) {
            /*
             *
             *
             * TODO First two check are not needed anymore
             */
            // If array has more than one field - see which one is marked
            if ( $field_object->__multiple && isset( $element['#_validate_this'] ) ) {
                $element['#validate'] = $field['data']['validate'];
            } else if ( !$field_object->__multiple ) {
                $element['#validate'] = $field['data']['validate'];
            }
        }

        // Set atributes #2 (override)
        if ( isset( $field['disable'] ) ) {
            $element['#disable'] = $field['disable'];
        }
        if ( !empty( $field['disable'] ) ) {
            $element['#attributes']['disabled'] = 'disabled';
        }
        if ( !empty( $field['readonly'] ) ) {
            $element['#attributes']['readonly'] = 'readonly';
            if ( !empty( $element['#options'] ) ) {
                foreach ( $element['#options'] as $key => $option ) {
                    if ( !is_array( $option ) ) {
                        $element['#options'][$key] = array(
                            '#title' => $key,
                            '#value' => $option,
                        );
                    }
                    $element['#options'][$key]['#attributes']['readonly'] = 'readonly';
                    if ( $element['#type'] == 'select' ) {
                        $element['#options'][$key]['#attributes']['disabled'] = 'disabled';
                    }
                }
            }
            if ( $element['#type'] == 'select' ) {
                $element['#attributes']['disabled'] = 'disabled';
            }
        }

        // Check if it was invalid on submit and add error message
        if ( $post && !empty( $invalid_fields ) ) {
            if ( isset( $invalid_fields[$element['#id']]['#error'] ) ) {
                $element['#error'] = $invalid_fields[$element['#id']]['#error'];
            }
        }

        // TODO WPML move Set WPML locked icon
        if ( wpcf_wpml_field_is_copied( $field ) ) {
            $element['#title'] .= '';
        }

        // Add repetitive class
        // TODO WPML move
        if ( types_is_repetitive( $field ) && $context != 'post_relationship' && wpcf_wpml_field_is_copied( $field ) ) {
            if ( !empty( $element['#options'] ) && $element['#type'] != 'select' ) {
                foreach ( $element['#options'] as $temp_key => $temp_value ) {
                    $element['#options'][$temp_key]['#attributes']['class'] = isset( $element['#attributes']['class'] ) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
                }
            } else {
                $element['#attributes']['class'] = isset( $element['#attributes']['class'] ) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive';
            }
            /*
             *
             *
             * Since Types 1.2 we allow same field values
             *
             * TODO Remove
             *
             * wpcf_admin_add_js_settings('wpcfFormRepetitiveUniqueValuesCheckText',
              '\'' . __('Warning: same values set', 'wpcf') . '\'');
             */
        }

        // Set read-only if copied by WPML
        // TODO WPML Move this to separate WPML code and use only hooks 1.1.5
        if ( wpcf_wpml_field_is_copied( $field ) ) {
            if ( isset( $element['#options'] ) ) {
                foreach ( $element['#options'] as $temp_key => $temp_value ) {
                    if ( isset( $temp_value['#attributes'] ) ) {
                        $element['#options'][$temp_key]['#attributes']['readonly'] = 'readonly';
                    } else {
                        $element['#options'][$temp_key]['#attributes'] = array('readonly' => 'readonly');
                    }
                }
            }
            if ( $field['type'] == 'select' ) {
                if ( isset( $element['#attributes'] ) ) {
                    $element['#attributes']['disabled'] = 'disabled';
                } else {
                    $element['#attributes'] = array('disabled' => 'disabled');
                }
            } else {
                if ( isset( $element['#attributes'] ) ) {
                    $element['#attributes']['readonly'] = 'readonly';
                } else {
                    $element['#attributes'] = array('readonly' => 'readonly');
                }
            }
        }

        // Final filter for disabled if readonly
        if ( isset( $element['#attributes']['readonly'] ) || isset( $element['#attributes']['disabled'] ) ) {
            if ( types_is_repetitive( $field ) ) {
                $element['#name'] .= '[]';
            }
            if ( $field['type'] == 'checkboxes' ) {
                if ( isset( $element['#options'] ) ) {
                    foreach ( $element['#options'] as $temp_key => $temp_value ) {
                        $value = isset( $temp_value['#default_value'] ) ? $temp_value['#default_value'] : $temp_value['#value'];
                        $_after = "<input type=\"hidden\" name=\"{$temp_value['#name']}\" value=\"{$value}\" />";
                        $temp_value['#after'] = isset( $temp_value['#after'] ) ? $temp_value['#after'] . $_after : $_after;
                        $temp_value['#name'] = "wpcf-disabled[{$field['id']}_{$temp_value['#id']}]";
                        $temp_value['#attributes']['disabled'] = 'disabled';
                        $element['#options'][$temp_key] = $temp_value;
                    }
                }
            } else if ( in_array( $element['#type'],
                            array('checkbox', 'checkboxes', 'radios') ) ) {
                if ( isset( $element['#options'] ) ) {
                    foreach ( $element['#options'] as $temp_key => $temp_value ) {
                        $element['#options'][$temp_key]['#attributes']['disabled'] = 'disabled';
                    }
                }
                $value = isset( $element['#default_value'] ) ? $element['#default_value'] : $element['#value'];
                $_after = "<input type=\"hidden\" name=\"{$element['#name']}\" value=\"{$value}\" />";
                $element['#after'] = isset( $element['#after'] ) ? $element['#after'] . $_after : $_after;
                $element['#attributes']['disabled'] = 'disabled';
                $element['#name'] = "wpcf-disabled[{$field['id']}_{$element['#id']}]";
            } else {
                $element['#attributes']['disabled'] = 'disabled';
                if ( is_array( $element['#value'] ) ) {//$field['type'] == 'skype' ) {
                    $element['#value'] = array_shift( $element['#value'] );
                }
                $value = htmlentities( $element['#value'] );
                $_after = "<input type=\"hidden\" name=\"{$element['#name']}\" value=\"{$value}\" />";
                $element['#after'] = isset( $element['#after'] ) ? $element['#after'] . $_after : $_after;
                $element['#name'] = "wpcf-disabled[{$field['id']}_{$element['#id']}]";
            }
        }
        return array('field' => $field, 'element' => $element);
    }
    return false;
}

/**
 * Gets all groups and fields for post.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @param type $post_ID
 * @return type
 */
function wpcf_admin_post_get_post_groups_fields( $post = false, $context = 'group' )
{
    // Get post_type
    /*
     *
     *
     * Since WP 3.5 | Types 1.2
     * Looks like $post is altered with get_post_type()
     * We do not want that if post is already set
     */
    if ( !empty( $post->post_type ) ) {
        $post_type = $post->post_type;
    } else if ( !empty( $post ) ) {
        $post_type = get_post_type( $post );
    } else {
        if ( !isset( $_GET['post_type'] ) ) {
            $post_type = 'post';
        } else if ( in_array( $_GET['post_type'], get_post_types( array('show_ui' => true) ) ) ) {
            $post_type = sanitize_text_field( $_GET['post_type'] );
        } else {
            $post_type = 'post';
        }
    }

    // Get post terms
    $support_terms = false;
    if ( !empty( $post ) ) {
        $post->_wpcf_post_terms = array();
        $taxonomies = get_taxonomies( '', 'objects' );
        if ( !empty( $taxonomies ) ) {
            foreach ( $taxonomies as $tax_slug => $tax ) {
                $temp_tax = get_taxonomy( $tax_slug );
                if ( !in_array( $post_type, $temp_tax->object_type ) ) {
                    continue;
                }
                $support_terms = true;
                $terms = wp_get_post_terms( $post->ID, $tax_slug,
                        array('fields' => 'all') );
                foreach ( $terms as $term_id ) {
                    $post->_wpcf_post_terms[] = $term_id->term_taxonomy_id;
                }
            }
        }
    }

    // Get post template
    if ( empty( $post ) ) {
        $post = new stdClass();
        $post->_wpcf_post_template = false;
        $post->_wpcf_post_views_template = false;
    } else {
        $post->_wpcf_post_template = get_post_meta( $post->ID, '_wp_page_template', true );
        $post->_wpcf_post_views_template = get_post_meta( $post->ID, '_views_template', true );
    }

    if ( empty( $post->_wpcf_post_terms ) ) {
        $post->_wpcf_post_terms = array();
    }

    // Filter groups
    $groups = array();
    $groups_all = apply_filters( 'wpcf_post_groups_all', wpcf_admin_fields_get_groups(), $post, $context );
    foreach ( $groups_all as $temp_key => $temp_group ) {
        if ( empty( $temp_group['is_active'] ) ) {
            unset( $groups_all[$temp_key] );
            continue;
        }
        // Get filters
        // Post Types
        $groups_all[$temp_key]['_wp_types_group_post_types'] = explode( ',',
                trim( get_post_meta( $temp_group['id'],
                                '_wp_types_group_post_types', true ), ',' ) );

        // Taxonomies
        $groups_all[$temp_key]['_wp_types_group_terms'] = explode( ',',
                trim( get_post_meta( $temp_group['id'], '_wp_types_group_terms',
                                true ), ',' ) );

        // Templates
        $groups_all[$temp_key]['_wp_types_group_templates'] = explode( ',',
                trim( get_post_meta( $temp_group['id'],
                                '_wp_types_group_templates', true ), ',' ) );

        // Data-Dependant
        $groups_all[$temp_key]['_wpcf_conditional_display'] =
            get_post_meta( $temp_group['id'], '_wpcf_conditional_display' );

        $post_type_filter = $groups_all[$temp_key]['_wp_types_group_post_types'][0] == 'all'
                            || empty( $groups_all[$temp_key]['_wp_types_group_post_types'][0] )
            ? -1
            : 0;
        $taxonomy_filter = $groups_all[$temp_key]['_wp_types_group_terms'][0] == 'all'
                            || empty( $groups_all[$temp_key]['_wp_types_group_terms'][0] )
            ? -1
            : 0;
        $template_filter = $groups_all[$temp_key]['_wp_types_group_templates'][0] == 'all'
                           || empty( $groups_all[$temp_key]['_wp_types_group_templates'][0] )
            ? -1
            : 0;

        $groups_all[$temp_key] = apply_filters( 'wpcf_post_group_filter_settings', $groups_all[$temp_key], $post, $context, $post->_wpcf_post_terms );

        // See if post type matches
        if ( $post_type_filter == 0 && in_array( $post_type,
                $groups_all[$temp_key]['_wp_types_group_post_types'] ) ) {
            $post_type_filter = 1;
        }

        // See if terms match
        if ( $taxonomy_filter == 0 ) {
            foreach ( $post->_wpcf_post_terms as $temp_post_term ) {
                if ( in_array( $temp_post_term,
                    $groups_all[$temp_key]['_wp_types_group_terms'] ) ) {
                    $taxonomy_filter = 1;
                }
            }
        }

        // See if template match
        if ( $template_filter == 0 ) {
            if ( (!empty( $post->_wpcf_post_template ) && in_array( $post->_wpcf_post_template,
                        $groups_all[$temp_key]['_wp_types_group_templates'] )) || (!empty( $post->_wpcf_post_views_template ) && in_array( $post->_wpcf_post_views_template,
                        $groups_all[$temp_key]['_wp_types_group_templates'] )) ) {
                $template_filter = 1;
            }
        }

        /**
         * if ALL must met
         */
        if( isset( $groups_all[$temp_key]['filters_association'] )
            && $groups_all[$temp_key]['filters_association'] == 'all' ) {

            // if no conditions are set, do not display the group
            // - this is important because ounce filter association is set to "all"
            //   it's not unset even if the user removes all conditions
            if( $post_type_filter == 0
                && $template_filter == 0
                && $taxonomy_filter == -1
                && empty( $groups_all[$temp_key]['_wpcf_conditional_display'] ) ) {
                unset( $groups_all[$temp_key] );

            // there are conditions set
            } else {
                // post types
                // accepted if none isset || one has to match (proofed above)
                if( empty( $groups_all[$temp_key]['_wp_types_group_post_types'][0] ) )
                    $post_type_filter = 1;

                // taxonomies
                // none isset || all have to match
                if( empty( $groups_all[$temp_key]['_wp_types_group_terms'][0] )
                    || $groups_all[$temp_key]['_wp_types_group_terms'][0] == 'all' ) {
                    $taxonomy_filter = 1;
                } else {
                    $taxonomy_filter = 1;
                    // check all terms which need to be active
                    foreach ( $groups_all[$temp_key]['_wp_types_group_terms'] as $term_has_to_match ) {
                        // break on first term not active
                        if( ! in_array( $term_has_to_match, $post->_wpcf_post_terms ) ) {
                            $taxonomy_filter = 0;
                            break;
                        }
                    }
                }

                // templates
                // one has to match  (proofed above) || none isset
                if( empty( $groups_all[$temp_key]['_wp_types_group_templates'][0] ) )
                    $template_filter = 1;
            }
        }

        // Filter by association
        if ( empty( $groups_all[$temp_key]['filters_association'] ) ) {
            $groups_all[$temp_key]['filters_association'] = 'any';
        }
        // If context is post_relationship allow all groups that match post type
        if ( $context == 'post_relationships_header' ) {
            $groups_all[$temp_key]['filters_association'] = 'any';
        }

        if ( $post_type_filter == -1 && $taxonomy_filter == -1 && $template_filter == -1 ) {
            $passed = 1;
        } else if ( $groups_all[$temp_key]['filters_association'] == 'any' ) {
            $passed = $post_type_filter == 1 || $taxonomy_filter == 1 || $template_filter == 1;
        } else {
            $passed = $post_type_filter != 0 && $taxonomy_filter != 0 && $template_filter != 0;
        }
        if ( !$passed ) {
            unset( $groups_all[$temp_key] );
        } else {
            $groups_all[$temp_key]['fields'] = wpcf_admin_fields_get_fields_by_group( $temp_group['id'],
                    'slug', true, false, true );
        }
    }
    $groups = apply_filters( 'wpcf_post_groups', $groups_all, $post, $context );
    return $groups;
}

/**
 * Stores fields for editor menu.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @staticvar array $fields
 * @param type $field
 * @return array
 */
function wpcf_admin_post_add_to_editor( $field ) {
    static $fields = array();
    if ( $field == 'get' ) {
        return $fields;
    }
    if (
		empty( $fields )
		&& ! (
			apply_filters( 'toolset_is_views_available', false )
			|| apply_filters( 'toolset_is_views_embedded_available', false )
		)
	) {
        add_action( 'admin_enqueue_scripts', 'wpcf_admin_post_add_to_editor_js' );
    }
    $fields[$field['id']] = $field;
}

/**
 * Renders JS for editor menu.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @return type
 */
function wpcf_admin_post_add_to_editor_js() {

    // since 1.3 we do not use global $post
    $post = wpcf_admin_get_edited_post();
    if ( empty( $post ) ) {
        $post = (object) array('ID' => -1);
        $post->post_type = false;
    }

    $fields = wpcf_admin_post_add_to_editor( 'get' );
    $groups = wpcf_admin_post_get_post_groups_fields( $post );

    if ( empty( $fields ) || empty( $groups ) ) {
        /**
         * check user fields too, but only when CF are empty
         */
        include_once dirname(__FILE__).'/usermeta-post.php';
        $groups = wpcf_admin_usermeta_get_groups_fields();
        if ( empty( $groups ) ) {
            return false;
        }
    }
    $editor_addon = new Editor_addon( 'types',
            __( 'Insert Types Shortcode', 'wpcf' ),
            WPCF_EMBEDDED_RES_RELPATH . '/js/types_editor_plugin.js',
            '', true, 'icon-types-logo ont-icon-18 ont-color-gray' );

    foreach ( $groups as $group ) {
        if ( empty( $group['fields'] ) ) {
            continue;
        }
        foreach ( $group['fields'] as $group_field_id => $group_field ) {
            if ( !isset( $fields[$group_field_id] ) ) {
                continue;
            }
            $field = $fields[$group_field_id];
            $callback = 'wpcfFieldsEditorCallback(\'' . $field['id']
                    . '\', \'postmeta\', ' . $post->ID . ')';

            $editor_addon->add_insert_shortcode_menu( stripslashes( $field['name'] ),
                    trim( wpcf_fields_get_shortcode( $field ), '[]' ),
                    $group['name'], $callback );
        }
    }
}

/**
 * Adds items to view dropdown.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @todo Remove (to WPCF_WPViews)
 *
 * @param type $menu
 * @return type
 */
function wpcf_admin_post_editor_addon_menus_filter( $menu ) {

    global $wpcf;

    $post = wpcf_admin_get_edited_post();
    if ( empty( $post ) ) {
        $post = (object) array('ID' => -1);
    }

    $groups = wpcf_admin_fields_get_groups( TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'group_active' );
    $all_post_types = implode( ' ', get_post_types( array('public' => true) ) );
    $add = array();
    if ( !empty( $groups ) ) {
        // $group_id is blank therefore not equal to $group['id']
        // use array for item key and CSS class
        $item_styles = array();

        foreach ( $groups as $group_id => $group ) {
            $fields = wpcf_admin_fields_get_fields_by_group( $group['id'],
                    'slug', true, false, true );
            if ( !empty( $fields ) ) {
                // code from Types used here without breaking the flow
                // get post types list for every group or apply all
                $post_types = get_post_meta( $group['id'],
                        '_wp_types_group_post_types', true );
                if ( $post_types == 'all' ) {
                    $post_types = $all_post_types;
                }
                $post_types = trim( str_replace( ',', ' ', $post_types ) );
                $item_styles[$group['name']] = $post_types;

                foreach ( $fields as $field_id => $field ) {

                    // Use field class
                    $wpcf->field->set( $wpcf->post, $field );

                    // Get field data
                    $data = (array) $wpcf->field->config;
                    // Get inherited field
                    if ( isset( $data['inherited_field_type'] ) ) {
                        $inherited_field_data = wpcf_fields_type_action( $data['inherited_field_type'] );
                    }

                    $callback = 'wpcfFieldsEditorCallback(\'' . $field['id']
                            . '\', \'postmeta\', ' . $post->ID . ')';

                    $menu[$group['name']][stripslashes( $field['name'] )] = array(
                        stripslashes( $field['name'] ), trim( wpcf_fields_get_shortcode( $field ),
                                '[]' ), $group['name'], $callback);

                    /*
                     * Since Types 1.2
                     * We use field class to enqueue JS and CSS
                     */
                    $wpcf->field->enqueue_script();
                    $wpcf->field->enqueue_style();
                }
            }
        }
    }

    return $menu;
}

function wpcf_admin_post_marketing_displaying_custom_content() {
    $displaying_custom_content = include( WPCF_ABSPATH . '/marketing/displaying-custom-content/title-content.php' );
    echo $displaying_custom_content['content'];
}

function wpcf_post_preview_warning() {
    $post = wpcf_admin_get_edited_post();
    // Add preview warning
    if ( isset( $post->post_status ) && !in_array( $post->post_status,
                    array('auto-draft', 'draft') ) && !in_array( $post->post_type,
                    array('cred', 'view', 'view-template') ) ) {
        wp_enqueue_style( 'wp-pointer' );
        wp_enqueue_script( 'wp-pointer' );

        ?><?php
    }
}

/**
 * Create fake post object.
 *
 * Create fake post object if in system is no post.
 *
 * @param mixed $post
 * @return object
 *
 */
function wpcf_admin_create_fake_post_if_need($post)
{
    if ( !is_object( $post ) ) {
        $post = new stdClass();
        $post->post_title = 'Lorem Ipsum';
        $post->ID = 0;
    }
    return $post;
}


text:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;

namespace NPOI.OpenXmlFormats.Spreadsheet
{
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_Metadata
    {

        private CT_MetadataTypes metadataTypesField;

        private CT_MetadataStrings metadataStringsField;

        private CT_MdxMetadata mdxMetadataField;

        private List<CT_FutureMetadata> futureMetadataField;

        private CT_MetadataBlocks cellMetadataField;

        private CT_MetadataBlocks valueMetadataField;

        private CT_ExtensionList extLstField;

        public CT_Metadata()
        {
            this.extLstField = new CT_ExtensionList();
            this.valueMetadataField = new CT_MetadataBlocks();
            this.cellMetadataField = new CT_MetadataBlocks();
            this.futureMetadataField = new List<CT_FutureMetadata>();
            this.mdxMetadataField = new CT_MdxMetadata();
            this.metadataStringsField = new CT_MetadataStrings();
            this.metadataTypesField = new CT_MetadataTypes();
        }

        public CT_MetadataTypes metadataTypes
        {
            get
            {
                return this.metadataTypesField;
            }
            set
            {
                this.metadataTypesField = value;
            }
        }

        public CT_MetadataStrings metadataStrings
        {
            get
            {
                return this.metadataStringsField;
            }
            set
            {
                this.metadataStringsField = value;
            }
        }

        public CT_MdxMetadata mdxMetadata
        {
            get
            {
                return this.mdxMetadataField;
            }
            set
            {
                this.mdxMetadataField = value;
            }
        }

        public List<CT_FutureMetadata> futureMetadata
        {
            get
            {
                return this.futureMetadataField;
            }
            set
            {
                this.futureMetadataField = value;
            }
        }

        public CT_MetadataBlocks cellMetadata
        {
            get
            {
                return this.cellMetadataField;
            }
            set
            {
                this.cellMetadataField = value;
            }
        }

        public CT_MetadataBlocks valueMetadata
        {
            get
            {
                return this.valueMetadataField;
            }
            set
            {
                this.valueMetadataField = value;
            }
        }

        public CT_ExtensionList extLst
        {
            get
            {
                return this.extLstField;
            }
            set
            {
                this.extLstField = value;
            }
        }
    }

    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_MetadataTypes
    {

        private List<CT_MetadataType> metadataTypeField;

        private uint countField;

        public CT_MetadataTypes()
        {
            this.metadataTypeField = new List<CT_MetadataType>();
            this.countField = ((uint)(0));
        }
        [XmlElement("metadataType")]
        public List<CT_MetadataType> metadataType
        {
            get
            {
                return this.metadataTypeField;
            }
            set
            {
                this.metadataTypeField = value;
            }
        }
        [XmlAttribute]
        [DefaultValueAttribute(typeof(uint), "0")]
        public uint count
        {
            get
            {
                return this.countField;
            }
            set
            {
                this.countField = value;
            }
        }
    }
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]
    public class CT_MetadataType
    {

        private string nameField;

        private uint minSupportedVersionField;

        private bool ghostRowField;

        private bool ghostColField;

        private bool editField;

        private bool deleteField;

        private bool copyField;

        private bool pasteAllField;

        private bool pasteFormulasField;

        private bool pasteValuesField;

        private bool pasteFormatsField;

        private bool pasteCommentsField;

        private bool pasteDataValidationField;

        private bool pasteBordersField;

        private bool pasteColWidthsField;

        private bool pasteNumberFormatsField;

        private bool mergeField;

        private bool splitFirstField;

        private bool splitAllField;

        private bool rowColShiftField;

        private bool clearAllField;

        private bool clearFormatsField;

        private bool clearContentsField;

        private bool clearCommentsField;

        private bool assignField;

        private bool coerceField;

        private bool adjustField;

        private bool cellMetaField;

        public CT_MetadataType()
        {
            this.ghostRowField = false;
            this.ghostColField = false;
            this.editField = false;
            this.deleteField = false;
            this.copyField = false;
            this.pasteAllField = false;
            this.pasteFormulasField = false;
            this.pasteValuesField = false;
            this.pasteFormatsField = false;
            this.pasteCommentsField = false;
            this.pasteDataValidationField = false;
            this.pasteBordersField = false;
            this.pasteColWidthsField = false;
            this.pasteNumberFormatsField = false;
            this.mergeField = false;
            this.splitFirstField = false;
            this.splitAllField = false;
            this.rowColShiftField = false;
            this.clearAllField = false;
            this.clearFormatsField = false;
            this.clearContentsField = false;
            this.clearCommentsField = false;
            this.assignField = false;
            this.coerceField = false;
            this.adjustField = false;
            this.cellMetaField = false;
        }

        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        public uint minSupportedVersion
        {
            get
            {
                return this.minSupportedVersionField;
            }
            set
            {
                this.minSupportedVersionField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool ghostRow
        {
            get
            {
                return this.ghostRowField;
            }
            set
            {
                this.ghostRowField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool ghostCol
        {
            get
            {
                return this.ghostColField;
            }
            set
            {
                this.ghostColField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool edit
        {
            get
            {
                return this.editField;
            }
            set
            {
                this.editField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool delete
        {
            get
            {
                return this.deleteField;
            }
            set
            {
                this.deleteField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool copy
        {
            get
            {
                return this.copyField;
            }
            set
            {
                this.copyField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteAll
        {
            get
            {
                return this.pasteAllField;
            }
            set
            {
                this.pasteAllField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteFormulas
        {
            get
            {
                return this.pasteFormulasField;
            }
            set
            {
                this.pasteFormulasField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteValues
        {
            get
            {
                return this.pasteValuesField;
            }
            set
            {
                this.pasteValuesField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteFormats
        {
            get
            {
                return this.pasteFormatsField;
            }
            set
            {
                this.pasteFormatsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteComments
        {
            get
            {
                return this.pasteCommentsField;
            }
            set
            {
                this.pasteCommentsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteDataValidation
        {
            get
            {
                return this.pasteDataValidationField;
            }
            set
            {
                this.pasteDataValidationField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteBorders
        {
            get
            {
                return this.pasteBordersField;
            }
            set
            {
                this.pasteBordersField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteColWidths
        {
            get
            {
                return this.pasteColWidthsField;
            }
            set
            {
                this.pasteColWidthsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool pasteNumberFormats
        {
            get
            {
                return this.pasteNumberFormatsField;
            }
            set
            {
                this.pasteNumberFormatsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool merge
        {
            get
            {
                return this.mergeField;
            }
            set
            {
                this.mergeField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool splitFirst
        {
            get
            {
                return this.splitFirstField;
            }
            set
            {
                this.splitFirstField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool splitAll
        {
            get
            {
                return this.splitAllField;
            }
            set
            {
                this.splitAllField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool rowColShift
        {
            get
            {
                return this.rowColShiftField;
            }
            set
            {
                this.rowColShiftField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool clearAll
        {
            get
            {
                return this.clearAllField;
            }
            set
            {
                this.clearAllField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool clearFormats
        {
            get
            {
                return this.clearFormatsField;
            }
            set
            {
                this.clearFormatsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool clearContents
        {
            get
            {
                return this.clearContentsField;
            }
            set
            {
                this.clearContentsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool clearComments
        {
            get
            {
                return this.clearCommentsField;
            }
            set
            {
                this.clearCommentsField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool assign
        {
            get
            {
                return this.assignField;
            }
            set
            {
                this.assignField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool coerce
        {
            get
            {
                return this.coerceField;
            }
            set
            {
                this.coerceField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool adjust
        {
            get
            {
                return this.adjustField;
            }
            set
            {
                this.adjustField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool cellMeta
        {
            get
            {
                return this.cellMetaField;
            }
            set
            {
                this.cellMetaField = value;
            }
        }
    }

    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]
    public class CT_MetadataRecord
    {

        private uint tField;

        private uint vField;
        [XmlAttribute]
        public uint t
        {
            get
            {
                return this.tField;
            }
            set
            {
                this.tField = value;
            }
        }
        [XmlAttribute]
        public uint v
        {
            get
            {
                return this.vField;
            }
            set
            {
                this.vField = value;
            }
        }
    }
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_MetadataBlocks
    {

        private List<CT_MetadataRecord> bkField;

        private uint countField;

        public CT_MetadataBlocks()
        {
            this.bkField = new List<CT_MetadataRecord>();
            this.countField = ((uint)(0));
        }

        [XmlArray(Order = 0)]
        [XmlArrayItem("rc", typeof(CT_MetadataRecord), IsNullable = false)]
        public List<CT_MetadataRecord> bk
        {
            get
            {
                return this.bkField;
            }
            set
            {
                this.bkField = value;
            }
        }
        [XmlAttribute]
        [DefaultValueAttribute(typeof(uint), "0")]
        public uint count
        {
            get
            {
                return this.countField;
            }
            set
            {
                this.countField = value;
            }
        }
    }
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_FutureMetadataBlock
    {

        private CT_ExtensionList extLstField;

        public CT_FutureMetadataBlock()
        {
            this.extLstField = new CT_ExtensionList();
        }

        public CT_ExtensionList extLst
        {
            get
            {
                return this.extLstField;
            }
            set
            {
                this.extLstField = value;
            }
        }
    }

    public class CT_FutureMetadata
    {

        private List<CT_FutureMetadataBlock> bkField;

        private CT_ExtensionList extLstField;

        private string nameField;

        private uint countField;

        public CT_FutureMetadata()
        {
            this.extLstField = new CT_ExtensionList();
            this.bkField = new List<CT_FutureMetadataBlock>();
            this.countField = ((uint)(0));
        }
        [XmlElement("bk")]
        public List<CT_FutureMetadataBlock> bk
        {
            get
            {
                return this.bkField;
            }
            set
            {
                this.bkField = value;
            }
        }

        public CT_ExtensionList extLst
        {
            get
            {
                return this.extLstField;
            }
            set
            {
                this.extLstField = value;
            }
        }
        [XmlAttribute]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
        [XmlAttribute]
        [DefaultValueAttribute(typeof(uint), "0")]
        public uint count
        {
            get
            {
                return this.countField;
            }
            set
            {
                this.countField = value;
            }
        }
    }

    public class CT_MdxKPI
    {

        private uint nField;

        private uint npField;

        private ST_MdxKPIProperty pField;

        public uint n
        {
            get
            {
                return this.nField;
            }
            set
            {
                this.nField = value;
            }
        }

        public uint np
        {
            get
            {
                return this.npField;
            }
            set
            {
                this.npField = value;
            }
        }

        public ST_MdxKPIProperty p
        {
            get
            {
                return this.pField;
            }
            set
            {
                this.pField = value;
            }
        }
    }

    public enum ST_MdxKPIProperty
    {

        v,

        g,

        s,

        t,

        w,

        m,
    }

    public class CT_MdxMemeberProp
    {

        private uint nField;

        private uint npField;

        public uint n
        {
            get
            {
                return this.nField;
            }
            set
            {
                this.nField = value;
            }
        }

        public uint np
        {
            get
            {
                return this.npField;
            }
            set
            {
                this.npField = value;
            }
        }
    }

    public class CT_MdxSet
    {

        private List<CT_MetadataStringIndex> nField;

        private uint nsField;

        private uint cField;

        private ST_MdxSetOrder oField;

        public CT_MdxSet()
        {
            this.nField = new List<CT_MetadataStringIndex>();
            this.cField = ((uint)(0));
            this.oField = ST_MdxSetOrder.u;
        }

        public List<CT_MetadataStringIndex> n
        {
            get
            {
                return this.nField;
            }
            set
            {
                this.nField = value;
            }
        }

        public uint ns
        {
            get
            {
                return this.nsField;
            }
            set
            {
                this.nsField = value;
            }
        }

        [DefaultValueAttribute(typeof(uint), "0")]
        public uint c
        {
            get
            {
                return this.cField;
            }
            set
            {
                this.cField = value;
            }
        }

        [DefaultValueAttribute(ST_MdxSetOrder.u)]
        public ST_MdxSetOrder o
        {
            get
            {
                return this.oField;
            }
            set
            {
                this.oField = value;
            }
        }
    }
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_MetadataStringIndex
    {

        private uint xField;

        private bool sField;

        public CT_MetadataStringIndex()
        {
            this.sField = false;
        }

        public uint x
        {
            get
            {
                return this.xField;
            }
            set
            {
                this.xField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool s
        {
            get
            {
                return this.sField;
            }
            set
            {
                this.sField = value;
            }
        }
    }

    public enum ST_MdxSetOrder
    {

        u,

        a,

        d,

        aa,

        ad,

        na,

        nd,
    }

    public class CT_MdxTuple
    {

        private List<CT_MetadataStringIndex> nField;

        private uint cField;

        private string ctField;

        private uint siField;

        private bool siFieldSpecified;

        private uint fiField;

        private bool fiFieldSpecified;

        private byte[] bcField = null;

        private byte[] fcField = null;

        private bool iField;

        private bool uField;

        private bool stField;

        private bool bField;

        public CT_MdxTuple()
        {
            this.nField = new List<CT_MetadataStringIndex>();
            this.cField = ((uint)(0));
            this.iField = false;
            this.uField = false;
            this.stField = false;
            this.bField = false;
        }

        public List<CT_MetadataStringIndex> n
        {
            get
            {
                return this.nField;
            }
            set
            {
                this.nField = value;
            }
        }

        [DefaultValueAttribute(typeof(uint), "0")]
        public uint c
        {
            get
            {
                return this.cField;
            }
            set
            {
                this.cField = value;
            }
        }

        public string ct
        {
            get
            {
                return this.ctField;
            }
            set
            {
                this.ctField = value;
            }
        }

        public uint si
        {
            get
            {
                return this.siField;
            }
            set
            {
                this.siField = value;
            }
        }

        [XmlIgnore]
        public bool siSpecified
        {
            get
            {
                return this.siFieldSpecified;
            }
            set
            {
                this.siFieldSpecified = value;
            }
        }

        public uint fi
        {
            get
            {
                return this.fiField;
            }
            set
            {
                this.fiField = value;
            }
        }

        [XmlIgnore]
        public bool fiSpecified
        {
            get
            {
                return this.fiFieldSpecified;
            }
            set
            {
                this.fiFieldSpecified = value;
            }
        }

        [XmlAttribute(DataType = "hexBinary")]
        // Type ST_UnsignedIntHex is base on xsd:hexBinary, Length 4 (octets!?)
        public byte[] bc
        {
            get
            {
                return this.bcField;
            }
            set
            {
                this.bcField = value;
            }
        }

        [XmlAttribute(DataType = "hexBinary")]
        // Type ST_UnsignedIntHex is base on xsd:hexBinary, Length 4 (octets!?)
        public byte[] fc
        {
            get
            {
                return this.fcField;
            }
            set
            {
                this.fcField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool i
        {
            get
            {
                return this.iField;
            }
            set
            {
                this.iField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool u
        {
            get
            {
                return this.uField;
            }
            set
            {
                this.uField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool st
        {
            get
            {
                return this.stField;
            }
            set
            {
                this.stField = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool b
        {
            get
            {
                return this.bField;
            }
            set
            {
                this.bField = value;
            }
        }
    }

    public class CT_Mdx
    {

        private object itemField;

        private uint nField;

        private ST_MdxFunctionType fField;

        public object Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }

        public uint n
        {
            get
            {
                return this.nField;
            }
            set
            {
                this.nField = value;
            }
        }

        public ST_MdxFunctionType f
        {
            get
            {
                return this.fField;
            }
            set
            {
                this.fField = value;
            }
        }
    }

    public enum ST_MdxFunctionType
    {

        m,

        v,

        s,

        c,

        r,

        p,

        k,
    }

    public class CT_MdxMetadata
    {

        private List<CT_Mdx> mdxField;

        private uint countField;

        public CT_MdxMetadata()
        {
            this.mdxField = new List<CT_Mdx>();
            this.countField = ((uint)(0));
        }

        public List<CT_Mdx> mdx
        {
            get
            {
                return this.mdxField;
            }
            set
            {
                this.mdxField = value;
            }
        }

        [DefaultValueAttribute(typeof(uint), "0")]
        public uint count
        {
            get
            {
                return this.countField;
            }
            set
            {
                this.countField = value;
            }
        }
    }
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_MetadataStrings
    {

        private List<CT_XStringElement> sField;

        private uint countField;

        public CT_MetadataStrings()
        {
            this.sField = new List<CT_XStringElement>();
            this.countField = ((uint)(0));
        }

        public List<CT_XStringElement> s
        {
            get
            {
                return this.sField;
            }
            set
            {
                this.sField = value;
            }
        }

        [DefaultValueAttribute(typeof(uint), "0")]
        public uint count
        {
            get
            {
                return this.countField;
            }
            set
            {
                this.countField = value;
            }
        }
    }
    [Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
    [XmlRoot(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main", IsNullable = true)]

    public class CT_MetadataBlock
    {

        private List<CT_MetadataRecord> rcField;

        public CT_MetadataBlock()
        {
            this.rcField = new List<CT_MetadataRecord>();
        }

        public List<CT_MetadataRecord> rc
        {
            get
            {
                return this.rcField;
            }
            set
            {
                this.rcField = value;
            }
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Common;
using Mosa.Compiler.Framework;
using Mosa.Compiler.Framework.Platform;
using System.Diagnostics;

namespace Mosa.Platform.x86.Instructions
{
	/// <summary>
	/// Representations the x86 Movzx instruction.
	/// </summary>
	public sealed class Movzx : X86Instruction
	{
		#region Construction

		/// <summary>
		/// Initializes a new instance of <see cref="Movzx" />.
		/// </summary>
		public Movzx() :
			base(1, 1)
		{
		}

		#endregion Construction

		#region Methods

		/// <summary>
		/// Emits the specified platform instruction.
		/// </summary>
		/// <param name="node">The node.</param>
		/// <param name="emitter">The emitter.</param>
		protected override void Emit(InstructionNode node, MachineCodeEmitter emitter)
		{
			MovzxRegToReg(node, emitter);
		}

		private static void MovzxRegToReg(InstructionNode node, MachineCodeEmitter emitter)
		{
			Debug.Assert(node.Result.IsRegister);
			Debug.Assert(node.Operand1.IsRegister);

			// register2 to register1 0000 1111 : 1011 011w : 11 reg1 reg2
			var opcode = new OpcodeEncoder()
				.AppendNibble(Bits.b0000)                           // 4:opcode
				.AppendNibble(Bits.b1111)                           // 4:opcode
				.AppendNibble(Bits.b1011)                           // 4:opcode
				.Append3Bits(Bits.b011)                             // 4:opcode
				.AppendWidthBit(node.Size != InstructionSize.Size8)      // 1:width
				.AppendMod(Bits.b11)                                // 2:mod
				.AppendRegister(node.Result)                        // 3:register (destination)
				.AppendRM(node.Operand1);                           // 3:r/m (source)

			emitter.Emit(opcode);
		}

		#endregion Methods
	}
}


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.sqlobject;

import static org.assertj.core.api.Assertions.assertThat;

import java.sql.Connection;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

import org.h2.jdbcx.JdbcDataSource;
import org.jdbi.v3.core.ConnectionException;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.Something;
import org.jdbi.v3.core.mapper.SomethingMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestNewApiOnDbiAndHandle
{
    private Jdbi db;
    private Handle handle;

    @Before
    public void setUp() throws Exception
    {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:" + UUID.randomUUID());
        db = Jdbi.create(ds);
        db.installPlugin(new SqlObjectPlugin());
        db.registerRowMapper(new SomethingMapper());
        handle = db.open();

        handle.execute("create table something (id int primary key, name varchar(100))");
    }

    @After
    public void tearDown() throws Exception
    {
        handle.execute("drop table something");
        handle.close();
    }

    @Test
    public void testOpenNewSpiffy() throws Exception
    {
        final AtomicReference<Connection> c = new AtomicReference<>();

        db.useExtension(Spiffy.class, spiffy -> {
            spiffy.insert(new Something(1, "Tim"));
            spiffy.insert(new Something(2, "Diego"));

            assertThat(spiffy.findNameById(2)).isEqualTo("Diego");
            c.set(spiffy.getHandle().getConnection());
        });

        assertThat(c.get().isClosed()).isTrue();
    }

    @Test
    public void testOnDemandSpiffy() throws Exception
    {
        Spiffy spiffy = db.onDemand(Spiffy.class);

        spiffy.insert(new Something(1, "Tim"));
        spiffy.insert(new Something(2, "Diego"));

        assertThat(spiffy.findNameById(2)).isEqualTo("Diego");
    }

    @Test
    public void testAttach() throws Exception
    {
        Spiffy spiffy = handle.attach(Spiffy.class);

        spiffy.insert(new Something(1, "Tim"));
        spiffy.insert(new Something(2, "Diego"));

        assertThat(spiffy.findNameById(2)).isEqualTo("Diego");
    }

    @Test(expected = ConnectionException.class)
    public void testCorrectExceptionIfUnableToConnectOnDemand(){
        Jdbi.create("jdbc:mysql://invalid.invalid/test", "john", "scott")
                .installPlugin(new SqlObjectPlugin())
                .onDemand(Spiffy.class)
                .findNameById(1);
    }

    @Test(expected = ConnectionException.class)
    public void testCorrectExceptionIfUnableToConnectOnOpen(){
        Jdbi.create("jdbc:mysql://invalid.invalid/test", "john", "scott")
                .installPlugin(new SqlObjectPlugin())
                .open()
                .attach(Spiffy.class);
    }

    @Test(expected = ConnectionException.class)
    public void testCorrectExceptionIfUnableToConnectOnAttach(){
        Jdbi.create("jdbc:mysql://invalid.invalid/test", "john", "scott")
                .installPlugin(new SqlObjectPlugin())
                .open()
                .attach(Spiffy.class);
    }

    public interface Spiffy extends SqlObject
    {
        @SqlUpdate("insert into something (id, name) values (:it.id, :it.name)")
        void insert(@BindSomething("it") Something s);

        @SqlQuery("select name from something where id = :id")
        String findNameById(@Bind("id") int id);
    }
}


text:
Grades

            <canvas style="width: 100%" id="line" class="chart chart-line" chart-data="allGradeData"
                    chart-labels="allGradeLabels" chart-legend="true" chart-series="allGradeSeries"
                    chart-colours="allGradeColours">
            </canvas>

                View

                    Year

                    Quarter

                    CP Mens GPA

                CP Womens GPA
                IFC GPA
                PHA GPA
                USFC GPA

                Edit

                {{ gradeaverage.year }}
                {{ gradeaverage.quarter }}
                {{ gradeaverage.calpolymensgpa }}
                {{ gradeaverage.calpolywomensgpa }}
                {{ gradeaverage.ifcgpa }}
                {{ gradeaverage.phagpa }}
                {{ gradeaverage.usfcgpa }}

        Create New GPA


text:
At the moment ProGuard rules for RxJava 2.x are not required, it doesn't use Reflection nor Unsafe in a ways that require ProGuard. If at some point rules for RxJava 2.x/3.x/etc will be required — this project will be updated accordingly.

See issue [#44 for more info](https://github.com/artem-zinnatullin/RxJavaProGuardRules/issues/44).

---

Versioning principle:

Imagine RxJava has version `x.y.z` — RxJavaProGuardRules version will be `x.y.z.n` where `n` is patch version of ProGuard rules for the concrete version of RxJava!

Example: RxJava `1.3.3`, RxJavaProGuardRules `1.3.3.n`. Easy breezy.

------------

#### Download

```groovy
// RxJava itself
compile 'io.reactivex:rxjava:1.3.3'

// And ProGuard rules for RxJava!
compile 'com.artemzin.rxjava:proguard-rules:1.3.3.0'
```

You can find all releases on [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.artemzin.rxjava%22%20AND%20a%3A%22proguard-rules%22), and here is [the file with ProGuard rules](rxjava-proguard-rules/proguard-rules.txt).

------------

Credit for the very first ProGuard rules in this library goes to [@felipecsl](https://github.com/felipecsl)!


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FM4CC.Util.Heatmap
{
    public class HeatPoint
    {
        public double X;
        public double Y;
        public double Intensity;

        public HeatPoint(double x, double y, double intensity)
        {
            X = x;
            Y = y;
            Intensity = intensity;
        }
    }
}


text:
import { test, moduleForComponent } from 'ember-qunit';

moduleForComponent('<%= dasherizedModuleName %>', '<%= classifiedModuleName %>Component', {
  // specify the other units that are required for this test
  // needs: ['component:foo', 'helper:bar']
});

test('it renders', function() {
  expect(2);

  // creates the component instance
  var component = this.subject();
  equal(component.state, 'preRender');

  // appends the component to the page
  this.append();
  equal(component.state, 'inDOM');
});


text:
layout: post
title: "Inconvenient Truths of NAND Flash"
date: 2015-10-09
crawlertitle: "Inconvenient Truths of NAND Flash - Jim Cooke"
summary: "Another great doc on NAND Flash shortcomings"
categories: posts
tags: 'electronics'
---

I've noticed that Jim Cooke's great presentation, "The Inconvenient Truths of NAND Flash", has gone missing from the internet. It's a pretty great primer on how NAND works and breaks, so I decided to serve up a local copy I dug out of my hard drive.

[It's available here for your convenience!](/assets/cooke_inconvenient_truths.pdf)

<iframe src="https://drive.google.com/file/d/0B1nlNdjzPFUYbGNLanJ4TVNrQmc/preview" width="640" height="480"></iframe>