Skip to content

Instantly share code, notes, and snippets.

@spencermorin
Last active December 22, 2015 18:19
Show Gist options
  • Save spencermorin/6511911 to your computer and use it in GitHub Desktop.
Save spencermorin/6511911 to your computer and use it in GitHub Desktop.
<?php
WP_CLI::add_command( 'export-feedback', 'Export_Feedback_Command' );
class Export_Feedback_Command extends WP_CLI_Command {
private $args = array();
/**
* This command can be used to export all feedbacks for the current site.
*
* @subcommand all
* @synopsis <no arguments>
*/
public function all( $args, $args_assoc ) {
$this->create_export();
}
/**
* This command can be used to export feedback for a form on a specific post.
*
* @subcommand form
* @synopsis [--post_id=<post_id>]
*/
public function form( $args, $args_assoc ) {
if( empty( $args_assoc['post_id'] ) )
WP_CLI::error( 'You must pass a post id!' );
$this->args['post_id'] = intval( $args_assoc['post_id'] );
$this->create_export();
}
private function create_export() {
$args = array(
'posts_per_page' => 100,
'post_type' => 'feedback',
'post_status' => 'publish',
'meta_key' => '_feedback_subject',
'orderby' => 'meta_value',
'fields' => 'ids',
'suppress_filters' => 'false'
);
if( ! empty( $this->args['post_id'] ) )
$args['post_parent'] = $this->args['post_id'];
$filename = date( 'Y-m-d' ) . '-feedback-export.csv';
$filename = sanitize_file_name( $filename );
$handle = fopen( $filename, 'w' );
$all_feedbacks_ids = array();
while( $feedbacks_ids = get_posts( $args ) ) {
$args['offset'] += $args['posts_per_page'];
$all_feedbacks_ids = array_merge( $feedbacks_ids, $all_feedbacks_ids );
// Clear the cache on each iteration since we could be here awhile.
$GLOBALS['wp_object_cache']->cache = array();
}
if ( empty( $all_feedbacks_ids ) )
WP_CLI::error( 'No Feedbacks found.' );
else
WP_CLI::line( 'Export started. This may take awhile.' );
$fields = $this->get_field_names( $all_feedbacks_ids );
array_unshift( $fields, __( 'Contact Form', 'jetpack' ) );
// Prints the header
fputcsv( $handle, $fields );
// Create the csv string from the array of post id
foreach ( $all_feedbacks as $feedback ) {
fputcsv( $handle, $this->make_csv_row_from_feedback( $feedback, $fields ) );
$GLOBALS['wp_object_cache']->cache = array();
}
fclose( $handle );
WP_CLI::success( "All done!" );
}
/*
* Creates a valid csv row from a post id
*
* @param int $post_id The id of the post
* @param array $fields An array containing the names of all the fields of the csv
* @return String The csv row
*/
protected function make_csv_row_from_feedback( $post_id, $fields ) {
$all_fields = get_post_meta( $post_id, '_feedback_all_fields', true );
// The first element in all of the exports will be the subject
$row_items[] = get_post_meta( $post_id, '_feedback_subject', true );
// Loop the fields array in order to fill the $row_items array correctly
foreach ( $fields as $field ) {
if ( $field === __( 'Contact Form', 'jetpack' ) ) // the first field will ever be the contact form, so we can continue
continue;
elseif ( array_key_exists( $field, $all_fields ) )
$row_items[] = $all_fields[ $field ];
else
$row_items[] = '';
}
return $row_items;
}
/*
* Get the names of all the form's fields
*
* @param array|int $posts the post we want the fields of
* @return array the array of fields
*/
protected function get_field_names( $posts ) {
$posts = (array) $posts;
$all_fields = array();
foreach ( $posts as $post ) {
$extra_fields = array_keys( get_post_meta( $post, '_feedback_all_fields', true ) );
$all_fields = array_merge( $all_fields, $extra_fields );
$GLOBALS['wp_object_cache']->cache = array();
$all_fields = array_unique( $all_fields );
return $all_fields;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment