loco-ajax.php
12 years ago
loco-download.php
12 years ago
loco-fail.php
12 years ago
loco-fatal.php
12 years ago
loco-posave.php
12 years ago
loco-posync.php
12 years ago
loco-posync.php
62 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin ajax include that syncs PO or POT file with sources |
| 4 | */ |
| 5 | |
| 6 | DOING_AJAX or die(); |
| 7 | |
| 8 | if( empty($path) || empty($root) ){ |
| 9 | throw new Exception( Loco::__('Invalid data posted to server'), 422 ); |
| 10 | } |
| 11 | |
| 12 | // path is allowed not exist |
| 13 | if( '/' !== $path{0} ){ |
| 14 | $path = WP_CONTENT_DIR.'/'.$path; |
| 15 | } |
| 16 | |
| 17 | // but root must |
| 18 | $root = LocoAdmin::resolve_path( $root, true ); |
| 19 | |
| 20 | while( true ){ |
| 21 | |
| 22 | // If file we're syncing is POT, we can only sync from sources |
| 23 | if( ! LocoAdmin::is_pot($path) ){ |
| 24 | |
| 25 | // if a POT file exists, sync from that. |
| 26 | foreach( LocoAdmin::find_pot($root) as $pot_path ){ |
| 27 | $exp = LocoAdmin::parse_po( $pot_path ); |
| 28 | if( ! $exp || ( 1 === count($exp) && '' === $exp[0]['source'] ) ){ |
| 29 | //throw new Exception( Loco::__('POT file is empty') ); |
| 30 | continue; |
| 31 | } |
| 32 | $pot = basename($pot_path); |
| 33 | break 2; |
| 34 | } |
| 35 | |
| 36 | } |
| 37 | |
| 38 | // Extract from sources by default |
| 39 | if( $exp = LocoAdmin::xgettext($root) ){ |
| 40 | $pot = ''; |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | throw new Exception( Loco::__('No strings could be extracted from source files') ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | // sync selected headers |
| 49 | $headers = array(); |
| 50 | if( '' === $exp[0]['source'] ){ |
| 51 | $keep = array('Project-Id-Version'=>'','Language-Team'=>'','POT-Creation-Date'=>'','POT-Revision-Date'=>''); |
| 52 | $head = loco_parse_po_headers( $exp[0]['target'] ); |
| 53 | $headers = array_intersect_key( $head->to_array(), $keep ); |
| 54 | $exp[0] = array(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // sync ok. |
| 59 | return compact( 'pot', 'exp', 'headers' ); |
| 60 | |
| 61 | |
| 62 |