array(
'name' => __( 'Contact Forms', 'contact-form-7' ),
'singular_name' => __( 'Contact Form', 'contact-form-7' ),
),
'rewrite' => false,
'query_var' => false,
'public' => false,
'capability_type' => 'page',
'capabilities' => array(
'edit_post' => 'wpcf7_edit_contact_form',
'read_post' => 'wpcf7_read_contact_form',
'delete_post' => 'wpcf7_delete_contact_form',
'edit_posts' => 'wpcf7_edit_contact_forms',
'edit_others_posts' => 'wpcf7_edit_contact_forms',
'publish_posts' => 'wpcf7_edit_contact_forms',
'read_private_posts' => 'wpcf7_edit_contact_forms',
),
) );
}
/**
* Retrieves contact form data that match given conditions.
*
* @param string|array $args Optional. Arguments to be passed to WP_Query.
* @return array Array of WPCF7_ContactForm objects.
*/
public static function find( $args = '' ) {
$defaults = array(
'post_status' => 'any',
'posts_per_page' => -1,
'offset' => 0,
'orderby' => 'ID',
'order' => 'ASC',
);
$args = wp_parse_args( $args, $defaults );
$args['post_type'] = self::post_type;
$q = new WP_Query();
$posts = $q->query( $args );
self::$found_items = $q->found_posts;
$objs = array();
foreach ( (array) $posts as $post ) {
$objs[] = new self( $post );
}
return $objs;
}
/**
* Returns a contact form data filled by default template contents.
*
* @param string|array $options Optional. Contact form options.
* @return WPCF7_ContactForm A new contact form object.
*/
public static function get_template( $options = '' ) {
$options = wp_parse_args( $options, array(
'locale' => null,
'title' => __( 'Untitled', 'contact-form-7' ),
) );
if ( ! isset( $options['locale'] ) ) {
$options['locale'] = determine_locale();
}
$callback = static function ( $options ) {
$contact_form = new self;
$contact_form->title = $options['title'];
$contact_form->locale = $options['locale'];
$properties = $contact_form->get_properties();
foreach ( $properties as $key => $value ) {
$default_template = WPCF7_ContactFormTemplate::get_default( $key );
if ( isset( $default_template ) ) {
$properties[$key] = $default_template;
}
}
$contact_form->properties = $properties;
return $contact_form;
};
$contact_form = wpcf7_switch_locale(
$options['locale'],
$callback,
$options
);
self::$current = apply_filters( 'wpcf7_contact_form_default_pack',
$contact_form, $options
);
return self::$current;
}
/**
* Creates a WPCF7_ContactForm object and sets it as the current instance.
*
* @param WPCF7_ContactForm|WP_Post|int $post Object or post ID.
* @return WPCF7_ContactForm|null Contact form object. Null if unset.
*/
public static function get_instance( $post ) {
$contact_form = null;
if ( $post instanceof self ) {
$contact_form = $post;
} elseif ( ! empty( $post ) ) {
$post = get_post( $post );
if ( isset( $post ) and self::post_type === get_post_type( $post ) ) {
$contact_form = new self( $post );
}
}
return self::$current = $contact_form;
}
/**
* Generates a "unit-tag" for the given contact form ID.
*
* @return string Unit-tag.
*/
private static function generate_unit_tag( $id = 0 ) {
static $global_count = 0;
$global_count += 1;
if ( in_the_loop() ) {
$unit_tag = sprintf( 'wpcf7-f%1$d-p%2$d-o%3$d',
absint( $id ),
get_the_ID(),
$global_count
);
} else {
$unit_tag = sprintf( 'wpcf7-f%1$d-o%2$d',
absint( $id ),
$global_count
);
}
return $unit_tag;
}
/**
* Constructor.
*/
private function __construct( $post = null ) {
$post = get_post( $post );
if ( $post
and self::post_type === get_post_type( $post ) ) {
$this->id = $post->ID;
$this->name = $post->post_name;
$this->title = $post->post_title;
$this->locale = get_post_meta( $post->ID, '_locale', true );
$this->hash = get_post_meta( $post->ID, '_hash', true );
$this->construct_properties( $post );
$this->upgrade();
} else {
$this->construct_properties();
}
do_action( 'wpcf7_contact_form', $this );
}
/**
* Magic method for property overloading.
*/
public function __get( $name ) {
$message = __( '%1$s property of a WPCF7_ContactForm object is no longer accessible. Use %2$s method instead.', 'contact-form-7' );
if ( 'id' == $name ) {
if ( WP_DEBUG ) {
trigger_error(
sprintf( $message, 'id', 'id()' ),
E_USER_DEPRECATED
);
}
return $this->id;
} elseif ( 'title' == $name ) {
if ( WP_DEBUG ) {
trigger_error(
sprintf( $message, 'title', 'title()' ),
E_USER_DEPRECATED
);
}
return $this->title;
} elseif ( $prop = $this->prop( $name ) ) {
if ( WP_DEBUG ) {
trigger_error(
sprintf( $message, $name, 'prop(\'' . $name . '\')' ),
E_USER_DEPRECATED
);
}
return $prop;
}
}
/**
* Returns true if this contact form is not yet saved to the database.
*/
public function initial() {
return empty( $this->id );
}
/**
* Constructs contact form properties. This is called only once
* from the constructor.
*/
private function construct_properties( $post = null ) {
$builtin_properties = array(
'form' => '',
'mail' => array(),
'mail_2' => array(),
'messages' => array(),
'additional_settings' => '',
);
$properties = apply_filters(
'wpcf7_pre_construct_contact_form_properties',
$builtin_properties, $this
);
// Filtering out properties with invalid name
$properties = array_filter(
$properties,
static function ( $key ) {
$sanitized_key = sanitize_key( $key );
return $key === $sanitized_key;
},
ARRAY_FILTER_USE_KEY
);
foreach ( $properties as $name => $val ) {
$prop = $this->retrieve_property( $name );
if ( isset( $prop ) ) {
$properties[$name] = $prop;
}
}
$this->properties = $properties;
foreach ( $properties as $name => $val ) {
$properties[$name] = apply_filters(
"wpcf7_contact_form_property_{$name}",
$val, $this
);
}
$this->properties = $properties;
$properties = (array) apply_filters(
'wpcf7_contact_form_properties',
$properties, $this
);
$this->properties = $properties;
}
/**
* Retrieves contact form property of the specified name from the database.
*
* @param string $name Property name.
* @return array|string|null Property value. Null if property does not exist.
*/
private function retrieve_property( $name ) {
$property = null;
if ( ! $this->initial() ) {
$post_id = $this->id;
if ( metadata_exists( 'post', $post_id, '_' . $name ) ) {
$property = get_post_meta( $post_id, '_' . $name, true );
} elseif ( metadata_exists( 'post', $post_id, $name ) ) {
$property = get_post_meta( $post_id, $name, true );
}
}
return $property;
}
/**
* Returns the value for the given property name.
*
* @param string $name Property name.
* @return array|string|null Property value. Null if property does not exist.
*/
public function prop( $name ) {
$props = $this->get_properties();
return isset( $props[$name] ) ? $props[$name] : null;
}
/**
* Returns all the properties.
*
* @return array This contact form's properties.
*/
public function get_properties() {
return (array) $this->properties;
}
/**
* Updates properties.
*
* @param array $properties New properties.
*/
public function set_properties( $properties ) {
$defaults = $this->get_properties();
$properties = wp_parse_args( $properties, $defaults );
$properties = array_intersect_key( $properties, $defaults );
$this->properties = $properties;
}
/**
* Returns ID of this contact form.
*
* @return int The ID.
*/
public function id() {
return $this->id;
}
/**
* Returns unit-tag for this contact form.
*
* @return string Unit-tag.
*/
public function unit_tag() {
return $this->unit_tag;
}
/**
* Returns name (slug) of this contact form.
*
* @return string Name.
*/
public function name() {
return $this->name;
}
/**
* Returns title of this contact form.
*
* @return string Title.
*/
public function title() {
return $this->title;
}
/**
* Set a title for this contact form.
*
* @param string $title Title.
*/
public function set_title( $title ) {
$title = strip_tags( $title );
$title = trim( $title );
if ( '' === $title ) {
$title = __( 'Untitled', 'contact-form-7' );
}
$this->title = $title;
}
/**
* Returns the locale code of this contact form.
*
* @return string Locale code. Empty string if no valid locale is set.
*/
public function locale() {
if ( wpcf7_is_valid_locale( $this->locale ) ) {
return $this->locale;
} else {
return '';
}
}
/**
* Sets a locale for this contact form.
*
* @param string $locale Locale code.
*/
public function set_locale( $locale ) {
$locale = trim( $locale );
if ( wpcf7_is_valid_locale( $locale ) ) {
$this->locale = $locale;
} else {
$this->locale = 'en_US';
}
}
/**
* Retrieves the random hash string tied to this contact form.
*
* @param int $length Length of hash string.
* @return string Hash string unique to this contact form.
*/
public function hash( $length = 7 ) {
return substr( $this->hash, 0, absint( $length ) );
}
/**
* Returns the specified shortcode attribute value.
*
* @param string $name Shortcode attribute name.
* @return string|null Attribute value. Null if the attribute does not exist.
*/
public function shortcode_attr( $name ) {
if ( isset( $this->shortcode_atts[$name] ) ) {
return (string) $this->shortcode_atts[$name];
}
}
/**
* Returns true if this contact form is identical to the submitted one.
*/
public function is_posted() {
if ( ! WPCF7_Submission::get_instance() ) {
return false;
}
if ( empty( $_POST['_wpcf7_unit_tag'] ) ) {
return false;
}
return $this->unit_tag() === $_POST['_wpcf7_unit_tag'];
}
/**
* Generates HTML that represents a form.
*
* @param string|array $options Optional. Form options.
* @return string HTML output.
*/
public function form_html( $options = '' ) {
$options = wp_parse_args( $options, array(
'html_id' => '',
'html_name' => '',
'html_title' => '',
'html_class' => '',
'output' => 'form',
) );
$this->shortcode_atts = $options;
if ( 'raw_form' == $options['output'] ) {
return sprintf(
'
%s
',
esc_html( $this->prop( 'form' ) )
);
}
if ( $this->is_true( 'subscribers_only' )
and ! current_user_can( 'wpcf7_submit', $this->id() ) ) {
$notice = __(
"This contact form is available only for logged in users.",
'contact-form-7'
);
$notice = sprintf(
'