'off', 'image_order_by' => 'date', 'image_order' => 'ASC', 'term_id' => '1', 'order_by' => 'title', 'order' => 'ASC', 'str' => '%', 'limit' => '0,50', 'size' => 'medium', 'design' => '2', 'item' => '9', 'column' => '3', 'date_format' => 'Y-m-d', 'date_show' => 'off', 'title_show' => 'on', 'cache' => 'on', 'section_name' => 'Section', 'section_sort' => 'number', 'section_result_number_show' => 'on' ), $atts ) ); //Substitution //first_image_mode if( ($first_image_mode == 'on') || ($first_image_mode == 'off') ) $this->v_first_image_mode = $first_image_mode; else return "shortcode atts error. first_image_mode is required to be 'on' or 'off'."; //image_order_by if( $image_order_by == 'title' ) $this->v_image_order_by = 'p1.post_title'; elseif( $image_order_by == 'date' ) $this->v_image_order_by = 'p1.post_date'; else return "shortcode atts error. image_order_by is required to be 'title' or 'date'."; //image_order if( ($image_order == 'ASC') || ($image_order == 'DESC') ) $this->v_image_order = $image_order; else return "shortcode atts error. image_order is required to be 'ASC' or 'DESC'."; //term_id $this->v_term_id = $term_id; //order_by if( $order_by == 'title' ) $this->v_order_by = 'post_title'; elseif( $order_by == 'date' ) $this->v_order_by = 'post_date'; else return "shortcode atts error. order_by is required to be 'title' or 'date'."; //order if( ($order == 'ASC') || ($order == 'DESC') ) $this->v_order = $order; else return "shortcode atts error. order is required to be 'ASC' or 'DESC'."; //string $this->v_str = $str; //limit $this->v_limit = $limit; //img_size if( ($size == 'thumbnail') || ($size == 'medium') || ($size == 'large') || ($size == 'full') ) $this->v_img_size = $size; else return "shortcode atts error. size is required to be 'thumbnail' or 'medium' or 'large' or 'full'."; //design $this->v_design = intval( $design ); //item $this->v_item = intval( $item ); //column $column = intval( $column ); if( $column < 1 ) return "the number of 'column' is required to be larger than 0."; elseif( $column > 100 ) return "the number of 'column' is too big."; $this->v_column = $column; //date format $this->v_date_format = $date_format; //date show if( ($date_show == 'on') || ($date_show == 'off') ) $this->v_date_show = $date_show; else return "date_show is required to be 'on' or 'off'."; //title show if( ($title_show == 'on') || ($title_show == 'off') ) $this->v_title_show = $title_show; else return "title_show is required to be 'on' or 'off'."; //cache if( ($cache == 'on') || ($cache == 'off') ) $this->v_cache = $cache; else return "cache is required to be 'on' or 'off'."; //section_name $this->v_section_name = htmlspecialchars($section_name); //section_sort if( ($section_sort == 'category') || ($section_sort == 'number') ) $this->v_section_sort = $section_sort; else return "section_sort is required to be 'category' or 'number'"; //section_result_number_show if( ($section_result_number_show == 'on') || ($section_result_number_show == 'off') ) $this->v_section_result_number_show = $section_result_number_show; else return "section_result_number_show is required to be 'on' or 'off'"; return $this->ia_core(); } //template tag function // cannot use "return". function ia_template_tag ( $args = '' ) { $default = array( 'first_image_mode' => 'off', 'image_order_by' => 'date', 'image_order' => 'ASC', 'term_id' => '1', 'order_by' => 'title', 'order' => 'ASC', 'str' => '%', 'limit' => '0,50', 'size' => 'medium', 'design' => '2', 'item' => '9', 'column' => '3', 'date_format' => 'Y-m-d', 'date_show' => 'off', 'title_show' => 'on', 'cache' => 'on', 'section_name' => 'Section', 'section_sort' => 'number', 'section_result_number_show' => 'on' ); $args = wp_parse_args($args, $default); extract( $args, EXTR_SKIP ); //Substitution //first_image_mode if( ($first_image_mode == 'on') || ($first_image_mode == 'off') ) $this->v_first_image_mode = $first_image_mode; else { echo "shortcode atts error. first_image_mode is required to be 'on' or 'off'."; return; } //image_order_by if( $image_order_by == 'title' ) $this->v_image_order_by = 'p1.post_title'; elseif( $image_order_by == 'date' ) $this->v_image_order_by = 'p1.post_date'; else { echo "shortcode atts error. image_order_by is required to be 'title' or 'date'."; return; } //image_order if( ($image_order == 'ASC') || ($image_order == 'DESC') ) $this->v_image_order = $image_order; else { echo "shortcode atts error. image_order is required to be 'ASC' or 'DESC'."; return; } //term_id $this->v_term_id = $term_id; //order_by if( $order_by == 'title' ) $this->v_order_by = 'post_title'; elseif( $order_by == 'date' ) $this->v_order_by = 'post_date'; else { echo "shortcode atts error. order_by is required to be 'title' or 'date'."; return; } //order if( ($order == 'ASC') || ($order == 'DESC') ) $this->v_order = $order; else { echo "shortcode atts error. order is required to be 'ASC' or 'DESC'."; return; } //string $this->v_str = $str; //limit $this->v_limit = $limit; //img_size if( ($size == 'thumbnail') || ($size == 'medium') || ($size == 'large') || ($size == 'full') ) $this->v_img_size = $size; else { echo "shortcode atts error. size is required to be 'thumbnail' or 'medium' or 'large' or 'full'."; return; } //design $this->v_design = intval( $design ); //item $this->v_item = intval( $item ); //column $column = intval( $column ); if( $column < 1 ) { echo "the number of 'column' is required to be larger than 0."; return; } elseif( $column > 100 ) { echo "the number of 'column' is too big."; return; } $this->v_column = $column; //date format $this->v_date_format = $date_format; //date show if( ($date_show == 'on') || ($date_show == 'off') ) $this->v_date_show = $date_show; else { echo "date_show is required to be 'on' or 'off'."; return; } //title show if( ($title_show == 'on') || ($title_show == 'off') ) $this->v_title_show = $title_show; else { echo "title_show is required to be 'on' or 'off'."; return; } //cache if( ($cache == 'on') || ($cache == 'off') ) $this->v_cache = $cache; else { echo "cache is required to be 'on' or 'off'."; return; } //section_name $this->v_section_name = htmlspecialchars($section_name); //section_sort if( ($section_sort == 'category') || ($section_sort == 'number') ) $this->v_section_sort = $section_sort; else { echo "section_sort is required to be 'category' or 'number'"; return; } //section_result_number_show if( ($section_result_number_show == 'on') || ($section_result_number_show == 'off') ) $this->v_section_result_number_show = $section_result_number_show; else { echo "section_result_number_show is required to be 'on' or 'off'"; return; } //important echo $this->ia_core(); } function ia_settings_write () { $file = WP_PLUGIN_DIR . '/image-archives/settings.ini'; $str = "first_image_mode = \"".$this->v_first_image_mode ."\"\n" ."image_order_by = \"" .$this->v_image_order_by ."\"\n" ."image_order = \"" .$this->v_image_order ."\"\n" ."term_id = \"" .$this->v_term_id ."\"\n" ."order_by = \"" .$this->v_order_by ."\"\n" ."order = \"" .$this->v_order ."\"\n" ."str = \"" .$this->v_str ."\"\n" ."limit = \"" .$this->v_limit ."\"\n" ."img_size = \"" .$this->v_img_size ."\"\n" ."item = \"" .$this->v_item ."\"\n" ."column = \"" .$this->v_column ."\"\n" ."design = \"" .$this->v_design ."\"\n" ."date_format = \"" .$this->v_date_format ."\"\n" ."date_show = \"" .$this->v_date_show ."\"\n" ."title_show = \"" .$this->v_title_show ."\"\n" ."cache = \"" .$this->v_cache ."\"\n" ."section_name = \"" .$this->v_section_name ."\"\n" ."section_sort = \"" .$this->v_section_sort ."\"\n" ."section_result_number_show = \"" .$this->v_section_result_number_show ."\"\n"; if( $fp = fopen( $file, 'w' ) ) { flock( $fp, LOCK_EX ); fwrite( $fp, $str ); flock( $fp, LOCK_UN ); fclose($fp); } else { echo 'the settings file cannot be opened or be created.'; } } function ia_settings_read () { $file = WP_PLUGIN_DIR . '/image-archives/settings.ini'; if( file_exists($file) ) { $ini = parse_ini_file($file); $this->v_first_image_mode = $ini["first_image_mode"]; $this->v_image_order_by = $ini["image_order_by"]; $this->v_image_order = $ini["image_order"]; $this->v_term_id = $ini["term_id"]; $this->v_order_by = $ini["order_by"]; $this->v_order = $ini["order"]; $this->v_str = $ini["str"]; $this->v_limit = $ini["limit"]; $this->v_img_size = $ini["img_size"]; $this->v_item = $ini["item"]; $this->v_column = $ini["column"]; $this->v_design = $ini["design"]; $this->v_date_format = $ini["date_format"]; $this->v_date_show = $ini["date_show"]; $this->v_title_show = $ini["title_show"]; $this->v_cache = $ini["cache"]; $this->v_section_name = $ini["section_name"]; $this->v_section_sort = $ini["section_sort"]; $this->v_section_result_number_show = $ini["section_result_number_show"]; return true; } else { return false; } } function ia_query( &$row_count = 0 ) { global $wpdb; if( $this->v_first_image_mode == 'off' ) { $query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT p1.ID AS image_post_id, p1.post_parent AS parent_article_id, $wpdb->posts.post_title, $wpdb->posts.post_date" . " FROM $wpdb->posts AS p1" . " INNER JOIN $wpdb->term_relationships ON ( $wpdb->term_relationships.object_id = p1.post_parent )" . " INNER JOIN $wpdb->posts ON ( $wpdb->posts.ID = p1.post_parent )" . " INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id )" . " WHERE p1.post_mime_type LIKE 'image%'" . " AND p1.post_type = 'attachment'" . " AND p1.post_status = 'inherit'" . " AND $wpdb->posts.post_status = 'publish'"; if( $this->v_term_id == "ALL" ) $query .= " AND $wpdb->term_taxonomy.term_id IS NOT NULL"; else $query .= " AND $wpdb->term_taxonomy.term_id IN (". $wpdb->escape( $this->v_term_id ) .")"; $query .= " AND p1.post_title LIKE '". $wpdb->escape( $this->v_str ) ."'" . " ORDER BY ". $wpdb->escape( $this->v_order_by ) ." ". $wpdb->escape( $this->v_order ) . " LIMIT ". $wpdb->escape( $this->v_limit ); $query_array = $wpdb->get_results($query, ARRAY_A); // query_array[ROW][ image_post_id / parent_article_id / post_title / post_date ] } elseif( $this->v_first_image_mode == 'on' ) { $query2 = "SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT p1.ID AS image_post_id, p1.post_title AS image_post_title, p1.post_parent AS parent_article_id, $wpdb->posts.post_title, $wpdb->posts.post_date" . " FROM $wpdb->posts AS p1" . " INNER JOIN $wpdb->term_relationships ON ($wpdb->term_relationships.object_id = p1.post_parent)" . " INNER JOIN $wpdb->posts ON ($wpdb->posts.ID = p1.post_parent)" . " INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id )" . " WHERE p1.post_mime_type LIKE 'image%'" . " AND p1.post_type = 'attachment'" . " AND p1.post_status = 'inherit'" . " AND $wpdb->posts.post_status = 'publish'"; if( $this->v_term_id == "ALL" ) $query2 .= " AND $wpdb->term_taxonomy.term_id IS NOT NULL"; else $query2 .= " AND $wpdb->term_taxonomy.term_id IN (". $wpdb->escape( $this->v_term_id ) .")"; $query2 .= " AND p1.post_title LIKE '". $wpdb->escape( $this->v_str ) ."'" . " ORDER BY ". $wpdb->escape( $this->v_image_order_by ) ." ". $wpdb->escape( $this->v_image_order ) .") AS m1" . " GROUP BY parent_article_id" . " ORDER BY ". $wpdb->escape( $this->v_order_by ) ." ". $wpdb->escape( $this->v_order ) . " LIMIT ". $wpdb->escape( $this->v_limit ); $query2_array = $wpdb->get_results($query2, ARRAY_A); // query2_array[ROW][ image_post_id / parent_article_id / post_title / post_date ] //echo $query2; //var_dump($query2_array); } $row_count = $wpdb->num_rows; // get the number of rows without limit and without Wordpress's function. //$query_count = "SELECT FOUND_ROWS();"; //$row_count = $wpdb->get_var($query_count); if( is_array($query_array) || is_array($query2_array) ) { if( $this->v_first_image_mode == 'off' ) return $query_array; if( $this->v_first_image_mode == 'on' ) return $query2_array; } else { return false; } } function ia_core () { if( $this->v_cache == 'on' ) { //$this->ia_settings_write(); $this->ia_cache_file ( $c_dir, $c_file ); if( file_exists($c_file) ) { $content = file_get_contents($c_file); return $content; } else { $this->ia_cache_create(); $content = file_get_contents($c_file); return $content; } } else { return $this->ia_output(); } } function ia_cache_file ( &$cache_dir, &$cache_file ) { $cache_dir = WP_PLUGIN_DIR . '/image-archives/cache'; $str = $this->v_first_image_mode . $this->v_image_order_by . $this->v_image_order . $this->v_term_id . $this->v_order_by . $this->v_order . $this->v_str . $this->v_limit . $this->v_img_size . $this->v_item . $this->v_column . $this->v_design . $this->v_date_format . $this->v_date_show . $this->v_title_show . $this->v_cache . $this->v_section_name . $this->v_section_sort . $this->v_section_result_number_show; $md5 = md5($str); $cache_file = $cache_dir . '/ia-' . $md5; } function ia_cache_create () { $this->ia_cache_file ( $c_dir, $c_file ); // create a cache dir if( !is_dir( $c_dir ) ) { if( !mkdir( $c_dir , 0755 ) ) echo 'failed to creat cache dir.'; } if( $fp = fopen( $c_file, 'w' ) ) { flock( $fp, LOCK_EX ); fwrite( $fp, $this->ia_output() ); flock( $fp, LOCK_UN ); fclose( $fp ); } else { echo 'a cache file cannot be opened or be created.'; } } // not in use function ia_cache_update () { /* After reading "settings.ini", WordPress runs this "cache update" function. * * ia_cache_update() が add_action によって直接呼び出される時、 * $this->v_* に何も値が入っていない。Wordpress のデータベースに値を保存する手段もあるが * それをしてしまうと Image Archives を同じページで二度呼べなくなる。この解決方法として * settings.ini を作り、それを読み出す様にした。 * PHPをセーフモードで使っているとこのプラグインが使えないだろう。 */ if( $this->ia_settings_read() == false ) return; $this->ia_cache_file ( $c_dir, $c_file ); if( $this->v_cache == 'on' && file_exists($c_file) ) $this->ia_cache_create(); } function ia_cache_delete () { $cache_dir = WP_PLUGIN_DIR . '/image-archives/cache'; if ( $handle = @opendir($cache_dir) ) { while( false !== ($file_name = readdir($handle)) ) { if( preg_match("/^ia-/", $file_name) ) unlink($cache_dir ."/". $file_name); } closedir($handle); return true; } else { return false; } } function ia_output () { //send query if( ($this->v_design != 4) || ($this->v_design != 5) ) { $arr = $this->ia_query( $count ); if( !$arr ) return "Query Error. Searching your database was done, but any images were not found. Your 'str'(search strings) may be wrong or your input 'term_id' doesn't exist, or 'limit' may be wrong."; } // OUTPUT if( $this->v_design == 1 ) { $output = "
| \n" . " \n" . " | \n" . "\n";
if( $this->v_title_show == 'on' ) $output .= " \n";
if( $this->v_date_show == 'on' ) $output .= " ( ". date( "$this->v_date_format", strtotime($arr[$i][post_date]) ) ." ) \n";
$output .= " | \n"
. "
| \n"
. " \n";
if( $this->v_title_show == 'on' ) $output .= " \n";
if( $this->v_date_show == 'on' ) $output .= " ( ". date( "$this->v_date_format", strtotime($arr[$i][post_date]) ) ." ) \n";
$output .= " | \n"
. "
| \n"
. " \n";
if( $this->v_title_show == 'on' ) $output .= " \n";
if( $this->v_date_show == 'on' ) $output .= " ( ". date( "$this->v_date_format", strtotime($arr[$i][post_date]) ) ." ) \n";
$output .= " | \n";
if( $i % $this->v_column == $this->v_column - 1 ) $output .= "