Changeset 1409632
- Timestamp:
- 05/03/2016 01:26:58 PM (10 years ago)
- Location:
- bablic
- Files:
-
- 8 added
- 1 deleted
- 1 edited
-
tags/2.0 (added)
-
tags/2.0/Bablic.php (added)
-
tags/2.0/admin.js (added)
-
tags/2.0/lang (added)
-
tags/2.0/lang/Bablic-en_US.po (added)
-
tags/2.0/readme.txt (added)
-
tags/2.0/sdk.php (added)
-
trunk/Bablic.php (modified) (11 diffs)
-
trunk/bablic_db.php (deleted)
-
trunk/sdk.php (added)
Legend:
- Unmodified
- Added
- Removed
-
bablic/trunk/Bablic.php
r1350844 r1409632 1 1 <?php 2 require_once("bablic_db.php"); 3 4 $meta_tag_names = array('title','description','og:description','og:title','twitter:title','twitter:description','og:site_name','og:image'); 5 $split_str_regex = '/\s+\|\s+/'; 6 $META_REPLACE = '/<meta\s+([^>]*?)(name|property)="([^"]+)"\s+([^>]*?)content="([^"]+)"/i'; 7 $TITLE_REPLACE = '/<title>(.*?)<\/title>/i'; 8 $BABLIC_REP = '/<BABLIC_REP_(\d+)/'; 9 $BABLIC_TITLE = '/<BABLIC_TITLE_(\d+) \/>/'; 10 $CANONIAL_REPLACE = '/<(?:link|meta)\s+([^>]*?)(?:rel|property)="(?:canonical|og:url)"\s+([^>]*?)(?:href|content)="([^"]+)"([^>]*?)\/?>/i'; 2 require_once("sdk.php"); 11 3 12 4 /* … … 14 6 Plugin URI: https://www.bablic.com/docs#wordpress' 15 7 Description: Integrates your site with Bablic localization cloud service. 16 Version: 1.78 Version: 2.0 17 9 Author: Ishai Jaffe 18 10 Author URI: https://www.bablic.com … … 29 21 var $plugin_name = 'Bablic'; 30 22 var $plugin_textdomain = 'Bablic'; 31 var $bablic_version = ' 2.2';23 var $bablic_version = '3.1'; 32 24 var $query_var = 'bablic_locale'; 33 25 … … 35 27 36 28 var $log = array(); 37 var $db;38 var $processing = false;39 29 var $locale; 40 30 var $saved; … … 45 35 function bablic() { 46 36 $options = $this->optionsGetOptions(); 47 $this->db = new bablic_db();48 37 add_filter( 'plugin_row_meta', array( &$this, 'optionsSetPluginMeta' ), 10, 2 ); // add plugin page meta links 49 38 add_action( 'admin_init', array( &$this, 'optionsInit' ) ); // whitelist options page … … 53 42 54 43 add_action( 'parse_request', array( &$this, 'before_header' ),0); 55 //add_action( 'wp_head', array( &$this, 'after_header' ),99999999); 56 //add_action( 'wp_footer', array( &$this, 'after_footer' ),99999999); 57 58 //add_action('parse_request', array(&$this, 'on_parse_request'), 0); // should have high enough priority 59 //add_action('plugins_loaded', array(&$this, 'plugin_loaded')); 44 60 45 add_action('shutdown', array(&$this, 'after_header'),9999999999); 61 46 … … 88 73 add_action( 'admin_notices', array(&$this, 'bablic_admin_messages') ); 89 74 add_action('wp_ajax_bablicHideRating',array(&$this, 'bablic_hide_rating')); 90 add_action('wp_ajax_bablicMetaTags',array(&$this, 'bablic_ajax_meta'));91 92 75 } 93 76 94 77 function after_footer(){ 95 //print_r('<!--\n'); 96 //print_r($this->log); 97 //print_r('\n-->'); 98 } 99 100 function before_header(){ 78 } 79 80 81 function before_header(){ 101 82 if(is_admin()) 102 83 return; 103 $options = $this->optionsGetOptions(); 84 $options = $this->optionsGetOptions(); 104 85 if($options['site_id'] == '') 105 86 return; 106 if($options['meta'] != '1') 107 return; 108 109 $locale = $this->get_locale(); 110 if(!$locale) 111 return; 112 if($locale == $options['orig']) 113 return; 114 115 $this->locale = $locale; 116 $this->processing = true; 117 ob_start(array(&$this, "process_buffer")); 118 } 119 87 $this->sdk = new BablicSDK(array('site_id'=> $options['site_id'])); 88 } 89 120 90 function after_header(){ 121 // if($this->processing)122 // ob_end_flush();123 $this->processing = false;124 91 $this->after_footer(); 125 92 } 126 93 127 function hashcontent($c,$isImage){ 128 if($isImage) 129 $hex = sha1(preg_replace('/^https?:\/\//','',$c)); 130 else 131 $hex = sha1(strtolower(html_entity_decode(htmlspecialchars_decode($c,ENT_QUOTES),ENT_QUOTES,"UTF-8"))); 132 133 $options = $this->optionsGetOptions(); 134 $version = (float)($options['version']); 135 if($version < 3) 136 return $hex; 137 return base64_encode(pack('H*', $hex)); 138 } 139 function qualify_meta_element($match){ 140 global $meta_tag_names; 141 $type = $match[3]; 142 if(!in_array($type,$meta_tag_names)) 143 return $match[0]; 144 145 $contents = $this->splitContent($match[5]); 146 if(count($contents) < 1) 147 return $match[0]; 148 array_push($this->saved,$match); 149 foreach($contents as $content){ 150 $c = trim(preg_replace('/\s+/', ' ',$content)); 151 $hash = $this->hashcontent($c,$type == 'og:image'); 152 $this->keys[$hash] = $c; 153 } 154 155 return '<BABLIC_REP_' . count($this->saved); 156 } 157 158 function qualify_title_element($match){ 159 $this->log($match[1]); 160 $contents = $this->splitContent($match[1]); 161 array_push($this->saved,$match); 162 foreach($contents as $content){ 163 $c = trim(preg_replace('/\s+/', ' ',$content)); 164 $hash = $this->hashcontent($c,0); 165 $this->keys[$hash] = $c; 166 } 167 168 return '<BABLIC_TITLE_' . count($this->saved) . ' />'; 169 } 170 function replace_meta_element($match){ 171 $num = intval($match[1]); 172 $match = $this->saved[$num-1]; 173 174 $full = $match[5]; 175 $contents = $this->splitContent($full); 176 $did_translate = false; 177 $h = array(); 178 foreach($contents as $content){ 179 $c = trim(preg_replace('/\s+/', ' ',$content)); 180 $hash = $this->hashcontent($c,$match[3] == 'og:image'); 181 array_push($h,$hash); 182 $translated = $this->tcs[$hash]; 183 if(!$translated) 184 continue; 185 if($translated == '__empty__') 186 continue; 187 $did_translate = true; 188 $full = str_replace($content,$translated,$full); 189 } 190 191 return '<meta bablic-id="'.implode(",",$h).'" bablic-translated' . ($did_translate ? '="'. $match[5] .'"' : '').' ' . $match[1] . $match[2] . '="'.$match[3].'" '.$match[4] .'content="'.$this->escapeHTML($full).'"'; 192 } 193 function escapeHTML($str){ 194 return str_replace('&','&',htmlspecialchars($str)); 195 } 196 197 function splitContent($content){ 198 global $split_str_regex; 199 return preg_split($split_str_regex,$content, NULL, PREG_SPLIT_NO_EMPTY); 200 } 201 function replace_title_element($match){ 202 $this->log($match); 203 $num = intval($match[1]); 204 $match = $this->saved[$num-1]; 205 206 $full = $match[1]; 207 $contents = $this->splitContent($full); 208 $did_translate = false; 209 $h = array(); 210 foreach($contents as $content){ 211 $c = trim(preg_replace('/\s+/', ' ',$content)); 212 $hash = $this->hashcontent($c,0); 213 array_push($h,$hash); 214 $translated = $this->tcs[$hash]; 215 if(!$translated) 216 continue; 217 if($translated == '__empty__') 218 continue; 219 $did_translate = true; 220 $full = str_replace($content,$translated,$full); 221 } 222 223 return '<title bablic-id="'.implode(",",$h).'" bablic-translated' . ($did_translate ? '="'. $match[1] .'"' : '').'>'.$this->escapeHTML($full).'</title>'; 224 } 225 226 function replace_canonical($match){ 227 $this->log($match); 228 $href = $match[3]; 229 if($this->get_locale_from_url($href) != $this->locale) 230 return ''; 231 return $match[0]; 232 } 233 234 function process_buffer($buffer){ 235 //$this->log($buffer); 236 global $META_REPLACE, $BABLIC_REP, $CANONIAL_REPLACE,$TITLE_REPLACE,$BABLIC_TITLE; 237 $this->saved = array(); 238 $this->keys = array(); 239 $orig = $buffer; 240 $buffer = preg_replace_callback($META_REPLACE,array(&$this,'qualify_meta_element'),$buffer); 241 $buffer = preg_replace_callback($TITLE_REPLACE,array(&$this,'qualify_title_element'),$buffer); 242 if(count($this->keys) < 1) 243 return $orig; 244 245 $this->log($this->keys); 246 $this->log($this->locale); 247 $tcs = $this->db->fetch_translations(array_keys($this->keys),$this->locale); 248 //$tcs = $this->db->fetch_all_translations(); 249 $this->log('translations'); 250 $this->log($tcs); 251 $this->tcs = $tcs;//array(); 252 // foreach($tcs as $tc){ 253 // $this->tcs[$tc['hash']] = $tc['translation']; 254 // } 255 256 $buffer = preg_replace_callback($BABLIC_REP,array(&$this,'replace_meta_element'),$buffer); 257 $this->log($BABLIC_TITLE); 258 $buffer = preg_replace_callback($BABLIC_TITLE,array(&$this,'replace_title_element'),$buffer); 259 $this->saved = null; 260 $this->keys = null; 261 $this->tcs = null; 262 263 $buffer = preg_replace_callback($CANONIAL_REPLACE,array(&$this,'replace_canonical'),$buffer); 264 265 266 // $html = str_get_html('<html></html>'); 267 // return $html->outertext; 268 return $buffer; 269 } 270 94 271 95 function get_locale_from_url($url){ 272 96 $options = $this->optionsGetOptions(); … … 396 220 'dont_permalink' => 'yes', 397 221 'date' => '', 398 'rated' => 'no', 399 'meta' => '0' 222 'rated' => 'no' 400 223 ); 401 224 return $defaults; … … 476 299 477 300 478 function load_meta_translation(){ 479 $this->log($this->db->db_install()); 480 $options = $this->optionsGetOptions(); 481 if(!$options['site_id']) 482 return; 483 $translations = json_decode(file_get_contents("http://api.bablic.com/api/meta?site=" . $options['site_id']), true); 484 if(!$translations) 485 return; 486 if($translations['not_modified']) 487 return; 488 // $this->db->db_install(); 489 //$this->log($result); 490 $locales = array(); 491 //$this->log($translations); 492 foreach($translations as $locale => $meta){ 493 array_push($locales,$locale); 494 foreach($meta as $hash => $translated){ 495 if($translated == '__empty__') 496 continue; 497 $this->log($hash); 498 $this->log($translated); 499 $res = $this->db->insert_translations($hash,$locale,$translated); 500 //$this->log($res); 501 } 502 } 503 $options['locales'] = $locales; 504 $this->updateOptions($options); 505 } 506 507 508 301 509 302 // sanitize and validate options input 510 303 function optionsValidate( $input ) { … … 581 374 <label><input type="checkbox" id="bablic_dont_permalink" <?php checked( 'no', $options['dont_permalink'], true ) ?> > Generate SEO-friendly localization urls (for example: /es/, /fr/about, ...)</label> 582 375 <br> 583 <br>584 <input type="hidden" id="bablic_use_meta_hidden" name="<?php echo $this->options_name; ?>[meta]" value="<?php echo $options['meta']; ?>" />585 <label><input type="checkbox" id="bablic_use_meta" <?php checked( '1', $options['meta'], true ) ?> > Translate meta tags on server side</label> <button id="bablic_refresh_meta" style="display:none;" class="button button-primary">Update DB</button>586 376 </div> 587 377 <?php } ?> … … 696 486 } 697 487 698 function bablic_ajax_meta(){699 $options = $this->optionsGetOptions();700 $metaOn = $_POST['meta'] == 'on';701 $options['meta'] = $metaOn ? '1' : '0';702 $this->updateOptions($options);703 704 if($metaOn)705 $this->load_meta_translation();706 707 //print_r($this->log);708 echo json_encode(array("success")); exit;709 }710 711 488 } // end class 712 489
Note: See TracChangeset
for help on using the changeset viewer.