Skip to content

Instantly share code, notes, and snippets.

@zenith6
Created April 21, 2016 05:51
Show Gist options
  • Save zenith6/ad3088eff369dae38b5ece4add16c35d to your computer and use it in GitHub Desktop.
Save zenith6/ad3088eff369dae38b5ece4add16c35d to your computer and use it in GitHub Desktop.
MW WP Form メモリ使用量軽減パッチ
diff --git a/src/wp-content/plugins/mw-wp-form/classes/controllers/class.chart.php b/src/wp-content/plugins/mw-wp-form/classes/controllers/class.chart.php
index 65dd853..72e091d 100644
--- a/src/wp-content/plugins/mw-wp-form/classes/controllers/class.chart.php
+++ b/src/wp-content/plugins/mw-wp-form/classes/controllers/class.chart.php
@@ -91,6 +91,27 @@ class MW_WP_Form_Chart_Controller extends MW_WP_Form_Controller {
}
/**
+ * データ件数を取得
+ *
+ * @param string $post_type 投稿タイプ名
+ *
+ * @return int 投稿数
+ */
+ protected function get_count( $post_type ) {
+ $_args = apply_filters( 'mwform_get_inquiry_data_args-' . $post_type, array() );
+ $args = array(
+ 'post_type' => $post_type,
+ 'posts_per_page' => 1,
+ );
+ if ( !empty( $_args ) && is_array( $_args ) ) {
+ $args = array_merge( $_args, $args );
+ }
+ $query = new WP_Query( $args );
+
+ return $query->found_posts;
+ }
+
+ /**
* グラフページを表示
*/
public function index() {
@@ -98,32 +119,45 @@ class MW_WP_Form_Chart_Controller extends MW_WP_Form_Controller {
// form_posts
$default_args = array(
- 'posts_per_page' => -1,
+ 'post_type' => $post_type,
);
$_args = apply_filters( 'mwform_get_inquiry_data_args-' . $post_type, $default_args );
$args = array(
- 'post_type' => $post_type,
);
if ( !empty( $_args ) && is_array( $_args ) ) {
$args = array_merge( $_args, $args );
} else {
$args = array_merge( $_args, $default_args );
}
- $form_posts = get_posts( $args );
// custom_keys
$custom_keys = array();
- foreach ( $form_posts as $post ) {
- $post_custom_keys = get_post_custom_keys( $post->ID );
- if ( is_array( $post_custom_keys ) ) {
- foreach ( $post_custom_keys as $post_custom_key ) {
- if ( preg_match( '/^_/', $post_custom_key ) ) {
- continue;
+ $posts_count = 0;
+ $posts_per_page = 500;
+ $page_total = ceil( $this->get_count( $post_type ) / $posts_per_page );
+
+ for ($paged = 1; $paged <= $page_total; $paged++) {
+ $args['posts_per_page'] = $posts_per_page;
+ $args['paged'] = $paged;
+ $form_posts = get_posts( $args );
+
+ foreach ( $form_posts as $post ) {
+ $post_custom_keys = get_post_custom_keys( $post->ID );
+ if ( is_array( $post_custom_keys ) ) {
+ foreach ( $post_custom_keys as $post_custom_key ) {
+ if ( preg_match( '/^_/', $post_custom_key ) ) {
+ continue;
+ }
+ $post_meta = get_post_meta( $post->ID, $post_custom_key, true );
+ $custom_keys[$post_custom_key][$post_meta] = (isset($custom_keys[ $post_custom_key ][ $post_meta ]) ? $custom_keys[ $post_custom_key ][ $post_meta ] : 0) + 1;
}
- $post_meta = get_post_meta( $post->ID, $post_custom_key, true );
- $custom_keys[$post_custom_key][$post_meta][] = $post->ID;
}
}
+
+ $posts_count += count( $form_posts );
+
+ wp_reset_query();
+ wp_cache_flush();
}
// postdata
@@ -142,9 +176,10 @@ class MW_WP_Form_Chart_Controller extends MW_WP_Form_Controller {
$this->assign( 'post_type' , $post_type );
$this->assign( 'option_group', $this->option_group );
- $this->assign( 'form_posts' , $form_posts );
+ // $this->assign( 'form_posts' , $form_posts ); # 投稿件数以外は不要のためコメントアウト
+ $this->assign( 'posts_count', $posts_count ); # 投稿件数
$this->assign( 'custom_keys' , $custom_keys );
$this->assign( 'postdata' , $postdata );
$this->render( 'chart/index' );
}
-}
\ No newline at end of file
+}
diff --git a/src/wp-content/plugins/mw-wp-form/classes/models/class.csv.php b/src/wp-content/plugins/mw-wp-form/classes/models/class.csv.php
index 8bac830..ce2a643 100644
--- a/src/wp-content/plugins/mw-wp-form/classes/models/class.csv.php
+++ b/src/wp-content/plugins/mw-wp-form/classes/models/class.csv.php
@@ -56,16 +56,15 @@ class MW_WP_Form_CSV {
return;
}
- $posts_per_page = $this->get_posts_per_page();
- $paged = $this->get_paged();
-
$file_name = 'mw_wp_form_' . date( 'YmdHis' ) . '.csv';
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename=' . $file_name );
$download_all = isset( $_POST['download-all'] ) && $_POST['download-all'] === 'true';
- $first_paged = $download_all ? 1 : $paged;
- $last_paged = $download_all && $posts_per_page > 0 ? ceil( $this->get_count( $this->post_type ) / $posts_per_page ) : $paged;
+ $posts_per_page = $download_all ? 500 : $this->get_posts_per_page();
+ $paged = $this->get_paged();
+ $first_paged = $download_all ? 1 : $paged;
+ $last_paged = $download_all && $posts_per_page > 0 ? ceil( $this->get_count( $this->post_type ) / $posts_per_page ) : $paged;
$_args = apply_filters( 'mwform_get_inquiry_data_args-' . $this->post_type, array() );
$args = array(
@@ -109,6 +108,9 @@ class MW_WP_Form_CSV {
$to_encoding = apply_filters( 'mwform_csv_encoding-' . $this->post_type, 'sjis-win' );
$csv = mb_convert_encoding( $csv, $to_encoding, get_option( 'blog_charset' ) );
echo $csv;
+
+ wp_reset_query();
+ wp_cache_flush();
}
exit;
diff --git a/src/wp-content/plugins/mw-wp-form/templates/chart/index.php b/src/wp-content/plugins/mw-wp-form/templates/chart/index.php
index 06c16c0..044d2b1 100644
--- a/src/wp-content/plugins/mw-wp-form/templates/chart/index.php
+++ b/src/wp-content/plugins/mw-wp-form/templates/chart/index.php
@@ -69,7 +69,7 @@
<div class="%s" style="width: 100%%; max-width: 800px"></div>',
esc_html( $chart['target'] ),
esc_html__( 'The number of inquiries', 'mw-wp-form' ),
- count( $form_posts ),
+ $posts_count,
esc_attr( MWF_Config::NAME . '-chart-div-' . $postdata_key )
);
}
@@ -88,9 +88,9 @@
$_item = '(Empty)';
}
if ( empty( $raw_data[$_item] ) ) {
- $raw_data[$_item] = count( $values );
+ $raw_data[$_item] = $values;
} else {
- $raw_data[$_item] += count( $values );
+ $raw_data[$_item] += $values;
}
}
} else {
@@ -98,16 +98,16 @@
$item = '(Empty)';
}
if ( empty( $raw_data[$item] ) ) {
- $raw_data[$item] = count( $values );
+ $raw_data[$item] = $values;
} else {
- $raw_data[$item] += count( $values );
+ $raw_data[$item] += $values;
}
}
}
$data[] = array( '', '' );
foreach ( $raw_data as $raw_data_key => $raw_data_value ) {
if ( $chart['chart'] === 'bar' ) {
- $value = $raw_data_value / count( $form_posts );
+ $value = $raw_data_value / $posts_count;
} else {
$value = $raw_data_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment