\n\n";
// Don't show the feedback details unless the nonce matches
if ( $feedback_id && wp_verify_nonce( stripslashes( $_GET['_wpnonce'] ), "contact-form-sent-{$feedback_id}" ) ) {
$r_success_message .= self::success_message( $feedback_id, $form );
}
$r .= apply_filters( 'grunion_contact_form_success_message', $r_success_message );
} else {
// Nothing special - show the normal contact form
if ( $form->get_attribute( 'widget' ) ) {
// Submit form to the current URL
$url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', 'action', '_wpnonce' ) );
} else {
// Submit form to the post permalink
$url = get_permalink();
}
// For SSL/TLS page. See RFC 3986 Section 4.2
$url = set_url_scheme( $url );
// May eventually want to send this to admin-post.php...
$url = apply_filters( 'grunion_contact_form_form_action', "{$url}#contact-form-{$id}", $GLOBALS['post'], $id );
$r .= "\n";
}
$r .= "
";
return $r;
}
static function success_message( $feedback_id, $form ) {
$r_success_message = '';
$feedback = get_post( $feedback_id );
$field_ids = $form->get_field_ids();
$content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $feedback_id );
// Maps field_ids to post_meta keys
$field_value_map = array(
'name' => 'author',
'email' => 'author_email',
'url' => 'author_url',
'subject' => 'subject',
'textarea' => false, // not a post_meta key. This is stored in post_content
);
$contact_form_message = "
\n";
// "Standard" field whitelist
foreach ( $field_value_map as $type => $meta_key ) {
if ( isset( $field_ids[$type] ) ) {
$field = $form->fields[$field_ids[$type]];
if ( $meta_key ) {
if ( isset( $content_fields["_feedback_{$meta_key}"] ) )
$value = $content_fields["_feedback_{$meta_key}"];
} else {
// The feedback content is stored as the first "half" of post_content
$value = $feedback->post_content;
list( $value ) = explode( '', $value );
$value = trim( $value );
}
$contact_form_message .= sprintf(
_x( '%1$s: %2$s', '%1$s = form field label, %2$s = form field value', 'jetpack' ),
wp_kses( $field->get_attribute( 'label' ), array() ),
wp_kses( $value, array() )
) . ' ';
}
}
// "Non-standard" fields
if ( $field_ids['extra'] ) {
// array indexed by field label (not field id)
$extra_fields = get_post_meta( $feedback_id, '_feedback_extra_fields', true );
$extra_field_keys = array_keys( $extra_fields );
$i = 0;
foreach ( $field_ids['extra'] as $field_id ) {
$field = $form->fields[$field_id];
$label = $field->get_attribute( 'label' );
$contact_form_message .= sprintf(
_x( '%1$s: %2$s', '%1$s = form field label, %2$s = form field value', 'jetpack' ),
wp_kses( $label, array() ),
wp_kses( $extra_fields[$extra_field_keys[$i]], array() )
) . ' ';
$i++;
}
}
$contact_form_message .= "
";
$r_success_message .= wp_kses( $contact_form_message, array( 'br' => array(), 'blockquote' => array() ) );
return $r_success_message;
}
/**
* The contact-field shortcode processor
* We use an object method here instead of a static Grunion_Contact_Form_Field class method to parse contact-field shortcodes so that we can tie them to the contact-form object.
*
* @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
* @param string|null $content The shortcode's inner content: [contact-field]$content[/contact-field]
* @return HTML for the contact form field
*/
static function parse_contact_field( $attributes, $content ) {
// Don't try to parse contact form fields if not inside a contact form
if ( ! Grunion_Contact_Form_Plugin::$using_contact_form_field ) {
$att_strs = array();
foreach ( $attributes as $att => $val ) {
if ( is_numeric( $att ) ) { // Is a valueless attribute
$att_strs[] = esc_html( $val );
} else if ( isset( $val ) ) { // A regular attr - value pair
$att_strs[] = esc_html( $att ) . '=\'' . esc_html( $val ) . '\'';
}
}
$html = '[contact-field ' . implode( ' ', $att_strs );
if ( isset( $content ) && ! empty( $content ) ) { // If there is content, let's add a closing tag
$html .= ']' . esc_html( $content ) . '[/contact-field]';
} else { // Otherwise let's add a closing slash in the first tag
$html .= '/]';
}
return $html;
}
$form = Grunion_Contact_Form::$current_form;
$field = new Grunion_Contact_Form_Field( $attributes, $content, $form );
$field_id = $field->get_attribute( 'id' );
if ( $field_id ) {
$form->fields[$field_id] = $field;
} else {
$form->fields[] = $field;
}
if (
isset( $_POST['action'] ) && 'grunion-contact-form' === $_POST['action']
&&
isset( $_POST['contact-form-id'] ) && $form->get_attribute( 'id' ) == $_POST['contact-form-id']
) {
// If we're processing a POST submission for this contact form, validate the field value so we can show errors as necessary.
$field->validate();
}
// Output HTML
return $field->render();
}
/**
* Loops through $this->fields to generate a (structured) list of field IDs
* @return array
*/
function get_field_ids() {
$field_ids = array(
'all' => array(), // array of all field_ids
'extra' => array(), // array of all non-whitelisted field IDs
// Whitelisted "standard" field IDs:
// 'email' => field_id,
// 'name' => field_id,
// 'url' => field_id,
// 'subject' => field_id,
// 'textarea' => field_id,
);
foreach ( $this->fields as $id => $field ) {
$field_ids['all'][] = $id;
$type = $field->get_attribute( 'type' );
if ( isset( $field_ids[$type] ) ) {
// This type of field is already present in our whitelist of "standard" fields for this form
// Put it in extra
$field_ids['extra'][] = $id;
continue;
}
switch ( $type ) {
case 'email' :
case 'name' :
case 'url' :
case 'subject' :
case 'textarea' :
$field_ids[$type] = $id;
break;
default :
// Put everything else in extra
$field_ids['extra'][] = $id;
}
}
return $field_ids;
}
/**
* Process the contact form's POST submission
* Stores feedback. Sends email.
*/
function process_submission() {
global $post;
$plugin = Grunion_Contact_Form_Plugin::init();
$id = $this->get_attribute( 'id' );
$to = $this->get_attribute( 'to' );
$widget = $this->get_attribute( 'widget' );
$contact_form_subject = $this->get_attribute( 'subject' );
$to = str_replace( ' ', '', $to );
$emails = explode( ',', $to );
$valid_emails = array();
foreach ( (array) $emails as $email ) {
if ( !is_email( $email ) ) {
continue;
}
if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $email ) ) {
continue;
}
$valid_emails[] = $email;
}
// No one to send it to :(
if ( !$valid_emails ) {
return false;
}
$to = $valid_emails;
// Make sure we're processing the form we think we're processing... probably a redundant check.
if ( $widget ) {
if ( 'widget-' . $widget != $_POST['contact-form-id'] ) {
return false;
}
} else {
if ( $post->ID != $_POST['contact-form-id'] ) {
return false;
}
}
$field_ids = $this->get_field_ids();
// Initialize all these "standard" fields to null
$comment_author_email = $comment_author_email_label = // v
$comment_author = $comment_author_label = // v
$comment_author_url = $comment_author_url_label = // v
$comment_content = $comment_content_label = null;
// For each of the "standard" fields, grab their field label and value.
if ( isset( $field_ids['name'] ) ) {
$field = $this->fields[$field_ids['name']];
$comment_author = Grunion_Contact_Form_Plugin::strip_tags( stripslashes( apply_filters( 'pre_comment_author_name', addslashes( $field->value ) ) ) );
$comment_author_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
}
if ( isset( $field_ids['email'] ) ) {
$field = $this->fields[$field_ids['email']];
$comment_author_email = Grunion_Contact_Form_Plugin::strip_tags( stripslashes( apply_filters( 'pre_comment_author_email', addslashes( $field->value ) ) ) );
$comment_author_email_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
}
if ( isset( $field_ids['url'] ) ) {
$field = $this->fields[$field_ids['url']];
$comment_author_url = Grunion_Contact_Form_Plugin::strip_tags( stripslashes( apply_filters( 'pre_comment_author_url', addslashes( $field->value ) ) ) );
if ( 'http://' == $comment_author_url ) {
$comment_author_url = '';
}
$comment_author_url_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
}
if ( isset( $field_ids['textarea'] ) ) {
$field = $this->fields[$field_ids['textarea']];
$comment_content = trim( Grunion_Contact_Form_Plugin::strip_tags( $field->value ) );
$comment_content_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
}
if ( isset( $field_ids['subject'] ) ) {
$field = $this->fields[$field_ids['subject']];
if ( $field->value ) {
$contact_form_subject = Grunion_Contact_Form_Plugin::strip_tags( $field->value );
}
}
$all_values = $extra_values = array();
$i = 1; // Prefix counter for stored metadata
// For all fields, grab label and value
foreach ( $field_ids['all'] as $field_id ) {
$field = $this->fields[$field_id];
$label = $i . '_' . $field->get_attribute( 'label' );
$value = $field->value;
$all_values[$label] = $value;
$i++; // Increment prefix counter for the next field
}
// For the "non-standard" fields, grab label and value
// Extra fields have their prefix starting from count( $all_values ) + 1
foreach ( $field_ids['extra'] as $field_id ) {
$field = $this->fields[$field_id];
$label = $i . '_' . $field->get_attribute( 'label' );
$value = $field->value;
$extra_values[$label] = $value;
$i++; // Increment prefix counter for the next extra field
}
$contact_form_subject = trim( $contact_form_subject );
$comment_author_IP = Grunion_Contact_Form_Plugin::get_ip_address();
$vars = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'contact_form_subject', 'comment_author_IP' );
foreach ( $vars as $var )
$$var = str_replace( array( "\n", "\r" ), '', $$var );
$vars[] = 'comment_content';
$spam = '';
$akismet_values = $plugin->prepare_for_akismet( compact( $vars ) );
// Is it spam?
$is_spam = apply_filters( 'contact_form_is_spam', $akismet_values );
if ( is_wp_error( $is_spam ) ) // WP_Error to abort
return $is_spam; // abort
elseif ( $is_spam === TRUE ) // TRUE to flag a spam
$spam = '***SPAM*** ';
if ( !$comment_author )
$comment_author = $comment_author_email;
$to = (array) apply_filters( 'contact_form_to', $to );
foreach ( $to as $to_key => $to_value ) {
$to[$to_key] = Grunion_Contact_Form_Plugin::strip_tags( $to_value );
}
$blog_url = parse_url( site_url() );
$from_email_addr = 'wordpress@' . $blog_url['host'];
$reply_to_addr = $to[0];
if ( ! empty( $comment_author_email ) ) {
$reply_to_addr = $comment_author_email;
}
$headers = 'From: "' . $comment_author .'" <' . $from_email_addr . ">\r\n" .
'Reply-To: "' . $comment_author . '" <' . $reply_to_addr . ">\r\n" .
"Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"";
$subject = apply_filters( 'contact_form_subject', $contact_form_subject, $all_values );
$url = $widget ? home_url( '/' ) : get_permalink( $post->ID );
$date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' );
$date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) );
$time = date_i18n( $date_time_format, current_time( 'timestamp' ) );
$message = "$comment_author_label: $comment_author\n";
if ( !empty( $comment_author_email ) ) {
$message .= "$comment_author_email_label: $comment_author_email\n";
}
if ( !empty( $comment_author_url ) ) {
$message .= "$comment_author_url_label: $comment_author_url\n";
}
if ( !empty( $comment_content_label ) ) {
$message .= "$comment_content_label: $comment_content\n";
}
if ( !empty( $extra_values ) ) {
foreach ( $extra_values as $label => $value ) {
$message .= preg_replace( '#^\d+_#i', '', $label ) . ': ' . trim( $value ) . "\n";
}
}
$message .= "\n";
$message .= __( 'Time:', 'jetpack' ) . ' ' . $time . "\n";
$message .= __( 'IP Address:', 'jetpack' ) . ' ' . $comment_author_IP . "\n";
$message .= __( 'Contact Form URL:', 'jetpack' ) . " $url\n";
if ( is_user_logged_in() ) {
$message .= "\n";
$message .= sprintf(
__( 'Sent by a verified %s user.', 'jetpack' ),
isset( $GLOBALS['current_site']->site_name ) && $GLOBALS['current_site']->site_name ? $GLOBALS['current_site']->site_name : '"' . get_option( 'blogname' ) . '"'
);
} else {
$message .= __( 'Sent by an unverified visitor to your site.', 'jetpack' );
}
$message = apply_filters( 'contact_form_message', $message );
$message = Grunion_Contact_Form_Plugin::strip_tags( $message );
// keep a copy of the feedback as a custom post type
$feedback_time = current_time( 'mysql' );
$feedback_title = "{$comment_author} - {$feedback_time}";
$feedback_status = $is_spam === TRUE ? 'spam' : 'publish';
foreach ( (array) $akismet_values as $av_key => $av_value ) {
$akismet_values[$av_key] = Grunion_Contact_Form_Plugin::strip_tags( $av_value );
}
foreach ( (array) $all_values as $all_key => $all_value ) {
$all_values[$all_key] = Grunion_Contact_Form_Plugin::strip_tags( $all_value );
}
foreach ( (array) $extra_values as $ev_key => $ev_value ) {
$extra_values[$ev_key] = Grunion_Contact_Form_Plugin::strip_tags( $ev_value );
}
/* We need to make sure that the post author is always zero for contact
* form submissions. This prevents export/import from trying to create
* new users based on form submissions from people who were logged in
* at the time.
*
* Unfortunately wp_insert_post() tries very hard to make sure the post
* author gets the currently logged in user id. That is how we ended up
* with this work around. */
add_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );
$post_id = wp_insert_post( array(
'post_date' => addslashes( $feedback_time ),
'post_type' => 'feedback',
'post_status' => addslashes( $feedback_status ),
'post_parent' => (int) $post->ID,
'post_title' => addslashes( wp_kses( $feedback_title, array() ) ),
'post_content' => addslashes( wp_kses( $comment_content . "\n\n" . "AUTHOR: {$comment_author}\nAUTHOR EMAIL: {$comment_author_email}\nAUTHOR URL: {$comment_author_url}\nSUBJECT: {$subject}\nIP: {$comment_author_IP}\n" . print_r( $all_values, TRUE ), array() ) ), // so that search will pick up this data
'post_name' => md5( $feedback_title ),
) );
// once insert has finished we don't need this filter any more
remove_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );
update_post_meta( $post_id, '_feedback_extra_fields', $this->addslashes_deep( $extra_values ) );
update_post_meta( $post_id, '_feedback_akismet_values', $this->addslashes_deep( $akismet_values ) );
update_post_meta( $post_id, '_feedback_email', $this->addslashes_deep( compact( 'to', 'message' ) ) );
/**
* Fires right before the contact form message is sent via email to
* the recipient specified in the contact form.
*
* @since ?
* @module Contact_Forms
* @param integer $post_id Post contact form lives on
* @param array $all_values Contact form fields
* @param array $extra_values Contact form fields not included in $all_values
**/
do_action( 'grunion_pre_message_sent', $post_id, $all_values, $extra_values );
// schedule deletes of old spam feedbacks
if ( !wp_next_scheduled( 'grunion_scheduled_delete' ) ) {
wp_schedule_event( time() + 250, 'daily', 'grunion_scheduled_delete' );
}
if ( $is_spam !== TRUE && true === apply_filters( 'grunion_should_send_email', true, $post_id ) ) {
wp_mail( $to, "{$spam}{$subject}", $message, $headers );
} elseif ( true === $is_spam && apply_filters( 'grunion_still_email_spam', FALSE ) == TRUE ) { // don't send spam by default. Filterable.
wp_mail( $to, "{$spam}{$subject}", $message, $headers );
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return self::success_message( $post_id, $this );
}
$redirect = wp_get_referer();
if ( !$redirect ) { // wp_get_referer() returns false if the referer is the same as the current page
$redirect = $_SERVER['REQUEST_URI'];
}
$redirect = add_query_arg( urlencode_deep( array(
'contact-form-id' => $id,
'contact-form-sent' => $post_id,
'_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :(
) ), $redirect );
$redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id );
wp_safe_redirect( $redirect );
exit;
}
function addslashes_deep( $value ) {
if ( is_array( $value ) ) {
return array_map( array( $this, 'addslashes_deep' ), $value );
} elseif ( is_object( $value ) ) {
$vars = get_object_vars( $value );
foreach ( $vars as $key => $data ) {
$value->{$key} = $this->addslashes_deep( $data );
}
return $value;
}
return addslashes( $value );
}
}
/**
* Class for the contact-field shortcode.
* Parses shortcode to output the contact form field as HTML.
* Validates input.
*/
class Grunion_Contact_Form_Field extends Crunion_Contact_Form_Shortcode {
var $shortcode_name = 'contact-field';
/**
* @var Grunion_Contact_Form parent form
*/
var $form;
/**
* @var string default or POSTed value
*/
var $value;
/**
* @var bool Is the input invalid?
*/
var $error = false;
/**
* @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
* @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
* @param Grunion_Contact_Form $form The parent form
*/
function __construct( $attributes, $content = null, $form = null ) {
$attributes = shortcode_atts( array(
'label' => null,
'type' => 'text',
'required' => false,
'options' => array(),
'id' => null,
'default' => null,
'placeholder' => null,
), $attributes, 'contact-field' );
// special default for subject field
if ( 'subject' == $attributes['type'] && is_null( $attributes['default'] ) && !is_null( $form ) ) {
$attributes['default'] = $form->get_attribute( 'subject' );
}
// allow required=1 or required=true
if ( '1' == $attributes['required'] || 'true' == strtolower( $attributes['required'] ) )
$attributes['required'] = true;
else
$attributes['required'] = false;
// parse out comma-separated options list (for selects and radios)
if ( !empty( $attributes['options'] ) && is_string( $attributes['options'] ) ) {
$attributes['options'] = array_map( 'trim', explode( ',', $attributes['options'] ) );
}
if ( $form ) {
// make a unique field ID based on the label, with an incrementing number if needed to avoid clashes
$form_id = $form->get_attribute( 'id' );
$id = isset( $attributes['id'] ) ? $attributes['id'] : false;
$unescaped_label = $this->unesc_attr( $attributes['label'] );
$unescaped_label = str_replace( '%', '-', $unescaped_label ); // jQuery doesn't like % in IDs?
$unescaped_label = preg_replace( '/[^a-zA-Z0-9.-_:]/', '', $unescaped_label );
if ( empty( $id ) ) {
$id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label );
$i = 0;
$max_tries = 24;
while ( isset( $form->fields[$id] ) ) {
$i++;
$id = sanitize_title_with_dashes( 'g' . $form_id . '-' . $unescaped_label . '-' . $i );
if ( $i > $max_tries ) {
break;
}
}
}
$attributes['id'] = $id;
}
parent::__construct( $attributes, $content );
// Store parent form
$this->form = $form;
}
/**
* This field's input is invalid. Flag as invalid and add an error to the parent form
*
* @param string $message The error message to display on the form.
*/
function add_error( $message ) {
$this->is_error = true;
if ( !is_wp_error( $this->form->errors ) ) {
$this->form->errors = new WP_Error;
}
$this->form->errors->add( $this->get_attribute( 'id' ), $message );
}
/**
* Is the field input invalid?
*
* @see $error
*
* @return bool
*/
function is_error() {
return $this->error;
}
/**
* Validates the form input
*/
function validate() {
// If it's not required, there's nothing to validate
if ( !$this->get_attribute( 'required' ) ) {
return;
}
$field_id = $this->get_attribute( 'id' );
$field_type = $this->get_attribute( 'type' );
$field_label = $this->get_attribute( 'label' );
$field_value = isset( $_POST[$field_id] ) ? stripslashes( $_POST[$field_id] ) : '';
switch ( $field_type ) {
case 'email' :
// Make sure the email address is valid
if ( !is_email( $field_value ) ) {
$this->add_error( sprintf( __( '%s requires a valid email address', 'jetpack' ), $field_label ) );
}
break;
default :
// Just check for presence of any text
if ( !strlen( trim( $field_value ) ) ) {
$this->add_error( sprintf( __( '%s is required', 'jetpack' ), $field_label ) );
}
}
}
/**
* Outputs the HTML for this form field
*
* @return string HTML
*/
function render() {
global $current_user, $user_identity;
$r = '';
$field_id = $this->get_attribute( 'id' );
$field_type = $this->get_attribute( 'type' );
$field_label = $this->get_attribute( 'label' );
$field_required = $this->get_attribute( 'required' );
$placeholder = $this->get_attribute( 'placeholder' );
$field_placeholder = ( ! empty( $placeholder ) ) ? "placeholder='" . esc_attr( $placeholder ) . "'" : '';
if ( isset( $_POST[$field_id] ) ) {
$this->value = stripslashes( (string) $_POST[$field_id] );
} elseif (
is_user_logged_in()
&& ( ( defined( 'IS_WPCOM' ) && IS_WPCOM )
|| true === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
)
) {
// Special defaults for logged-in users
switch ( $this->get_attribute( 'type' ) ) {
case 'email';
$this->value = $current_user->data->user_email;
break;
case 'name' :
$this->value = $user_identity;
break;
case 'url' :
$this->value = $current_user->data->user_url;
break;
default :
$this->value = $this->get_attribute( 'default' );
}
} else {
$this->value = $this->get_attribute( 'default' );
}
$field_value = Grunion_Contact_Form_Plugin::strip_tags( $this->value );
$field_label = Grunion_Contact_Form_Plugin::strip_tags( $field_label );
switch ( $field_type ) {
case 'email' :
$r .= "\n
\n";
wp_enqueue_script( 'grunion-frontend', plugins_url( 'js/grunion-frontend.js', __FILE__ ), array( 'jquery', 'jquery-ui-datepicker' ) );
break;
default : // text field
// note that any unknown types will produce a text input, so we can use arbitrary type names to handle
// input fields like name, email, url that require special validation or handling at POST
$r .= "\n
\n";
$r .= "\t\t\n";
$r .= "\t\t\n";
$r .= "\t
\n";
}
return apply_filters( 'grunion_contact_form_field_html', $r, $field_label, ( in_the_loop() ? get_the_ID() : null ) );
}
}
add_action( 'init', array( 'Grunion_Contact_Form_Plugin', 'init' ) );
add_action( 'grunion_scheduled_delete', 'grunion_delete_old_spam' );
/**
* Deletes old spam feedbacks to keep the posts table size under control
*/
function grunion_delete_old_spam() {
global $wpdb;
$grunion_delete_limit = 100;
$now_gmt = current_time( 'mysql', 1 );
$sql = $wpdb->prepare( "
SELECT `ID`
FROM $wpdb->posts
WHERE DATE_SUB( %s, INTERVAL 15 DAY ) > `post_date_gmt`
AND `post_type` = 'feedback'
AND `post_status` = 'spam'
LIMIT %d
", $now_gmt, $grunion_delete_limit );
$post_ids = $wpdb->get_col( $sql );
foreach ( (array) $post_ids as $post_id ) {
# force a full delete, skip the trash
wp_delete_post( $post_id, TRUE );
}
# Arbitrary check points for running OPTIMIZE
# nothing special about 5000 or 11
# just trying to periodically recover deleted rows
$random_num = mt_rand( 1, 5000 );
if ( apply_filters( 'grunion_optimize_table', ( $random_num == 11 ) ) ) {
$wpdb->query( "OPTIMIZE TABLE $wpdb->posts" );
}
# if we hit the max then schedule another run
if ( count( $post_ids ) >= $grunion_delete_limit ) {
wp_schedule_single_event( time() + 700, 'grunion_scheduled_delete' );
}
}