1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | Plugin Name: SEO Optimized Images |
---|
5 | Plugin URI: http://webriti.com |
---|
6 | Description: The **SEO Optimized Images** plugin Seo Optmized Images Plugin lets you dynamically insert Seo Friendly alt attributes and title attributes to your Images . Simply activate the plugin, provide the pattern and you are ready to go. |
---|
7 | Version: 2.1.3 |
---|
8 | Author: priyanshu.mittal |
---|
9 | Author URI: http://webriti.com |
---|
10 | Text Domain: seo-optimized-images |
---|
11 | Domain Path: /lang |
---|
12 | */ |
---|
13 | |
---|
14 | // Plugin Root File. |
---|
15 | if ( ! defined( 'SEO_IMAGES_LITE_PLUGIN_FILE' ) ) { |
---|
16 | define( 'SEO_IMAGES_LITE_PLUGIN_FILE', __FILE__ ); |
---|
17 | } |
---|
18 | |
---|
19 | add_action('admin_menu', 'soi_add_menu_page'); |
---|
20 | function soi_add_menu_page() |
---|
21 | { |
---|
22 | $seoimageslite_lang_dir = dirname( plugin_basename( SEO_IMAGES_LITE_PLUGIN_FILE ) ) . '/lang/'; |
---|
23 | load_plugin_textdomain( 'seo-optimized-images', false, $seoimageslite_lang_dir ); |
---|
24 | |
---|
25 | add_menu_page( 'soi_settings_page', __('Seo Optimized Images','seo-optimized-images'), 'administrator', 'soi_setting','soi_create_setting_page',''); |
---|
26 | } |
---|
27 | |
---|
28 | function soi_create_setting_page() |
---|
29 | { |
---|
30 | require_once('seo-optimized-images-settings.php'); |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | add_action( 'admin_enqueue_scripts', 'soi_load_custom_wp_admin_style' ); |
---|
35 | |
---|
36 | function soi_load_custom_wp_admin_style($hook) { |
---|
37 | if ($hook != 'toplevel_page_soi_setting'){return;} // we dont want to load our css on other pages |
---|
38 | wp_register_style ('soi_custom_wp_admin_css', plugins_url('css/plugin-admin-panel.css', __FILE__)); |
---|
39 | wp_enqueue_style( 'soi_custom_wp_admin_css' ); |
---|
40 | wp_enqueue_style( 'wp-color-picker' ); // here we add the color picker style for use in our plugin |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | function soi_load_custom_wp_admin_scripts($hook) { |
---|
47 | |
---|
48 | |
---|
49 | if ($hook != 'toplevel_page_soi_setting'){return;} // we dont want to load our js on other pages |
---|
50 | |
---|
51 | wp_register_script( 'soi_custom_wp_admin_js', plugin_dir_url( __FILE__ ) . 'js/plugin-admin-panel.js',array('jquery','jquery-ui-core','jquery-ui-tabs','wp-color-picker'), false, '1.0.0' ); |
---|
52 | wp_enqueue_script ('soi_custom_wp_admin_js'); |
---|
53 | |
---|
54 | |
---|
55 | } |
---|
56 | add_action( 'admin_enqueue_scripts', 'soi_load_custom_wp_admin_scripts' ); |
---|
57 | |
---|
58 | |
---|
59 | // add_filter(); |
---|
60 | add_filter('the_content', 'soi_replace_tags', 100); |
---|
61 | function soi_replace_tags ($content, $alt_text='',$title='') |
---|
62 | { |
---|
63 | |
---|
64 | |
---|
65 | global $post; |
---|
66 | |
---|
67 | $soi_options_array = get_option('soi_options_values'); |
---|
68 | |
---|
69 | $alt_text = $soi_options_array['soi_alt_value']; |
---|
70 | $title_text = $soi_options_array['soi_title_value']; |
---|
71 | |
---|
72 | // get the post title for later use |
---|
73 | $post_title = esc_attr($post->post_title); |
---|
74 | |
---|
75 | // preapre the alt text |
---|
76 | |
---|
77 | |
---|
78 | // Check if we need to overide the default alt and existing alt text |
---|
79 | // We will set the flag 1 or 0 |
---|
80 | |
---|
81 | //check setting for overinding alt tag |
---|
82 | $alt_flag = $soi_options_array['soi_override_alt_value']; |
---|
83 | |
---|
84 | //check setting for overinding title tag |
---|
85 | $title_flag = $soi_options_array['soi_override_title_value']; |
---|
86 | |
---|
87 | // Set the alt pattern |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | // print_r($post); |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | // This piece of code first finds all the images in the page |
---|
97 | // Then we proceed to finding missing or empty alt tags |
---|
98 | |
---|
99 | $soi_options_array = get_option('soi_options_values'); |
---|
100 | |
---|
101 | // count number of images found in content |
---|
102 | $count = preg_match_all('/<img[^>]+>/i', $content, $images); |
---|
103 | |
---|
104 | |
---|
105 | // If we find images on the page then proceed to check the alt tags |
---|
106 | |
---|
107 | // We also need to calaculate the velue to be inserted in the tags based on user input |
---|
108 | |
---|
109 | |
---|
110 | if($count>0) |
---|
111 | { |
---|
112 | |
---|
113 | // Here we will set the alt value to be inserted. |
---|
114 | // $t = "$post_title" |
---|
115 | // we want to output like alt = "text" |
---|
116 | |
---|
117 | $t = 'alt="'.$alt_text.'"'; |
---|
118 | |
---|
119 | // we want to output like title = "text" |
---|
120 | $t_title = 'title="'.$title_text.'"'; |
---|
121 | |
---|
122 | |
---|
123 | foreach($images[0] as $img) |
---|
124 | { // check if the alt tag exists in the image |
---|
125 | |
---|
126 | |
---|
127 | // Get the Name of Image Files. |
---|
128 | |
---|
129 | $output = preg_match_all( '/<img[^>]+src=[\'"]([^\'"]+)[\'"].*>/i', $img, $matches); |
---|
130 | |
---|
131 | $get_file_name = pathinfo($matches[1][0]); |
---|
132 | $image_file_name = $get_file_name['filename']; |
---|
133 | |
---|
134 | |
---|
135 | // Get post categories |
---|
136 | $postcategories = get_the_category(); |
---|
137 | $post_category=''; |
---|
138 | if ($postcategories) { |
---|
139 | foreach($postcategories as $category) { |
---|
140 | $post_category .= $category->name .' '; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /// fetch the values of alt and title tags from the option panel |
---|
145 | $alt_text = $soi_options_array['soi_alt_value']; |
---|
146 | $title_text = $soi_options_array['soi_title_value']; |
---|
147 | |
---|
148 | // Replace the Values for alt tag |
---|
149 | |
---|
150 | $alt_text = str_replace('%title',$post_title,$alt_text ); |
---|
151 | $alt_text = str_replace('%name',$image_file_name,$alt_text ); |
---|
152 | $alt_text = str_replace('%category',$post_category,$alt_text ); |
---|
153 | |
---|
154 | // replace the values for title tag. |
---|
155 | $title_text = str_replace('%title',$post_title,$title_text ); |
---|
156 | $title_text = str_replace('%name',$image_file_name,$title_text ); |
---|
157 | $title_text = str_replace('%category',$post_category,$title_text ); |
---|
158 | |
---|
159 | //configure tags with specified values from option panel. |
---|
160 | $t = ' alt="'.$alt_text.'" '; |
---|
161 | $t_title = ' title="'.$title_text.'" '; |
---|
162 | |
---|
163 | //take the alt tag out from the image html markup |
---|
164 | $is_alt = preg_match_all('/alt="([^"]*)"/i', $img, $alt); |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | ////////////////// check for alt tag ///////////////////////// |
---|
169 | // In case there is not alt tag, create the tag and insert the value |
---|
170 | if ($alt_flag == "1") |
---|
171 | |
---|
172 | { |
---|
173 | // if alt tag is not present than insert the tag. |
---|
174 | if($is_alt == 0) |
---|
175 | { $new_img = str_replace('<img ', '<img '.$t , $img); |
---|
176 | $content = str_replace($img, $new_img, $content); |
---|
177 | } |
---|
178 | |
---|
179 | // if alt tag is present |
---|
180 | elseif($is_alt==1) |
---|
181 | |
---|
182 | { |
---|
183 | |
---|
184 | $text = trim($alt[1][0]); |
---|
185 | |
---|
186 | |
---|
187 | // Check if the alt text is empty. |
---|
188 | |
---|
189 | if(empty($text)) |
---|
190 | { |
---|
191 | |
---|
192 | |
---|
193 | $new_img = str_replace($alt[0][0], $t, $img); |
---|
194 | |
---|
195 | $content = str_replace($img, $new_img, $content); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | // Should we override the existing alt tag |
---|
202 | if ($alt_flag == "1") |
---|
203 | |
---|
204 | { |
---|
205 | |
---|
206 | $new_img = str_replace($alt[0][0], $t, $img); |
---|
207 | |
---|
208 | $content = str_replace($img, $new_img, $content); |
---|
209 | |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | } |
---|
214 | }//////////////////// checked for alt tag //////////////////// |
---|
215 | |
---|
216 | ///////////////// check for title tag /////////////////////////// |
---|
217 | |
---|
218 | |
---|
219 | // first check weither title tag needs to be overide |
---|
220 | if($title_flag == "1"){ |
---|
221 | |
---|
222 | if(!isset($new_img)) $new_img=$img; // when alt tag is not overridden, than , use actual image markup ie $new_img. |
---|
223 | |
---|
224 | $is_title = preg_match_all('/title="([^"]*)"/i', $new_img, $title); |
---|
225 | |
---|
226 | // check if title tag is not present in the img tag |
---|
227 | if($is_title == 0) |
---|
228 | { |
---|
229 | // create the title tag and insert the tag |
---|
230 | $final_img = str_replace('<img ', '<img '.$t_title , $new_img); |
---|
231 | $content = str_replace($new_img, $final_img, $content); |
---|
232 | |
---|
233 | } else { |
---|
234 | |
---|
235 | // you are here bcs title tags exsis and needs to be override |
---|
236 | $final_img = str_replace($title[0][0], $t_title, $new_img); |
---|
237 | $content = str_replace($new_img, $final_img, $content); |
---|
238 | } |
---|
239 | } |
---|
240 | ///////////////////// title tag checked //////////////// |
---|
241 | |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | return $content; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|