array(),
'tags' => array(),
'exclude' => array(
'bysize' => array(
'width' => 50,
'height' => 50
),
'byplug' => 'on'
),
'date' => ''
);
add_option('ais_options', array());
}
function ais_admin_page(){
global $hook, $ais_image_sizes;
$hook = add_options_page('ais', 'AIS', 8, 'ais', 'ais_page');
$ais_image_sizes = ais_get_image_sizes();
}
function ais_get_image_sizes( $size = '' ) {
global $_wp_additional_image_sizes;
$sizes = array();
$get_intermediate_image_sizes = get_intermediate_image_sizes();
foreach( $get_intermediate_image_sizes as $_size ) {
if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
$sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
$sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
$sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop']
);
}
}
if ( $size ) {
if( isset( $sizes[ $size ] ) ) {
return $sizes[ $size ];
} else {
return false;
}
}
return $sizes;
}
function ais_get_items(){
global $wpdb;
$ais_image_sizes = ais_get_image_sizes();
$ais_options = get_option('ais_options');
$pattern = '~https?://[^/\s]+/\S+\.(jpe?g|png|gif|[tg]iff?|svg)~i';
$page_templates = ais_get_page_templates();
$post_formats = array_merge(get_theme_support( 'post-formats' ), array(array('standard')));
$items = array();
$posts = $wpdb->get_results(
" SELECT p.ID as img_id, p.guid as img_url, pm.post_id as post_id ".
" FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ".
" ON (p.post_status = 'publish' or p.post_status = 'inherit') " .
" AND p.post_type = 'attachment' AND ( " .
"(p.post_mime_type = 'image/jpg') or " .
"(p.post_mime_type = 'image/gif') or " .
"(p.post_mime_type = 'image/jpeg') or " .
"(p.post_mime_type = 'image/png') or " .
"(p.post_mime_type = 'image/svg+xml') or " .
"(p.post_mime_type = 'image/tiff')) " .
" AND pm.meta_value = p.ID AND pm.meta_key = '_thumbnail_id'"
);
if(!empty($posts)){
foreach($posts as $post) {
$images_by_pt[$post->post_id] = $post->img_url;
}
}
unset($posts);
foreach(get_post_types(array('public' => true), 'names') as $pt){
if('page' !== $pt){
foreach( $post_formats as $pf ){
if(!isset($examples[$pt][$pf[0]])){
if('standard' != $pf[0]) $in_query = "t.name = 'post-format-".$pf[0]."'";
else $in_query = "t.name NOT LIKE 'post-format'";
$p = $wpdb->get_results( "SELECT p.ID as pid FROM $wpdb->posts p"
. " INNER JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id"
. " INNER JOIN $wpdb->terms t ON $in_query AND tr.term_taxonomy_id = t.term_id"
. " WHERE p.post_type = '$pt' LIMIT 1");
/******/
@preg_match_all($pattern, @file_get_contents(get_the_permalink($p[0]->pid)), $matches);
$examples[$pt][$pf[0]] = array_unique(array_map('trim', $matches[0]));
$search_key = @array_search($images_by_pt[$p[0]->pid], $examples[$pt][$pf[0]]);
unset($examples[$pt][$pf[0]][$search_key]);
$examples[$pt][$pf[0]]['full'] = '';
$info = @pathinfo($images_by_pt[$p[0]->pid]);
foreach($ais_image_sizes as $k => $size){
if(isset($ais_options['sizes'][$k]) and 'on' === $ais_options['sizes'][$k]){
$search_key = @array_search($info['dirname'].'/'.$info['filename'].'-'.$size['width'].'x'.$size['height'].'.'.$info['extension'], $examples[$pt][$pf[0]]);
unset($examples[$pt][$pf[0]][$search_key]);
$examples[$pt][$pf[0]][$size['width'].'x'.$size['height']] = '';
}
}
unset($p, $matches, $search_key);
/******/
}
else break;
}
} else{
foreach($page_templates as $tpl){
if(!isset($examples[$pt][$tpl])){
$p = $wpdb->get_results( "SELECT p.ID as pid FROM $wpdb->posts p"
. " INNER JOIN $wpdb->postmeta pm ON pm.post_id = p.ID AND pm.meta_value = '$tpl'"
. " WHERE p.post_type = '$pt' LIMIT 1");
/******/
@preg_match_all($pattern, @file_get_contents(get_the_permalink($p[0]->pid)), $matches);
$examples[$pt][$tpl] = array_unique(array_map('trim', $matches[0]));
$search_key = @array_search($images_by_pt[$p[0]->pid], $examples[$pt][$tpl]);
unset($examples[$pt][$tpl][$search_key]);
$examples[$pt][$tpl]['full'] = '';
$info = @pathinfo($images_by_pt[$p[0]->pid]);
foreach($ais_image_sizes as $k => $size){
if(isset($ais_options['sizes'][$k]) and 'on' === $ais_options['sizes'][$k]){
$search_key = @array_search($info['dirname'].'/'.$info['filename'].'-'.$size['width'].'x'.$size['height'].'.'.$info['extension'], $examples[$pt][$tpl]);
unset($examples[$pt][$tpl][$search_key]);
$examples[$pt][$tpl][$size['width'].'x'.$size['height']] = '';
}
}
unset($p, $matches, $search_key);
/******/
}
else break;
}
}
$posts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type ='$pt' AND post_status = 'publish'");
if(!empty($posts)){
foreach($posts as $post) {
if('page' === $pt){
$by = (get_page_template_slug($post->ID)) ? get_page_template_slug($post->ID) : 'default';
}else{
$by = (get_post_format( $post->ID )) ? get_post_format( $post->ID ) : 'standard';
}
$images = $examples[$pt][$by];
if(isset($images_by_pt[$post->ID])){
$images['full'] = $images_by_pt[$post->ID];
$info = pathinfo($images_by_pt[$post->ID]);
foreach($ais_image_sizes as $k => $size){
if(isset($ais_options['sizes'][$k]) and 'on' === $ais_options['sizes'][$k]){
$images[$size['width'].'x'.$size['height']] = $info['dirname'].'/'.$info['filename'].'-'.$size['width'].'x'.$size['height'].'.'.$info['extension'];
}
}
}
if(!empty($images)){
$items[$post->ID] = array(
'link' => get_the_permalink($post->ID),
'images' => array_filter($images)
);
unset($images);
}
}
}
}
return $items;
}
function ais_get_xml(){
$xml = '';
$items = ais_get_items();
if(!empty($items)){
$ais_options = get_option('ais_options');
$xml .= ''."\n";
$xml .= ''."\n";
$xml .= '