array(),
'tags' => array(),
'ctags' => 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_urls(){
global $wpdb;
$urls = array(home_url());
$turls = array();
$purls = array();
foreach( @array_merge(get_post_types(array('public' => true, '_builtin' => false)), array('post', 'page')) as $pt ){
/* archive urls */
$purls[] = get_post_type_archive_link($pt);
/* single urls */
if($posts = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = '$pt' AND post_status = 'publish'")){
foreach($posts as $pid) if($purl = get_permalink($pid)) $purls[] = $purl;
}
}
/* terms urls */
foreach(get_taxonomies(array('public' => true)) as $tax){
if($terms = get_terms(array('taxonomy' => $tax, 'hide_empty' => false))){
foreach($terms as $term){
$turl = get_term_link($term, $tax);
if(!is_wp_error($turl)) $turls[] = $turl;
}
}
}
$urls = array_merge($urls, $purls);
$urls = array_merge($urls, $turls);
return array_unique($urls);
}
function ais_get_images($urls = array()){
$curl = new Zebra_cURL();
$curl->cache('cache', 3600);
$options = get_option('ais_options');
$tags = implode( '|', array_merge(array('alt', 'title', 'src'), (array)$options['ctags']) );
$images = array();
if(!empty($urls)){
foreach($urls as $url){
$html = $curl->scrap($url);
/*@preg_match_all('~https?://[^/\s]+/\S+\.(jpe?g|png|gif|[tg]iff?|svg)~i', $html->body, $matches);*/
if(@preg_match_all('/
]+>/i', $html->body, $result)){
foreach( $result as $imgs ){
foreach( $imgs as $i => $img){
if(@preg_match_all('/('.$tags.')="([^"]*)"/i',$img, $match)){
foreach($match[1] as $k => $tag){
$images[$url][$i][$tag] = $match[2][$k];
}
}
}
}
}
}
}
return $images;
}
function ais_get_xml($images = array()){
$xml = '';
$options = get_option('ais_options');
$tags = array_merge(array('alt', 'title'), (array)$options['ctags']);
if(!empty($images)){
$xml .= ''."\n";
$xml .= ''."\n";
$xml .= ''."\n";
foreach($images as $url => $imgs){
$xml .= "\n$url\n";
foreach($imgs as $k => $img){
if( ais_exclude_check($img['src'], $options) ){
$xml .= "\n";
$xml .= "". htmlspecialchars($img['src']) ."\n";
foreach(array_filter($options['tags']) as $tname => $tvalue){
if(@preg_match_all('/\%(.*)\%/', $tvalue, $matches)){
foreach($matches[0] as $j => $tag){
$tvalue = str_ireplace($tag, (string)$img[mb_strtolower($matches[1][$j], 'UTF-8')], $tvalue);
}
}
$xml .= "" . $tvalue . "\n";
}
$xml .= "\n";
}
}
$xml .= "\n";
}
$xml .= "\n";
}
return $xml;
}
function ajax_ais_generate(){
if(function_exists('current_user_can') && !current_user_can('manage_options') )
wp_die();
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'ajax_ais_generate_nonce' ) )
wp_die();
include_once( plugin_dir_path( __FILE__ ) . 'includes/zcurl.php' );
$urls = ais_get_urls();
$images = ais_get_images($urls);
$xml = ais_get_xml($images);
if($xml){
$ais_options = get_option('ais_options');
$file = '%s/sitemap-image.xml';
$sitemap_path = sprintf($file, $_SERVER["DOCUMENT_ROOT"]);
echo $sitemap_path;
if(ais_is_writable($_SERVER["DOCUMENT_ROOT"]) && ais_is_writable($sitemap_path)) {
if(file_put_contents($sitemap_path, $xml)) {
$ais_options['date'] = date("d/m/Y H:i:s");
update_option('ais_options', $ais_options);
}else{
wp_send_json_error( array( 'error' => __( 'Failure! Cannot save XML', 'ais' ) ) );
}
}else{
wp_send_json_error( array( 'error' => __( 'Failure! Directory isn\'t writable', 'ais' ) ) );
}
}else{
wp_send_json_error( array( 'error' => __( 'Failure! Cannot create XML', 'ais' ) ) );
}
}
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_imagename($img){
$fileinfo = @pathinfo($img);
$filename = @preg_replace('/\S\d{1,9}x\d{1,9}\S/', '', $fileinfo['filename']);
return ucwords(str_replace(array('-', '_'), ' ', $filename))
}
*/
function ais_exclude_check($img, $options){
$sizes = array_values($options['sizes']);
$plugins_url = plugins_url();
if(!$img){
return false;
}
@preg_match('/\d{1,9}x\d{1,9}/', $img, $match);
if($match[0] && in_array($match[0], $sizes)){
return false;
}
if( 'on' == $options['exclude']['byplug'] && strpos($img, $plugins_url) ){
return false;
}
return true;
}
function ais_xml_entities($xml) {
return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $xml);
}
function ais_is_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;
}
function ais_last_modified(){
$ais_options = get_option('ais_options');
$file = '%s/sitemap-image.xml';
if($ais_options['date'] && file_exists(sprintf($file, ABSPATH))){
printf( '%1$s %2$s %3$s %4$s',
__('Last modify', 'ais'), $ais_options['date'], sprintf($file, get_bloginfo('url')), __('Remove XML', 'ais')
);
}
}
function ajax_ais_remove(){
if(function_exists('current_user_can') && !current_user_can('manage_options') )
wp_die();
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'ajax_ais_remove_nonce' ) )
wp_die();
$ais_options = get_option('ais_options');
if(@unlink(ABSPATH.'/sitemap-image.xml')){
unset($ais_options['date']);
update_option('ais_options', $ais_options);
}else{
wp_send_json_error( array( 'error' => __( 'Failure! Cannot remove XML', 'ais' ) ) );
}
}
function ais_allowed_tags($ctags){
function format($tag){
return '%' . mb_strtoupper($tag, 'UTF-8') . '%';
}
$tags = array_merge(array('alt', 'title'), (array)$ctags);
$tags = array_map('format', $tags);
return implode(', ', $tags);
}
function ais_page(){
global $hook, $ais_image_sizes;
if($hook):
if(isset($_POST['ais_settings_btn'])){
if(function_exists('current_user_can') && !current_user_can('manage_options') )
wp_die();
if (function_exists ('check_admin_referer') )
check_admin_referer($_POST['action'].'_form');
update_option('ais_options', array(
'sizes' => $_POST['sizes'],
'tags' => array_map('sanitize_text_field', $_POST['tags']),
'ctags' => array_map('sanitize_text_field', @explode(',', $_POST['ctags'])),
'exclude' => array(
'bysize' => array(
'width' => absint($_POST['exclude']['bysize']['width']),
'height' => absint($_POST['exclude']['bysize']['height'])
),
'byplug' => $_POST['exclude']['byplug']
),
'date' => ''
));
}
$ais_options = get_option('ais_options');?>