This repository was archived by the owner on Mar 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadblock-notify-files.php
280 lines (268 loc) · 10.5 KB
/
adblock-notify-files.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* ************************************************************
*
* @package adblock-notify
* SECURITY : Exit if accessed directly
***************************************************************/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Direct acces not allowed!' );
}
/**
* ************************************************************
* Generate random selector or file name
***************************************************************/
function an_random_slug() {
$alphabet = 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ';
$prefix = array();
$alphaLength = strlen( $alphabet ) - 1;
for ( $i = 0; $i < 12; $i ++ ) {
$n = rand( 0, $alphaLength );
$prefix[] = $alphabet[ $n ];
}
return implode( $prefix );
}
/**
* ************************************************************
* Create new Style and Script files in a temp directory
***************************************************************/
function an_change_files_css_selectors( $flush, $tempFolderPath, $tempFolderURL, $file, $oldFileName, $newFileName, $oldSelectors, $newSelectors, $content = '' ) {
// Get default css and js files
$fileExt = pathinfo( $file, PATHINFO_EXTENSION );
$fileResponse = wp_remote_get( $file );
$fileContent = wp_remote_retrieve_body( $fileResponse ) . $content;
if ( is_wp_error( $fileResponse ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
WP_Filesystem();
global $wp_filesystem;
$fileContent = $wp_filesystem->get_contents( AN_PATH . str_replace( AN_URL, '', $file ) );
} else {
$fileContent = wp_remote_retrieve_body( $fileResponse ) . $content;
}
// Flush selectors
if ( true == $flush ) {
// Replace default selectors with new ones
$defaultSelectors = array( 'an-Modal', 'reveal-modal', 'an-alternative' );
$fileContent = str_replace( $defaultSelectors, $newSelectors, $fileContent );
} else {
// Replace default selectors with existings ones
$defaultSelectors = array( 'an-Modal', 'reveal-modal', 'an-alternative' );
$fileContent = str_replace( $defaultSelectors, $oldSelectors, $fileContent );
}
require_once( ABSPATH . 'wp-admin/includes/file.php' );
// Load WP_Filesystem API
WP_Filesystem();
global $wp_filesystem;
// Verify that we can create the file
if ( $wp_filesystem->exists( $tempFolderPath . $oldFileName ) ) {
if ( ! $wp_filesystem->is_writable( $tempFolderPath . $oldFileName ) ||
! $wp_filesystem->is_readable( $tempFolderPath . $oldFileName )
) {
return false;
}
}
// Verify directory
$uploadDir = wp_upload_dir();
if ( ! $wp_filesystem->is_dir( $uploadDir['basedir'] ) ||
! $wp_filesystem->is_writable( $uploadDir['basedir'] )
) {
return false;
}
// Create new dir and files
if ( $wp_filesystem->is_dir( $tempFolderPath ) ) {
$glob_files = glob( $tempFolderPath . '*.' . $fileExt );
array_map( 'unlink', is_array( $glob_files ) ? $glob_files : false );
} else {
$wp_filesystem->mkdir( $tempFolderPath );
}
// Create new file (flush) or update old one
if ( true == $flush ) {
$wp_filesystem->put_contents( $tempFolderPath . $newFileName . '.' . $fileExt, $fileContent, 0644 );
return $newFileName . '.' . $fileExt;
} else {
$wp_filesystem->put_contents( $tempFolderPath . $oldFileName, $fileContent, 0644 );
return $oldFileName;
}
}
/**
* ************************************************************
* Save scripts and styles with new random selectors after saving Titan Options
***************************************************************/
function an_save_setting_random_selectors( $force = false ) {
// Restart cookie on every options save.
if ( isset( $_COOKIE[ AN_COOKIE ] ) ) {
setcookie( AN_COOKIE, null, - 1, '/' );
}
$an_option = unserialize( an_get_option( 'adblocker_notify_options' ) );
$anScripts = unserialize( an_get_option( 'adblocker_notify_selectors' ) );
$selectors_option = isset( $an_option['an_option_selectors'] ) ? $an_option['an_option_selectors'] : true;
if ( true == $selectors_option || $force ) {
if ( ! isset( $an_option['an_option_flush'] ) ) {
$an_option['an_option_flush'] = false;
}
// Define new temp path
$uploadDir = wp_upload_dir();
$tempDirName = an_random_slug();
$tempFolderPath = trailingslashit( $uploadDir['basedir'] ) . $tempDirName . '/';
$tempFolderURL = trailingslashit( $uploadDir['baseurl'] ) . $tempDirName . '/';
// Retrieve old files infos
if ( ! isset( $anScripts['files']['css'] ) ) {
$anScripts['files']['css'] = '';
}
if ( ! isset( $anScripts['files']['js'] ) ) {
$anScripts['files']['js'] = '';
}
if ( ! isset( $anScripts['selectors'] ) ) {
$anScripts['selectors'] = '';
}
if ( ! isset( $anScripts['temp-path'] ) ) {
$anScripts['temp-path'] = false;
}
// Define new selectors
$newSelectors = array( an_random_slug(), an_random_slug(), an_random_slug() );
$flush = false;
if ( true == $an_option['an_option_flush'] || ! file_exists( $anScripts['temp-path'] ) || false == $anScripts['temp-path'] ) {
$flush = true;
}
// Keep old directory name and selectors if no flushed
if ( false == $flush ) {
$newSelectors = $anScripts['selectors'];
if ( isset( $anScripts['temp-path'] ) && false != $anScripts['temp-path'] ) {
$tempFolderPath = $anScripts['temp-path'];
$tempFolderURL = $anScripts['temp-url'];
}
} else {
// Or remove it before new files creation
if ( isset( $anScripts['temp-path'] ) && false != $anScripts['temp-path'] ) {
an_delete_temp_folder( $anScripts['temp-path'] );
}
}
// Generate new css and js files
$titanCssContent = an_update_titan_css_selectors( $an_option );
$newCSS = an_change_files_css_selectors(
$flush,
$tempFolderPath,
$tempFolderURL,
AN_URL . 'css/an-style.css',
$anScripts['files']['css'],
an_random_slug(),
$anScripts['selectors'],
$newSelectors,
$titanCssContent
);
$newJS = an_change_files_css_selectors(
$flush,
$tempFolderPath,
$tempFolderURL,
AN_URL . 'js/an-scripts.js',
$anScripts['files']['js'],
an_random_slug(),
$anScripts['selectors'],
$newSelectors
);
// Upload dir and temp dir are not writable
if ( false == $newCSS || false == $newJS ) {
$tempFolderPath = false;
}
// Store data
$newFiles = array(
'temp-path' => $tempFolderPath,
'temp-url' => $tempFolderURL,
'files' => array(
'css' => $newCSS,
'js' => $newJS,
),
'selectors' => $newSelectors,
);
an_update_option( 'adblocker_notify_selectors', serialize( $newFiles ) );
// remove option flush
if ( ! isset( $an_option['an_option_flush'] ) ) {
$an_option['an_option_flush'] = false;
}
$an_option['an_option_flush'] = false;
an_update_option( 'adblocker_notify_options', serialize( $an_option ) );
} else {
// Remove temp files
if ( isset( $anScripts['temp-path'] ) ) {
an_delete_temp_folder( $anScripts['temp-path'] );
}
}// End if().
}
add_action( 'tf_admin_options_saved_adblocker_notify', 'an_save_setting_random_selectors', 99 );
/**
* ************************************************************
* Admin Panel notice if wrong CHMOD on "wp-content/uploads"
***************************************************************/
function an_error_admin_notices() {
$prefix = an_is_pro() && is_multisite() ? '-network' : '';
$screen = get_current_screen();
if ( 'toplevel_page_' . AN_ID . $prefix != $screen->id ) {
return;
}
$anScripts = unserialize( an_get_option( 'adblocker_notify_selectors' ) );
if ( ! empty( $anScripts ) && false == $anScripts['temp-path'] ) {
echo '
<div class="error warning">
<p>
' . __( 'WARNING: There was an error creating Ad Blocker Notify CSS and JS files. Upload directory is not writable. Please CHMOD "wp-content/uploads" to 0664 and verify your server settings', 'an-translate' ) . '
[ <a href="http://codex.wordpress.org/Changing_File_Permissions" target="_blank" title="Changing File Permissions"> Changing File Permissions</a> ]
</p>
<p>
' . __( 'Don\'t worry, we thought about it. Ad Blocker Notify will print the scripts directly in your DOM, but for performance purpose it is recommended to change your uploads directory CHMOD.', 'an-translate' ) . '
</p>
</div>
';
}
}
add_action( 'admin_notices', 'an_error_admin_notices' );
/**
* ************************************************************
* Edit Titan Generated CSS
***************************************************************/
function an_update_titan_css_selectors( $an_option ) {
$tfStyle = '';
if ( isset( $an_option['an_alternative_custom_css'] ) ) {
$tfStyle .= $an_option['an_alternative_custom_css'];
}
if ( isset( $an_option['an_option_modal_custom_css'] ) ) {
$tfStyle .= $an_option['an_option_modal_custom_css'];
}
// Remove TitanFramework Generated Style
$uploadDir = wp_upload_dir();
$TfCssFile = trailingslashit( $uploadDir['basedir'] ) . 'titan-framework-adblocker_notify-css.css';
if ( file_exists( $TfCssFile ) ) {
unlink( $TfCssFile );
}
return $tfStyle;
}
/**
* ************************************************************
* Print Style & Sripts if temp dir. is not writable LOW PERF
***************************************************************/
function an_print_change_files_css_selectors( $an_option, $anScripts ) {
// Get AN style and script
$anCSS = AN_URL . 'css/an-style.css';
$anJS = AN_URL . 'js/an-scripts.js';
$newSelectors = $anScripts['selectors'];
$defaultSelectors = array( 'an-Modal', 'reveal-modal', 'an-alternative' );
$tfStyle = '';
$tfStyle .= $an_option->getOption( 'an_alternative_custom_css' );
$tfStyle .= $an_option->getOption( 'an_option_modal_custom_css' );
$anCSSFileContent = wp_remote_get( $anCSS );
$anCSSFileContent = wp_remote_retrieve_body( $anCSSFileContent );
$anCSSFileContent = str_replace( $defaultSelectors, $newSelectors, $anCSSFileContent . $tfStyle );
$anJSFileContent = wp_remote_get( $anJS );
$anJSFileContent = wp_remote_retrieve_body( $anJSFileContent );
$anJSFileContent = str_replace( $defaultSelectors, $newSelectors, $anJSFileContent );
return '
<style type="text/css">
' . $anCSSFileContent . '
</style>
<script>// <![CDATA[
var ajax_object = { ajaxurl : "' . admin_url( 'admin-ajax.php' ) . '" };
// ]]></script>
<script type="text/javascript">
' . $anJSFileContent . '
</script>
';
}