From 292154d4c0c8ba35585a629559e2a710f2de4884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Grimelid?= Date: Mon, 30 Mar 2015 15:54:23 +0200 Subject: [PATCH 1/2] Sublime Text auto cleanup, convert tabs to spaces and adjust spacings. --- functions/exporter.php | 172 +++++++++++++------------ settings.php | 280 ++++++++++++++++++++--------------------- templates/settings.php | 24 ++-- wp_ccsve.php | 102 +++++++-------- 4 files changed, 288 insertions(+), 290 deletions(-) diff --git a/functions/exporter.php b/functions/exporter.php index 405da84..a2fd0a3 100644 --- a/functions/exporter.php +++ b/functions/exporter.php @@ -1,102 +1,100 @@ $ccsve_generate_post_type, 'post_status' => 'publish', 'posts_per_page' => -1)); - // Count the number of instances of the custom post type - $ccsve_count_posts = count($ccsve_generate_query); - - // Build an array of the custom field values - $ccsve_generate_value_arr = array(); - $i = 0; - foreach ($ccsve_generate_query as $post): setup_postdata($post); - $post->permalink = get_permalink($post->ID); - $post->post_thumbnail = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); - // get the standard wordpress fields for each instance of the custom post type - foreach($post as $key => $value) { - if(in_array($key, $ccsve_generate_std_fields['selectinput'])) { - $ccsve_generate_value_arr[$key][$i] = $post->$key; - } - } - // get the custom field values for each instance of the custom post type - $ccsve_generate_post_values = get_post_custom($post->ID); - foreach ($ccsve_generate_custom_fields['selectinput'] as $key) { - // check if each custom field value matches a custom field that is being exported - if (array_key_exists($key, $ccsve_generate_post_values)) { - // if the the custom fields match, save them to the array of custom field values - $ccsve_generate_value_arr[$key][$i] = $ccsve_generate_post_values[$key]['0']; - - } - - } - // get custom taxonomy information - foreach($ccsve_generate_tax_terms['selectinput'] as $tax) { - $names = array(); - $terms = wp_get_object_terms($post->ID, $tax); - foreach($terms as $t) { - $names[] = $t->name; - } - $ccsve_generate_value_arr[$tax][$i] = implode(',', $names); - } - $i++; - - endforeach; - // create a new array of values that reorganizes them in a new multidimensional array where each sub-array contains all of the values for one custom post instance - $ccsve_generate_value_arr_new = array(); - - foreach($ccsve_generate_value_arr as $value) { - $i = 0; - while ($i <= ($ccsve_count_posts-1)) { - $ccsve_generate_value_arr_new[$i][] = $value[$i]; - $i++; - } - } + // Get the custom post type that is being exported + $ccsve_generate_post_type = get_option('ccsve_post_type'); + // Get the custom fields (for the custom post type) that are being exported + $ccsve_generate_custom_fields = get_option('ccsve_custom_fields'); + $ccsve_generate_std_fields = get_option('ccsve_std_fields'); + $ccsve_generate_tax_terms = get_option('ccsve_tax_terms'); + // Query the DB for all instances of the custom post type + $ccsve_generate_query = get_posts(array('post_type' => $ccsve_generate_post_type, 'post_status' => 'publish', 'posts_per_page' => -1)); + // Count the number of instances of the custom post type + $ccsve_count_posts = count($ccsve_generate_query); - // build a filename based on the post type and the data/time - $ccsve_generate_csv_filename = $ccsve_generate_post_type.'-'.date('Ymd_His').'-export.csv'; - - //output the headers for the CSV file - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); - header('Content-Description: File Transfer'); - header("Content-type: text/csv"); - header("Content-Disposition: attachment; filename={$ccsve_generate_csv_filename}"); - header("Expires: 0"); - header("Pragma: public"); - - //open the file stream - $fh = @fopen( 'php://output', 'w' ); - - $headerDisplayed = false; - - foreach ( $ccsve_generate_value_arr_new as $data ) { - // Add a header row if it hasn't been added yet -- using custom field keys from first array - if ( !$headerDisplayed ) { - fputcsv($fh, array_keys($ccsve_generate_value_arr)); - $headerDisplayed = true; + // Build an array of the custom field values + $ccsve_generate_value_arr = array(); + $i = 0; + foreach ($ccsve_generate_query as $post): setup_postdata($post); + $post->permalink = get_permalink($post->ID); + $post->post_thumbnail = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); + // get the standard wordpress fields for each instance of the custom post type + foreach($post as $key => $value) { + if(in_array($key, $ccsve_generate_std_fields['selectinput'])) { + $ccsve_generate_value_arr[$key][$i] = $post->$key; } - + } + // get the custom field values for each instance of the custom post type + $ccsve_generate_post_values = get_post_custom($post->ID); + foreach ($ccsve_generate_custom_fields['selectinput'] as $key) { + // check if each custom field value matches a custom field that is being exported + if (array_key_exists($key, $ccsve_generate_post_values)) { + // if the the custom fields match, save them to the array of custom field values + $ccsve_generate_value_arr[$key][$i] = $ccsve_generate_post_values[$key]['0']; + + } + + } + // get custom taxonomy information + foreach($ccsve_generate_tax_terms['selectinput'] as $tax) { + $names = array(); + $terms = wp_get_object_terms($post->ID, $tax); + foreach($terms as $t) { + $names[] = $t->name; + } + $ccsve_generate_value_arr[$tax][$i] = implode(',', $names); +} +$i++; + +endforeach; + // create a new array of values that reorganizes them in a new multidimensional array where each sub-array contains all of the values for one custom post instance +$ccsve_generate_value_arr_new = array(); + +foreach($ccsve_generate_value_arr as $value) { + $i = 0; + while ($i <= ($ccsve_count_posts-1)) { + $ccsve_generate_value_arr_new[$i][] = $value[$i]; + $i++; +} +} + + // build a filename based on the post type and the data/time +$ccsve_generate_csv_filename = $ccsve_generate_post_type.'-'.date('Ymd_His').'-export.csv'; + + //output the headers for the CSV file +header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); +header('Content-Description: File Transfer'); +header("Content-type: text/csv"); +header("Content-Disposition: attachment; filename={$ccsve_generate_csv_filename}"); +header("Expires: 0"); +header("Pragma: public"); + + //open the file stream +$fh = @fopen( 'php://output', 'w' ); + +$headerDisplayed = false; + +foreach ( $ccsve_generate_value_arr_new as $data ) { + // Add a header row if it hasn't been added yet -- using custom field keys from first array + if ( !$headerDisplayed ) { + fputcsv($fh, array_keys($ccsve_generate_value_arr)); + $headerDisplayed = true; + } + // Put the data from the new multi-dimensional array into the stream - fputcsv($fh, $data); + fputcsv($fh, $data); } // Close the file stream fclose($fh); // Make sure nothing else is sent, our file is done exit; - } +} diff --git a/settings.php b/settings.php index 14c00d4..e5941b7 100644 --- a/settings.php +++ b/settings.php @@ -1,210 +1,210 @@ true, - ); + + $args=array( + 'public' => true, + ); // Get the field name from the $args array - $items = get_post_types($args); + $items = get_post_types($args); // Get the value of this setting - $options = get_option('ccsve_post_type'); + $options = get_option('ccsve_post_type'); // echo a proper input type="text" - foreach ($items as $item) { - $checked = ($options==$item) ? ' checked="checked" ' : ''; - echo ''; - echo ''; - echo '
'; - } + foreach ($items as $item) { + $checked = ($options==$item) ? ' checked="checked" ' : ''; + echo ''; + echo ''; + echo '
'; + } } // END public function settings_field_input_text($args) public function settings_field_select_std_fields() { - $ccsve_post_type = get_option('ccsve_post_type'); - $fields = generate_std_fields($ccsve_post_type); - $ccsve_std_fields =get_option('ccsve_std_fields'); - $ccsve_std_fields_num = count($fields); - echo ''; + foreach ($fields as $field) { + if (in_array($field, $ccsve_std_fields['selectinput'])){ + echo '\n\t'; + } else { + echo '\n\t\'; } } // END public function settings_field_input_text($args) } // END public function settings_field_Select_std_fields() - + public function settings_field_select_tax_terms() { - $ccsve_post_type = get_option('ccsve_post_type'); - $object_tax = get_object_taxonomies($ccsve_post_type, 'names'); - $ccsve_tax_terms =get_option('ccsve_tax_terms'); - $ccsve_tax_terms_num = count($object_tax); - echo ''; + foreach ($object_tax as $tax) { + if (in_array($tax, $ccsve_tax_terms['selectinput'])){ + echo '\n\t'; + } else { + echo '\n\t\'; } } // END public function settings_field_input_text($args) } // END public function settings_field_Select_std_fields() - + public function settings_field_select_custom_fields() { - $ccsve_post_type = get_option('ccsve_post_type'); - $meta_keys = get_post_meta_keys($ccsve_post_type); - $ccsve_custom_fields =get_option('ccsve_custom_fields'); - $ccsve_meta_keys_num = count($meta_keys); - echo ''; + foreach ($meta_keys as $meta_key) { + if (in_array($meta_key, $ccsve_custom_fields['selectinput'])){ + echo '\n\t'; + } else { + echo '\n\t\'; } } // END public function settings_field_input_text($args) - + /** * add a menu - */ - } - public function add_menu() - { + */ + } + public function add_menu() + { // Add a page to manage this plugin's settings - add_options_page( - 'CCSVE Settings', - 'Custom CSV', - 'manage_options', - 'wp_ccsve_template', - array(&$this, 'plugin_settings_page') - ); + add_options_page( + 'CCSVE Settings', + 'Custom CSV', + 'manage_options', + 'wp_ccsve_template', + array(&$this, 'plugin_settings_page') + ); } // END public function add_menu() - + /** * Menu Callback - */ + */ public function plugin_settings_page() { - if(!current_user_can('manage_options')) - { - wp_die(__('You do not have sufficient permissions to access this page.')); - } - - // Render the settings template - include(sprintf("%s/templates/settings.php", dirname(__FILE__))); + if(!current_user_can('manage_options')) + { + wp_die(__('You do not have sufficient permissions to access this page.')); + } + + // Render the settings template + include(sprintf("%s/templates/settings.php", dirname(__FILE__))); } // END public function plugin_settings_page() } // END class wp_ccsve_template_Settings } // END if(!class_exists('wp_ccsve_template_Settings')) function generate_post_meta_keys($post_type){ -global $wpdb; - $query = " - SELECT DISTINCT($wpdb->postmeta.meta_key) - FROM $wpdb->posts - LEFT JOIN $wpdb->postmeta - ON $wpdb->posts.ID = $wpdb->postmeta.post_id - WHERE $wpdb->posts.post_type = '%s' - AND $wpdb->postmeta.meta_key != '' - AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)' - AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)' - "; - $meta_keys = $wpdb->get_col($wpdb->prepare($query, $post_type)); + global $wpdb; + $query = " + SELECT DISTINCT($wpdb->postmeta.meta_key) + FROM $wpdb->posts + LEFT JOIN $wpdb->postmeta + ON $wpdb->posts.ID = $wpdb->postmeta.post_id + WHERE $wpdb->posts.post_type = '%s' + AND $wpdb->postmeta.meta_key != '' + AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)' + AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)' + "; + $meta_keys = $wpdb->get_col($wpdb->prepare($query, $post_type)); set_transient($post_type.'post_meta_keys', $meta_keys, 60*60*24); # 1 Day Expiration return $meta_keys; -} + } -function generate_std_fields($post_type){ + function generate_std_fields($post_type){ $fields = array('permalink', 'post_thumbnail'); $q = new WP_Query(array('post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => 1)); $p = $q->posts[0]; - + foreach($p as $f => $v) { - $fields[] = $f; + $fields[] = $f; } return $fields; -} + } -function get_post_meta_keys($post_type){ + function get_post_meta_keys($post_type){ $cache = get_transient($post_type.'post_meta_keys'); $meta_keys = $cache ? $cache : generate_post_meta_keys($post_type); return $meta_keys; -} + } -function ccsve_checkboxes_fix($input) { + function ccsve_checkboxes_fix($input) { $options = get_option('ccsve_custom_fields'); $merged = $options; $merged[] = $input; -} \ No newline at end of file + } \ No newline at end of file diff --git a/templates/settings.php b/templates/settings.php index 839cefe..039b7d7 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -1,14 +1,14 @@
-

Custom CSV Exporter Settings

-
- - - - - - - - Export - -
+

Custom CSV Exporter Settings

+
+ + + + + + + + Export + +
\ No newline at end of file diff --git a/wp_ccsve.php b/wp_ccsve.php index 61d8558..5a925a1 100644 --- a/wp_ccsve.php +++ b/wp_ccsve.php @@ -1,7 +1,7 @@ Settings'; - array_unshift($links, $settings_link); - return $links; - } - - $plugin = plugin_basename(__FILE__); - add_filter("plugin_action_links_$plugin", 'plugin_settings_link'); + function plugin_settings_link($links) + { + $settings_link = 'Settings'; + array_unshift($links, $settings_link); + return $links; } + + $plugin = plugin_basename(__FILE__); + add_filter("plugin_action_links_$plugin", 'plugin_settings_link'); + } } From 1c5c7c52b1fa78f7ac10d83b7af7ae718d196a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Grimelid?= Date: Mon, 30 Mar 2015 16:09:02 +0200 Subject: [PATCH 2/2] Move to Tools menu and change menu item name. --- settings.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/settings.php b/settings.php index e5941b7..2e2eb73 100644 --- a/settings.php +++ b/settings.php @@ -142,9 +142,10 @@ public function settings_field_select_custom_fields() public function add_menu() { // Add a page to manage this plugin's settings - add_options_page( + add_submenu_page( + 'tools.php', 'CCSVE Settings', - 'Custom CSV', + 'CSV Content Export', 'manage_options', 'wp_ccsve_template', array(&$this, 'plugin_settings_page')