Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/wpforms-.../src/Forms
File: Preview.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Forms;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Form preview.
[5] Fix | Delete
*
[6] Fix | Delete
* @since 1.5.1
[7] Fix | Delete
*/
[8] Fix | Delete
class Preview {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Form data.
[12] Fix | Delete
*
[13] Fix | Delete
* @since 1.5.1
[14] Fix | Delete
*
[15] Fix | Delete
* @var array
[16] Fix | Delete
*/
[17] Fix | Delete
public $form_data;
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Post type.
[21] Fix | Delete
*
[22] Fix | Delete
* @since 1.8.8
[23] Fix | Delete
*
[24] Fix | Delete
* @var string
[25] Fix | Delete
*/
[26] Fix | Delete
private $post_type;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Whether this is a form template.
[30] Fix | Delete
*
[31] Fix | Delete
* @since 1.8.8
[32] Fix | Delete
*
[33] Fix | Delete
* @var bool
[34] Fix | Delete
*/
[35] Fix | Delete
private $is_form_template;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Constructor.
[39] Fix | Delete
*
[40] Fix | Delete
* @since 1.5.1
[41] Fix | Delete
*/
[42] Fix | Delete
public function __construct() {
[43] Fix | Delete
[44] Fix | Delete
if ( ! $this->is_preview_page() ) {
[45] Fix | Delete
return;
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
$this->hooks();
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Check if current page request meets requirements for form preview page.
[53] Fix | Delete
*
[54] Fix | Delete
* @since 1.5.1
[55] Fix | Delete
*
[56] Fix | Delete
* @return bool
[57] Fix | Delete
*/
[58] Fix | Delete
public function is_preview_page(): bool {
[59] Fix | Delete
[60] Fix | Delete
// Only proceed for the form preview page.
[61] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
[62] Fix | Delete
if ( empty( $_GET['wpforms_form_preview'] ) ) {
[63] Fix | Delete
return false;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
// Only logged-in users can access the preview page.
[67] Fix | Delete
if ( ! is_user_logged_in() ) {
[68] Fix | Delete
return false;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
[72] Fix | Delete
$form_id = absint( $_GET['wpforms_form_preview'] );
[73] Fix | Delete
[74] Fix | Delete
// Make sure the user is allowed to preview the form.
[75] Fix | Delete
if ( ! wpforms_current_user_can( 'view_form_single', $form_id ) ) {
[76] Fix | Delete
return false;
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
// Fetch form details.
[80] Fix | Delete
$this->form_data = wpforms()->obj( 'form' )->get( $form_id, [ 'content_only' => true ] );
[81] Fix | Delete
[82] Fix | Delete
// Get the post type for preview item.
[83] Fix | Delete
$this->post_type = get_post_type( $form_id );
[84] Fix | Delete
[85] Fix | Delete
// Check if this is a form template.
[86] Fix | Delete
$this->is_form_template = $this->post_type === 'wpforms-template';
[87] Fix | Delete
[88] Fix | Delete
// Check valid form was found.
[89] Fix | Delete
if ( empty( $this->form_data ) || empty( $this->form_data['id'] ) ) {
[90] Fix | Delete
return false;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
return true;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Hooks.
[98] Fix | Delete
*
[99] Fix | Delete
* @since 1.5.1
[100] Fix | Delete
*/
[101] Fix | Delete
public function hooks() {
[102] Fix | Delete
[103] Fix | Delete
add_filter( 'wpforms_frontend_assets_header_force_load', '__return_true' );
[104] Fix | Delete
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
[105] Fix | Delete
add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ] );
[106] Fix | Delete
add_filter( 'the_title', [ $this, 'the_title' ], 100, 1 );
[107] Fix | Delete
add_filter( 'the_content', [ $this, 'the_content' ], 999 );
[108] Fix | Delete
add_filter( 'get_the_excerpt', [ $this, 'the_content' ], 999 );
[109] Fix | Delete
add_filter( 'home_template_hierarchy', [ $this, 'force_page_template_hierarchy' ] );
[110] Fix | Delete
add_filter( 'frontpage_template_hierarchy', [ $this, 'force_page_template_hierarchy' ] );
[111] Fix | Delete
add_filter( 'wpforms_smarttags_process_page_title_value', [ $this, 'smart_tags_process_page_title_value' ], 10, 5 );
[112] Fix | Delete
add_filter( 'post_thumbnail_html', '__return_empty_string' );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Enqueue additional form preview styles.
[117] Fix | Delete
*
[118] Fix | Delete
* @since 1.8.8
[119] Fix | Delete
*/
[120] Fix | Delete
public function enqueue_assets() {
[121] Fix | Delete
[122] Fix | Delete
$min = wpforms_get_min_suffix();
[123] Fix | Delete
[124] Fix | Delete
// Enqueue the form preview styles.
[125] Fix | Delete
wp_enqueue_style(
[126] Fix | Delete
'wpforms-preview',
[127] Fix | Delete
WPFORMS_PLUGIN_URL . "assets/css/frontend/wpforms-form-preview{$min}.css",
[128] Fix | Delete
[],
[129] Fix | Delete
WPFORMS_VERSION
[130] Fix | Delete
);
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Modify query, limit to one post.
[135] Fix | Delete
*
[136] Fix | Delete
* @since 1.5.1
[137] Fix | Delete
* @since 1.7.0 Added `page_id`, `post_type` and `post__in` query variables.
[138] Fix | Delete
*
[139] Fix | Delete
* @param \WP_Query $query The WP_Query instance.
[140] Fix | Delete
*/
[141] Fix | Delete
public function pre_get_posts( $query ) {
[142] Fix | Delete
[143] Fix | Delete
if ( is_admin() || ! $query->is_main_query() ) {
[144] Fix | Delete
return;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
$query->set( 'page_id', '' );
[148] Fix | Delete
$query->set( 'post_type', $this->post_type ?? 'wpforms' );
[149] Fix | Delete
$query->set( 'post__in', empty( $this->form_data['id'] ) ? [] : [ (int) $this->form_data['id'] ] );
[150] Fix | Delete
$query->set( 'posts_per_page', 1 );
[151] Fix | Delete
[152] Fix | Delete
// The preview page reads as the home page and as an non-singular posts page, neither of which are actually the case.
[153] Fix | Delete
// So we hardcode the correct values for those properties in the query.
[154] Fix | Delete
$query->is_home = false;
[155] Fix | Delete
$query->is_singular = true;
[156] Fix | Delete
$query->is_single = true;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Customize form preview page title.
[161] Fix | Delete
*
[162] Fix | Delete
* @since 1.5.1
[163] Fix | Delete
*
[164] Fix | Delete
* @param string $title Page title.
[165] Fix | Delete
*
[166] Fix | Delete
* @return string
[167] Fix | Delete
*/
[168] Fix | Delete
public function the_title( $title ) {
[169] Fix | Delete
[170] Fix | Delete
if ( ! in_the_loop() ) {
[171] Fix | Delete
return $title;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
if ( $this->is_form_template ) {
[175] Fix | Delete
return sprintf( /* translators: %s - form name. */
[176] Fix | Delete
esc_html__( '%s Template Preview', 'wpforms-lite' ),
[177] Fix | Delete
! empty( $this->form_data['settings']['form_title'] ) ? sanitize_text_field( $this->form_data['settings']['form_title'] ) : esc_html__( 'Form Template', 'wpforms-lite' )
[178] Fix | Delete
);
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
return sprintf( /* translators: %s - form name. */
[182] Fix | Delete
esc_html__( '%s Preview', 'wpforms-lite' ),
[183] Fix | Delete
! empty( $this->form_data['settings']['form_title'] ) ? sanitize_text_field( $this->form_data['settings']['form_title'] ) : esc_html__( 'Form', 'wpforms-lite' )
[184] Fix | Delete
);
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Customize form preview page content.
[189] Fix | Delete
*
[190] Fix | Delete
* @since 1.5.1
[191] Fix | Delete
*
[192] Fix | Delete
* @return string
[193] Fix | Delete
*/
[194] Fix | Delete
public function the_content() {
[195] Fix | Delete
[196] Fix | Delete
if ( ! isset( $this->form_data['id'] ) ) {
[197] Fix | Delete
return '';
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
if ( ! wpforms_current_user_can( 'view_form_single', $this->form_data['id'] ) ) {
[201] Fix | Delete
return '';
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
$admin_url = admin_url( 'admin.php' );
[205] Fix | Delete
[206] Fix | Delete
$links = [];
[207] Fix | Delete
[208] Fix | Delete
if ( wpforms_current_user_can( 'edit_form_single', $this->form_data['id'] ) ) {
[209] Fix | Delete
$links[] = [
[210] Fix | Delete
'url' => esc_url(
[211] Fix | Delete
add_query_arg(
[212] Fix | Delete
[
[213] Fix | Delete
'page' => 'wpforms-builder',
[214] Fix | Delete
'view' => 'fields',
[215] Fix | Delete
'form_id' => absint( $this->form_data['id'] ),
[216] Fix | Delete
],
[217] Fix | Delete
$admin_url
[218] Fix | Delete
)
[219] Fix | Delete
),
[220] Fix | Delete
'text' => $this->is_form_template ? esc_html__( 'Edit Form Template', 'wpforms-lite' ) : esc_html__( 'Edit Form', 'wpforms-lite' ),
[221] Fix | Delete
];
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
if ( wpforms()->is_pro() && wpforms_current_user_can( 'view_entries_form_single', $this->form_data['id'] ) ) {
[225] Fix | Delete
$links[] = [
[226] Fix | Delete
'url' => esc_url(
[227] Fix | Delete
add_query_arg(
[228] Fix | Delete
[
[229] Fix | Delete
'page' => 'wpforms-entries',
[230] Fix | Delete
'view' => 'list',
[231] Fix | Delete
'form_id' => absint( $this->form_data['id'] ),
[232] Fix | Delete
],
[233] Fix | Delete
$admin_url
[234] Fix | Delete
)
[235] Fix | Delete
),
[236] Fix | Delete
'text' => esc_html__( 'View Entries', 'wpforms-lite' ),
[237] Fix | Delete
];
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
if (
[241] Fix | Delete
! $this->is_form_template &&
[242] Fix | Delete
wpforms_current_user_can( wpforms_get_capability_manage_options(), $this->form_data['id'] ) &&
[243] Fix | Delete
wpforms()->obj( 'payment' )->get_by( 'form_id', $this->form_data['id'] )
[244] Fix | Delete
) {
[245] Fix | Delete
$links[] = [
[246] Fix | Delete
'url' => esc_url(
[247] Fix | Delete
add_query_arg(
[248] Fix | Delete
[
[249] Fix | Delete
'page' => 'wpforms-payments',
[250] Fix | Delete
'form_id' => absint( $this->form_data['id'] ),
[251] Fix | Delete
],
[252] Fix | Delete
$admin_url
[253] Fix | Delete
)
[254] Fix | Delete
),
[255] Fix | Delete
'text' => esc_html__( 'View Payments', 'wpforms-lite' ),
[256] Fix | Delete
];
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
if ( ! empty( $_GET['new_window'] ) ) { // phpcs:ignore
[260] Fix | Delete
$links[] = [
[261] Fix | Delete
'url' => 'javascript:window.close();',
[262] Fix | Delete
'text' => esc_html__( 'Close this window', 'wpforms-lite' ),
[263] Fix | Delete
];
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
$content = '';
[267] Fix | Delete
[268] Fix | Delete
$content .= $this->add_preview_notice();
[269] Fix | Delete
[270] Fix | Delete
$content .= '<p>';
[271] Fix | Delete
$content .= $this->is_form_template ?
[272] Fix | Delete
esc_html__( 'This is a preview of the latest saved revision of your form template. If this preview does not match your template, save your changes and then refresh this page. This template preview is not publicly accessible.', 'wpforms-lite' ) :
[273] Fix | Delete
esc_html__( 'This is a preview of the latest saved revision of your form. If this preview does not match your form, save your changes and then refresh this page. This form preview is not publicly accessible.', 'wpforms-lite' );
[274] Fix | Delete
[275] Fix | Delete
if ( ! empty( $links ) ) {
[276] Fix | Delete
$content .= '<br>';
[277] Fix | Delete
$content .= '<span class="wpforms-preview-notice-links">';
[278] Fix | Delete
[279] Fix | Delete
foreach ( $links as $key => $link ) {
[280] Fix | Delete
$content .= '<a href="' . $link['url'] . '">' . $link['text'] . '</a>';
[281] Fix | Delete
$l = array_keys( $links );
[282] Fix | Delete
[283] Fix | Delete
if ( end( $l ) !== $key ) {
[284] Fix | Delete
$content .= ' <span style="display:inline-block;margin:0 6px;opacity: 0.5">|</span> ';
[285] Fix | Delete
}
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
$content .= '</span>';
[289] Fix | Delete
}
[290] Fix | Delete
$content .= '</p>';
[291] Fix | Delete
[292] Fix | Delete
$content .= '<p>';
[293] Fix | Delete
$content .= sprintf(
[294] Fix | Delete
wp_kses(
[295] Fix | Delete
/* translators: %s - WPForms doc link. */
[296] Fix | Delete
__( 'For form testing tips, check out our <a href="%s" target="_blank" rel="noopener noreferrer">complete guide!</a>', 'wpforms-lite' ),
[297] Fix | Delete
[
[298] Fix | Delete
'a' => [
[299] Fix | Delete
'href' => [],
[300] Fix | Delete
'target' => [],
[301] Fix | Delete
'rel' => [],
[302] Fix | Delete
],
[303] Fix | Delete
]
[304] Fix | Delete
),
[305] Fix | Delete
esc_url(
[306] Fix | Delete
wpforms_utm_link(
[307] Fix | Delete
'https://wpforms.com/docs/how-to-properly-test-your-wordpress-forms-before-launching-checklist/',
[308] Fix | Delete
$this->is_form_template ? 'Form Template Preview' : 'Form Preview',
[309] Fix | Delete
'Form Testing Tips Documentation'
[310] Fix | Delete
)
[311] Fix | Delete
)
[312] Fix | Delete
);
[313] Fix | Delete
$content .= '</p>';
[314] Fix | Delete
[315] Fix | Delete
$content .= do_shortcode( '[wpforms id="' . absint( $this->form_data['id'] ) . '"]' );
[316] Fix | Delete
[317] Fix | Delete
return $content;
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
/**
[321] Fix | Delete
* Add preview notice.
[322] Fix | Delete
*
[323] Fix | Delete
* @since 1.8.8
[324] Fix | Delete
*
[325] Fix | Delete
* @return string HTML content.
[326] Fix | Delete
*/
[327] Fix | Delete
private function add_preview_notice(): string {
[328] Fix | Delete
[329] Fix | Delete
if ( ! $this->is_form_template ) {
[330] Fix | Delete
return '';
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
$content = '<div class="wpforms-preview-notice">';
[334] Fix | Delete
$content .= sprintf(
[335] Fix | Delete
'<strong>%s</strong> %s',
[336] Fix | Delete
esc_html__( 'Heads up!', 'wpforms-lite' ),
[337] Fix | Delete
esc_html__( 'You\'re viewing a preview of a form template.', 'wpforms-lite' )
[338] Fix | Delete
);
[339] Fix | Delete
[340] Fix | Delete
if ( wpforms()->is_pro() ) {
[341] Fix | Delete
/** This filter is documented in wpforms/src/Pro/Tasks/Actions/PurgeTemplateEntryTask.php */
[342] Fix | Delete
$delay = (int) apply_filters( 'wpforms_pro_tasks_actions_purge_template_entry_task_delay', DAY_IN_SECONDS ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
[343] Fix | Delete
[344] Fix | Delete
$message = sprintf( /* translators: %s - time period, e.g. 24 hours. */
[345] Fix | Delete
__( 'Entries are automatically deleted after %s.', 'wpforms-lite' ),
[346] Fix | Delete
// The `- 1` hack is to avoid the "1 day" message in favor of "24 hours".
[347] Fix | Delete
human_time_diff( time(), time() + $delay - 1 )
[348] Fix | Delete
);
[349] Fix | Delete
[350] Fix | Delete
$content .= sprintf( '<p>%s</p>', esc_html( $message ) );
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
$content .= '</div>';
[354] Fix | Delete
[355] Fix | Delete
return wp_kses_post( $content );
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
/**
[359] Fix | Delete
* Force page template types.
[360] Fix | Delete
*
[361] Fix | Delete
* @since 1.7.2
[362] Fix | Delete
*
[363] Fix | Delete
* @param array $templates A list of template candidates, in descending order of priority.
[364] Fix | Delete
*
[365] Fix | Delete
* @return array
[366] Fix | Delete
*/
[367] Fix | Delete
public function force_page_template_hierarchy( $templates ) {
[368] Fix | Delete
[369] Fix | Delete
return [ 'page.php', 'single.php', 'index.php' ];
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
/**
[373] Fix | Delete
* Adjust value of the {page_title} smart tag.
[374] Fix | Delete
*
[375] Fix | Delete
* @since 1.7.7
[376] Fix | Delete
*
[377] Fix | Delete
* @param string $content Content.
[378] Fix | Delete
* @param array $form_data Form data.
[379] Fix | Delete
* @param array $fields List of fields.
[380] Fix | Delete
* @param string $entry_id Entry ID.
[381] Fix | Delete
* @param object $smart_tag_object The smart tag object or the Generic object for those cases when class unregistered.
[382] Fix | Delete
*
[383] Fix | Delete
* @return string
[384] Fix | Delete
*/
[385] Fix | Delete
public function smart_tags_process_page_title_value( $content, $form_data, $fields, $entry_id, $smart_tag_object ) {
[386] Fix | Delete
[387] Fix | Delete
return sprintf( /* translators: %s - form name. */
[388] Fix | Delete
esc_html__( '%s Preview', 'wpforms-lite' ),
[389] Fix | Delete
! empty( $form_data['settings']['form_title'] ) ? sanitize_text_field( $form_data['settings']['form_title'] ) : esc_html__( 'Form', 'wpforms-lite' )
[390] Fix | Delete
);
[391] Fix | Delete
}
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function