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 $args Optional. Contact form options.
* @return WPCF7_ContactForm A new contact form object.
*/
public static function get_template( $args = '' ) {
$args = wp_parse_args( $args, array(
'locale' => '',
'title' => __( 'Untitled', 'contact-form-7' ),
) );
$locale = $args['locale'];
$title = $args['title'];
if ( ! $switched = wpcf7_load_textdomain( $locale ) ) {
$locale = determine_locale();
}
$contact_form = new self;
$contact_form->title = $title;
$contact_form->locale = $locale;
$properties = $contact_form->get_properties();
foreach ( $properties as $key => $value ) {
$properties[$key] = WPCF7_ContactFormTemplate::get_default( $key );
}
$contact_form->properties = $properties;
$contact_form = apply_filters( 'wpcf7_contact_form_default_pack',
$contact_form, $args
);
if ( $switched ) {
wpcf7_load_textdomain();
}
self::$current = $contact_form;
return $contact_form;
}
/**
* Returns an instance of WPCF7_ContactForm.
*
* @return WPCF7_ContactForm A new contact form object.
*/
public static function get_instance( $post ) {
$post = get_post( $post );
if ( ! $post
or self::post_type != get_post_type( $post ) ) {
return false;
}
return self::$current = new self( $post );
}
/**
* 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 );
$properties = $this->get_properties();
foreach ( $properties as $key => $value ) {
if ( metadata_exists( 'post', $post->ID, '_' . $key ) ) {
$properties[$key] = get_post_meta( $post->ID, '_' . $key, true );
} elseif ( metadata_exists( 'post', $post->ID, $key ) ) {
$properties[$key] = get_post_meta( $post->ID, $key, true );
}
}
$this->properties = $properties;
$this->upgrade();
}
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 );
}
/**
* Returns the value for the given property name.
*
* @param string $name Property name.
* @return array|string|null Property value. Null if property doesn't 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() {
$properties = (array) $this->properties;
$properties = wp_parse_args( $properties, array(
'form' => '',
'mail' => array(),
'mail_2' => array(),
'messages' => array(),
'additional_settings' => '',
) );
$properties = (array) apply_filters(
'wpcf7_contact_form_properties',
$properties, $this
);
return $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';
}
}
/**
* Returns the specified shortcode attribute value.
*
* @param string $name Shortcode attribute name.
* @return string|null Attribute value. Null if the attribute doesn't 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 $args Optional. Form options.
* @return string HTML output.
*/
public function form_html( $args = '' ) {
$args = wp_parse_args( $args, array(
'html_id' => '',
'html_name' => '',
'html_class' => '',
'output' => 'form',
) );
$this->shortcode_atts = $args;
if ( 'raw_form' == $args['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(
'