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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php /** * pre-xgettext function. Initializes a new PO file for a given locale */ class Loco_admin_init_InitPotController extends Loco_admin_bundle_BaseController { /** * {@inheritdoc} */ public function init(){ parent::init(); $this->enqueueStyle('poinit'); // $bundle = $this->getBundle(); $this->set('title', __('New template','loco-translate').' ‹ '.$bundle ); }
/** * {@inheritdoc} */ public function getHelpTabs(){ return [ __('Overview') => $this->viewSnippet('tab-init-pot'), ]; }
/** * {@inheritdoc} */ public function render(){ $breadcrumb = $this->prepareNavigation(); // "new" tab is confusing when no project-scope navigation // $this->get('tabs')->add( __('New POT','loco-translate'), '', true );
$bundle = $this->getBundle(); $project = $this->getProject();
$domain = (string) $project->getDomain(); $this->set('domain', $domain ); // Tokenizer required for string extraction if( ! loco_check_extension('tokenizer') ){ return $this->view('admin/errors/no-tokenizer'); } // Establish default POT path whether it exists or not $pot = $project->getPot(); // POT should actually not exist at this stage. It should be edited instead. if( $pot->exists() ){ throw new Loco_error_Exception( __('Template file already exists','loco-translate') ); } // Bundle may deliberately lock template to avoid end-user tampering // it makes little sense to do so when template doesn't exist, but we will honour the setting anyway. if( $project->isPotLocked() ){ throw new Loco_error_Exception('Template is protected from updates by the bundle configuration'); } // Just warn if POT writing will fail when saved, but still show screen $dir = $pot->getParent(); // Avoiding full source scan until actioned, but calculate size to manage expectations $bytes = 0; $nfiles = 0; $nskip = 0; $largest = 0; $sources = $project->findSourceFiles(); // skip files larger than configured maximum $opts = Loco_data_Settings::get(); $max = wp_convert_hr_to_bytes( $opts->max_php_size ); /* @var $sourceFile Loco_fs_File */ foreach( $sources as $sourceFile ){ $nfiles++; $fsize = $sourceFile->size(); $largest = max( $largest, $fsize ); if( $fsize > $max ){ $nskip += 1; // uncomment to log which files are too large to be scanned // Loco_error_AdminNotices::debug( sprintf('%s is %s',$sourceFile,Loco_mvc_FileParams::renderBytes($fsize)) ); } else { $bytes += $fsize; } } $this->set( 'scan', new Loco_mvc_ViewParams( [ 'bytes' => $bytes, 'count' => $nfiles, 'skip' => $nskip, 'size' => Loco_mvc_FileParams::renderBytes($bytes), 'large' => Loco_mvc_FileParams::renderBytes($max), 'largest' => Loco_mvc_FileParams::renderBytes($largest), ] ) ); // file metadata $this->set('pot', Loco_mvc_FileParams::create( $pot ) ); $this->set('dir', Loco_mvc_FileParams::create( $dir ) ); $title = __('New template file','loco-translate'); // translators: %s refers to the name of a translation set (theme, plugin or core component) $subhead = sprintf( __('New translations template for "%s"','loco-translate'), $project ); $this->set('subhead', $subhead ); // navigate up to bundle listing page $breadcrumb->add( $title ); $this->set( 'breadcrumb', $breadcrumb ); // ajax service takes the target directory path $content_dir = loco_constant('WP_CONTENT_DIR'); $target_path = $pot->getParent()->getRelativePath($content_dir);
// hidden fields to pass through to Ajax endpoint $this->set( 'hidden', new Loco_mvc_ViewParams( [ 'action' => 'loco_json', 'route' => 'xgettext', 'loco-nonce' => $this->setNonce('xgettext')->value, 'type' => $bundle->getType(), 'bundle' => $bundle->getHandle(), 'domain' => $project->getId(), 'path' => $target_path, 'name' => $pot->basename(), ] ) );
// File system connect required if location not writable $relpath = $pot->getRelativePath($content_dir); $this->prepareFsConnect('create', $relpath ); $this->enqueueScript('potinit'); return $this->view( 'admin/init/init-pot' ); }
}
|