*/
function et_builder_get_built_in_dynamic_content_fields( $post_id = 0 ) {
$post_type = get_post_type( $post_id );
$post_type = $post_type ? $post_type : 'post';
$post_type_object = get_post_type_object( $post_type );
$post_type_label = $post_type_object->labels->singular_name;
$post_taxonomy_types = et_builder_get_taxonomy_types( get_post_type( $post_id ) );
$default_category = 'post' === $post_type ? 'category' : "${post_type}_category";
if ( ! empty( $post_taxonomy_types ) && ! isset( $post_taxonomy_types[$default_category] ) ) {
// Use the 1st available taxonomy as the default value.
// Do it in 2 steps in order to support PHP < 5.4 (array dereferencing).
$default_category = array_keys( $post_taxonomy_types );
$default_category = $default_category[0];
}
$date_format_options = array(
'default' => esc_html__( 'Default', 'et_builder' ),
'M j, Y' => esc_html__( 'Aug 6, 1999 (M j, Y)', 'et_builder' ),
'F d, Y' => esc_html__( 'August 06, 1999 (F d, Y)', 'et_builder' ),
'm/d/Y' => esc_html__( '08/06/1999 (m/d/Y)', 'et_builder' ),
'm.d.Y' => esc_html__( '08.06.1999 (m.d.Y)', 'et_builder' ),
'j M, Y' => esc_html__( '6 Aug, 1999 (j M, Y)', 'et_builder' ),
'l, M d' => esc_html__( 'Tuesday, Aug 06 (l, M d)', 'et_builder' ),
'custom' => esc_html__( 'Custom', 'et_builder' ),
);
$fields = array(
'post_title' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Title', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
),
'post_excerpt' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Excerpt', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'words' => array(
'label' => esc_html__( 'Number of Words', 'et_builder' ),
'type' => 'text',
'default' => '',
),
'read_more_label' => array(
'label' => esc_html__( 'Read More Text', 'et_builder' ),
'type' => 'text',
'default' => '',
),
),
),
'post_date' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Publish Date', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'date_format' => array(
'label' => esc_html__( 'Date Format', 'et_builder' ),
'type' => 'select',
'options' => $date_format_options,
'default' => 'default',
),
'custom_date_format' => array(
'label' => esc_html__( 'Custom Date Format', 'et_builder' ),
'type' => 'text',
'default' => '',
'show_if' => array(
'date_format' => 'custom',
),
),
),
),
'post_comment_count' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Comment Count', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'link_to_comments_page' => array(
'label' => esc_html__( 'Link to Comments Area', 'et_builder' ),
'type' => 'yes_no_button',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default' => 'on',
),
),
),
'post_categories' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Categories', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'link_to_term_page' => array(
'label' => esc_html__( 'Link to Category Index Pages', 'et_builder' ),
'type' => 'yes_no_button',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default' => 'on',
),
'separator' => array(
'label' => esc_html__( 'Categories Separator', 'et_builder' ),
'type' => 'text',
'default' => ' | ',
),
'category_type' => array(
'label' => esc_html__( 'Category Type', 'et_builder' ),
'type' => 'select',
'options' => $post_taxonomy_types,
'default' => $default_category,
),
),
),
'post_tags' => array(),
'post_link' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Link', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'text' => array(
'label' => esc_html__( 'Link Text', 'et_builder' ),
'type' => 'select',
'options' => array(
// Translators: %1$s: Post type name
'post_title' => esc_html( sprintf( __( '%1$s Title', 'et_builder' ), $post_type_label ) ),
'custom' => esc_html__( 'Custom', 'et_builder' ),
),
'default' => 'post_title',
),
'custom_text' => array(
'label' => esc_html__( 'Custom Link Text', 'et_builder' ),
'type' => 'text',
'default' => '',
'show_if' => array(
'text' => 'custom',
),
),
),
),
'post_author' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Author', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'name_format' => array(
'label' => esc_html__( 'Name Format', 'et_builder' ),
'type' => 'select',
'options' => array(
'display_name' => esc_html__( 'Public Display Name', 'et_builder' ),
'first_last_name' => esc_html__( 'First & Last Name', 'et_builder' ),
'last_first_name' => esc_html__( 'Last, First Name', 'et_builder' ),
'first_name' => esc_html__( 'First Name', 'et_builder' ),
'last_name' => esc_html__( 'Last Name', 'et_builder' ),
'nickname' => esc_html__( 'Nickname', 'et_builder' ),
'username' => esc_html__( 'Username', 'et_builder' ),
),
'default' => 'display_name',
),
'link' => array(
'label' => esc_html__( 'Link Name', 'et_builder' ),
'type' => 'yes_no_button',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default' => 'off',
),
'link_destination' => array(
'label' => esc_html__( 'Link Destination', 'et_builder' ),
'type' => 'select',
'options' => array(
'author_archive' => esc_html__( 'Author Archive Page', 'et_builder' ),
'author_website' => esc_html__( 'Author Website', 'et_builder' ),
),
'default' => 'author_archive',
'show_if' => array(
'link' => 'on',
),
),
),
),
'post_author_bio' => array(
'label' => esc_html__( 'Author Bio', 'et_builder' ),
'type' => 'text',
),
'site_title' => array(
'label' => esc_html__( 'Site Title', 'et_builder' ),
'type' => 'text',
),
'site_tagline' => array(
'label' => esc_html__( 'Site Tagline', 'et_builder' ),
'type' => 'text',
),
'current_date' => array(
'label' => esc_html__( 'Current Date', 'et_builder' ),
'type' => 'text',
'fields' => array(
'date_format' => array(
'label' => esc_html__( 'Date Format', 'et_builder' ),
'type' => 'select',
'options' => $date_format_options,
'default' => 'default',
),
'custom_date_format' => array(
'label' => esc_html__( 'Custom Date Format', 'et_builder' ),
'type' => 'text',
'default' => '',
'show_if' => array(
'date_format' => 'custom',
),
),
),
),
'post_link_url' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Link', 'et_builder' ), $post_type_label ) ),
'type' => 'url',
),
'home_url' => array(
'label' => esc_html__( 'Homepage Link', 'et_builder' ),
'type' => 'url',
),
'post_featured_image' => array(
'label' => esc_html__( 'Featured Image', 'et_builder' ),
'type' => 'image',
),
'post_author_profile_picture' => array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Author Profile Picture', 'et_builder' ), $post_type_label ) ),
'type' => 'image',
),
'site_logo' => array(
'label' => esc_html__( 'Site Logo', 'et_builder' ),
'type' => 'image',
),
);
if ( isset( $post_taxonomy_types["${post_type}_tag"] ) ) {
$fields['post_tags'] = array(
// Translators: %1$s: Post type name
'label' => esc_html( sprintf( __( '%1$s Tags', 'et_builder' ), $post_type_label ) ),
'type' => 'text',
'fields' => array(
'link_to_term_page' => array(
'label' => esc_html__( 'Link to Tag Index Pages', 'et_builder' ),
'type' => 'yes_no_button',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default' => 'on',
),
'separator' => array(
'label' => esc_html__( 'Tags Separator', 'et_builder' ),
'type' => 'text',
'default' => ' | ',
),
'category_type' => array(
'label' => esc_html__( 'Category Type', 'et_builder' ),
'type' => 'select',
'options' => $post_taxonomy_types,
'default' => "${post_type}_tag",
),
),
);
} else {
unset( $fields['post_tags'] );
}
// Fill in boilerplate.
foreach ( $fields as $key => $field ) {
$fields[ $key ]['custom'] = false;
if ( 'text' === $field['type'] ) {
$settings = isset( $field['fields'] ) ? $field['fields'] : array();
$settings = array_merge( array(
'before' => array(
'label' => esc_html__( 'Before', 'et_builder' ),
'type' => 'text',
'default' => '',
),
'after' => array(
'label' => esc_html__( 'After', 'et_builder' ),
'type' => 'text',
'default' => '',
),
), $settings );
$fields[ $key ]['fields'] = $settings;
}
}
return $fields;
}
/**
* Get all public taxonomies associated with a given post type.
*
* @since 3.17.2
*
* @param string $post_type
*
* @return array
*/
function et_builder_get_taxonomy_types( $post_type ) {
$taxonomies = get_object_taxonomies( $post_type, 'object' );
$list = array();
if ( empty( $taxonomies ) ) {
return $list;
}
foreach ( $taxonomies as $taxonomy ) {
if ( ! empty( $taxonomy ) && $taxonomy->public && $taxonomy->show_ui ) {
$list[ $taxonomy->name ] = $taxonomy->label;
}
}
return $list;
}
/**
* Get custom dynamic content fields.
*
* @since 3.17.2
*
* @param integer $post_id
*
* @return array
"}} // This is a rough solution implemented due to time constraints. $json = preg_replace( '~ ("(?:before|after)":") # $1 = Anchor to the before/after settings. (?: # Match cases where the value starts with the offending tag. <\/?p> # The root of all evil. [\r\n]+ # Whitespace follows the tag. )* (?: # Match cases where the value ends with the offending tag. ([^"]*) # $2 = The preceeding value. [\r\n]+ # Whitespace preceedes the tag. <\/?p> # The root of all evil. )* ~xi', '$1$2', $json ); // Remove line-breaks which break the json strings. $json = preg_replace( '/\r|\n/', '', $json ); // Strip wrapping
tag as it appears in shortcode content in certain cases (e.g. BB preview). $json = preg_replace( '/^(.*)<\/p>$/i', '$1', trim( $json ) ); // Parse and validate dynamic content schema. $dynamic_content = json_decode( $json, true ); $is_dynamic_content = is_array( $dynamic_content ) && isset( $dynamic_content['dynamic'] ) && (bool) $dynamic_content['dynamic']; $has_content = is_array( $dynamic_content ) && isset( $dynamic_content['content'] ) && is_string( $dynamic_content['content'] ); $has_settings = is_array( $dynamic_content ) && isset( $dynamic_content['settings'] ) && is_array( $dynamic_content['settings'] ); if ( ! $is_dynamic_content || ! $has_content || ! $has_settings ) { return new ET_Builder_Value( false, wp_kses_post( $content ), array() ); } return new ET_Builder_Value( (bool) $dynamic_content['dynamic'], sanitize_text_field( $dynamic_content['content'] ), array_map( 'wp_kses_post', $dynamic_content['settings'] ) ); } /** * Callback to resolve dynamic content for preg_replace_callback. * * @since 3.17.2 * * @param array $matches * * @return string */ function et_builder_resolve_dynamic_content_in_excerpt_callback( $matches ) { global $_et_brdcie_post_id; return et_builder_parse_dynamic_content( $matches[0] )->resolve( $_et_brdcie_post_id ); } /** * Resolve dynamic content in post excerpts instead of showing raw JSON. * * @since 3.17.2 * * @param string $post_excerpt * @param integer $post_id * * @return string */ function et_builder_resolve_dynamic_content_in_excerpt( $post_excerpt, $post_id ) { // Use an obscure acronym named global variable instead of an anonymous function as we are // targeting PHP 5.2. global $_et_brdcie_post_id; $json = '/ \{ # { character (?: # non-capturing group [^{}] # anything that is not a { or } | # OR (?R) # recurse the entire pattern )* # previous group zero or more times \} # } character /x'; $_et_brdcie_post_id = $post_id; $post_excerpt = preg_replace_callback( $json, 'et_builder_resolve_dynamic_content_in_excerpt_callback', $post_excerpt ); $_et_brdcie_post_id = 0; return $post_excerpt; } add_filter( 'et_truncate_post', 'et_builder_resolve_dynamic_content_in_excerpt', 10, 2 );