/var/www/html_it/wp-content/plugins/loco-translate/src/admin/file/ViewController.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * File view / source formatted view.
 */
class Loco_admin_file_ViewController extends Loco_admin_file_BaseController {


    
/**
     * {@inheritdoc}
     */
    
public function init(){
        
parent::init();
        
$this->enqueueStyle('poview');
        
//
        
$file $this->get('file');
        
$bundle $this->getBundle();
        
$this->set'title''Source of '.$file->basename().' &lsaquo; '.$bundle->getName() );
    }


    
/**
     * {@inheritdoc}
     */
    
public function getHelpTabs(){
        return  [
            
__('Overview') => $this->viewSnippet('tab-file-view'),
        ];
    }
    
    
    private function 
getUtf8SourceLoco_fs_File $fileLocoPoHeaders $head ){
        
$src $file->getContents();
        try {
            
$src loco_remove_bom$src$cs );
            if( 
'' === $cs ){
                
$cs $head->getCharset();
            }
            if( 
'' !== $cs ){
                
$src loco_convert_utf8($src,$cs,false);
            }
        }
        catch ( 
Exception $e ){
            
Loco_error_AdminNotices::debug$e->getMessage() );
        }
        return 
$src;
    }
    
    
    
    
/**
     * {@inheritdoc}
     */
    
public function render(){
        
        
// file must exist for editing
        /* @var Loco_fs_File $file */
        
$file $this->get('file');
        
$this->setFileTitle($file);
        
$type strtolower$file->extension() );

        if( 
$fail $this->getFileError($file) ){
            return 
$fail
        }

        
// Establish if file belongs to a configured project
        
$bundle null;
        
$project null;
        try {
            
$bundle $this->getBundle();
            
$project $this->getProject();
        }
        catch( 
Exception $e ){
            
Loco_error_AdminNotices::debug$e->getMessage() );
        }    
            
        
// Parse data before rendering, so we know it's a valid Gettext format
        
try {
            
$this->set('modified'$file->modified() );
            
$data Loco_gettext_Data::load$file );
        }
        catch( 
Loco_error_ParseException $e ){
            
Loco_error_AdminNotices::addLoco_error_Exception::convert($e) );
            
$data Loco_gettext_Data::dummy();
        }

        
$this->set'meta'Loco_gettext_Metadata::create($file$data) );

        
// binary MO will be hex-formatted in template
        
if( 'mo' === $type ){
            
$this->set('bin'$file->getContents() );
            return 
$this->view('admin/file/view-mo' );
        }
        
        
// l10n.php files are unlikely to be encountered without a po or mo, but still..
        
if( 'php'=== $type ){
            return 
$this->view('admin/file/view-php', ['phps'=>$file->getContents()] );
        }
        
        
// else is a PO or POT file 
        
$this->enqueueScript('poview');//->enqueueScript('min/highlight');
        
$lines preg_split('/\\n|\\r\\n?/'$this->getUtf8Source$file$data->getHeaders() ) );
        
$this->set'lines'$lines );
        
        
// ajax parameters required for pulling reference sources
        
$this->set('js', new Loco_mvc_ViewParams(  [
            
'popath' => $this->get('path'),
            
'nonces' => [
                
'fsReference' => wp_create_nonce('fsReference'),
            ],
            
'project' => $bundle ?  [
                
'bundle' => $bundle->getId(),
                
'domain' => $project $project->getId() : '',
            ] : 
null,
        ] ) );
        
        
// treat as PO if file name has locale
        
if( $this->getLocale() ){
            return 
$this->view('admin/file/view-po' );
        }

        
// else view as POT
        
return $this->view('admin/file/view-pot' );
    }

}