array(),
'tags' => array(),
'exclude' => array(
'bysize' => array(
'width' => 50,
'height' => 50
),
'byplug' => 'on'
),
'date' => ''
);
add_option('ais_options', array());
}
function ais_admin_page(){
if( 'ais' == $_GET['page']):?>
$post['sizes'],
'tags' => array_map('sanitize_text_field', $post['tags']),
'exclude' => array(
'bysize' => array(
'width' => absint($post['exclude']['bysize']['width']),
'height' => absint($post['exclude']['bysize']['height'])
),
'byplug' => $post['exclude']['byplug']
),
'date' => ''
));
}
if(isset($post['ais_generate_btn'])){
if(function_exists('current_user_can') && !current_user_can('manage_options') ) die ( _e('Hacker?', 'ais') );
if (function_exists ('check_admin_referer') ) check_admin_referer($post['action'].'_form');
ais_xml_generate();
}
}
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_xml_generate(){
global $wpdb, $ais_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')));
$image_sitemap_url = $_SERVER["DOCUMENT_ROOT"].'/sitemap-image.xml';
$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);
$date = date("d/m/Y H:i:s");
$xml = ''."\n";
$xml .= ''."\n";
$xml .= ''."\n";
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)){
$xml .= "\n\n" . get_the_permalink($post->ID) . "\n";
foreach(array_filter($images) as $k => $img){
if($img){
$file['info'] = @pathinfo($img);
$file['size'] = @getimagesize($img);
$filename = $file['info']['filename'];
if( $ais_options['exclude']['bysize']['width'] > $file['size'][0] ){
$filename = null;
}
if( $ais_options['exclude']['bysize']['height'] > $file['size'][1] ){
$filename = null;
}
if( 'on' === $ais_options['exclude']['byplug'] and strpos($file['info']['dirname'], 'plugins') ){
$filename = null;
}
}
if($filename){
$xml .= "\n";
$xml .= "$img";
foreach(array_filter($ais_options['tags']) as $tname => $tvalue){
$xml .= "" . str_replace('%NAME%', ucwords(str_replace(array('-', '_'), ' ', $filename)), $tvalue) . "\n";
}
$xml .= "\n";
}
unset($img, $file, $filename);
}
$xml .= "\n";
unset($images);
}
}
}
}
$xml .= "\n";
if(ais_is_sitemap_writable($_SERVER["DOCUMENT_ROOT"]) || ais_is_sitemap_writable($image_sitemap_url)) {
if(file_put_contents($image_sitemap_url, $xml)) {
$ais_options['date'] = $date;
update_option('ais_options', $ais_options);
}
}
}
function ais_get_page_templates(){
global $wpdb;
$templs = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' ");
foreach($templs as $templ){
$data[] = $templ->meta_value;
}
return $data;
}
function ais_exclude_check($img, $exclude = array()){
if($img){
$img_info = @pathinfo($img);
$img_size = @getimagesize($img);
if( $exclude['bysize']['width'] > $img_size[0] ){
return false;
}
if( $exclude['bysize']['height'] > $img_size[1] ){
return false;
}
if( 'on' === $exclude['byplug'] and strpos($img_info['dirname'], 'plugins') ){
return false;
}
return ucwords(str_replace(array('-', '_'), ' ', $img_info['filename']));
}
else return false;
}
function ais_xml_entities($xml) {
return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $xml);
}
function ais_is_sitemap_writable($filename) {
if(!is_writable($filename)) {
if(!@chmod($filename, 0666)) {
$pathtofilename = dirname($filename);
if(!is_writable($pathtofilename)) {
if(!@chmod($pathtoffilename, 0666)) {
return false;
}
}
}
}
return true;
}