PluginProbe
Markup Markdown / trunk
Markup Markdown vtrunk
4.0.2 4.0.1 3.14.1 3.14.2 3.14.3 3.14.4 3.14.5 3.14.6 3.14.7 3.15.0 3.15.1 3.15.2 3.16.0 3.17.0 3.17.1 3.18.0 3.18.1 3.18.2 3.19.0 3.19.1 3.20.0 3.20.1 3.20.10 3.20.11 3.20.12 All 61 releases
markup-markdown / markup-markdown.php

markup-markdown.php in Markup Markdown trunk, at markup-markdown.php

453 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Markup Markdown
4 *
5 * @package MarkupMarkdown
6 * @author Pierre-Henri Lavigne
7 * @license GPLv3 or later
8 *
9 * @wordpress-plugin
10 * Plugin Name: Markup Markdown
11 * Plugin URI: https://www.markup-markdown.com
12 * Description: Replaces the Gutenberg Block Editor in favor of pure markdown based markups
13 * Version: 4.0.2
14 * Author: Pierre-Henri Lavigne
15 * Author URI: https://www.markup-markdown.com
16 * License: GPLv3 or later
17 * License URI: https://www.gnu.org/licenses/gpl-3.0.html#license-text
18 * Text Domain: markup-markdown
19 * Domain Path: /languages
20 * Requires at least: 6.6
21 * Tested up to: 7.1
22 *
23 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
24 * General Public License version 3, as published by the Free Software Foundation. You may NOT assume
25 * that you can use any other version of the GPL.
26 *
27 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
31 defined( 'ABSPATH' ) || exit;
32 define( 'MARKUP_MARKDOWN_FILE_URL', __FILE__ );
33
34
35 if ( ! class_exists( 'Markup_Markdown' ) ) :
36
37 /**
38 * The root core class, everything begins here.
39 *
40 * @class Markup_Markdown
41 */
42 class Markup_Markdown {
43
44
45 /**
46 * The property that will held the PHP markdown parser
47 *
48 * @var object
49 */
50 protected $parser;
51
52
53 /**
54 * The basic setup
55 *
56 * @var array<string, array>
57 */
58 protected $settings = array(
59 'version' => '4.0.2',
60 'plugin_uri' => '', // The http url used to access the plugin assets.
61 'plugin_dir' => '', // The full path to the plugin directory.
62 'plugin_slug' => '', // The slug used inside the WordPress plugin directory.
63 'cache_dir' => '', // The full path to the cache directory.
64 'conf_dir' => '', // The full path where configuration files are stored.
65 'curr_blog' => '1_1', // Default active blog configuration.
66 'default_conf' => array(), // Default setup variables.
67 );
68
69
70 /**
71 * Flag to check if the file system helpers were loaded
72 *
73 * @var integer
74 */
75 protected $filesystem_ready = 0;
76
77
78 /**
79 * Class to perform various actions with static files
80 *
81 * @var object
82 */
83 protected $filesystem;
84
85
86 /**
87 * Everything starts here
88 */
89 public function __construct() {
90 $curr_blog = get_current_network_id() . '_' . get_current_blog_id();
91 $this->settings['plugin_slug'] = plugin_basename( __DIR__ );
92 $this->settings['plugin_uri'] = plugin_dir_url( __FILE__ );
93 $this->settings['plugin_dir'] = plugin_dir_path( __FILE__ );
94 $this->settings['cache_dir'] = WP_CONTENT_DIR . '/mmd-cache';
95 $this->settings['conf_dir'] = WP_CONTENT_DIR . '/mmd-conf';
96 $this->settings['curr_blog'] = $curr_blog;
97 $this->settings['cache_blog_prefix'] = WP_CONTENT_DIR . '/mmd-cache/.posts/' . $curr_blog . '_';
98 $this->settings['conf_blog_prefix'] = WP_CONTENT_DIR . '/mmd-conf/' . $curr_blog . '_';
99 require_once $this->settings['plugin_dir'] . 'MarkupMarkdown/Core/class-activation.php';
100 }
101
102
103 /**
104 * Overloading method __get
105 *
106 * @since 2.0.0
107 * @access public
108 *
109 * @param string $name The name of the key in the $settings variable to retrieve.
110 * @return mixed The value of the related key in $settings or an empty string.
111 */
112 public function __get( $name ) {
113 return isset( $this->settings[ $name ] ) ? $this->settings[ $name ] : '';
114 }
115
116
117 /**
118 * Overloading method __set
119 *
120 * @since 2.0.0
121 * @access public
122 *
123 * @param string $name The name of the key in the $settings variable to overwrite.
124 * @param mixed $val The new value of the related key in the $settings variable.
125 * @return void
126 */
127 public function __set( $name, $val ) {
128 if ( isset( $this->settings[ $name ] ) && is_array( $this->settings[ $name ] ) && is_array( $val ) ) :
129 $this->settings[ $name ] = array_merge( $this->settings[ $name ], $val );
130 else :
131 $fixed = array( 'plugin_uri', 'plugin_dir', 'plugin_slug', 'cache_dir', 'conf_dir', 'curr_blog', 'default_conf', 'cache_blog_prefix', 'conf_blog_prefix' );
132 if ( false === in_array( $name, $fixed, true ) ) :
133 $this->settings[ $name ] = $val;
134 endif;
135 endif;
136 }
137
138
139 /**
140 * Global method that will be used to convert markdown string on the fly
141 *
142 * @since 1.0
143 * @access public
144 *
145 * @param string $content The markdown code.
146 *
147 * @return string The HTML content
148 */
149 final public function markdown2html( $content ) {
150 $filtered = apply_filters( 'markup_markdown_field_mmd2html', $content );
151 $html = htmlspecialchars_decode( $filtered, ENT_COMPAT );
152 return do_shortcode( $html );
153 }
154
155
156 /**
157 * Global method that can be access to clear OP Cache file
158 *
159 * @since 3.0
160 * @access public
161 *
162 * @param string $file String Target file.
163 *
164 * @return void
165 */
166 final public function clear_cache( $file = '' ) {
167 if ( function_exists( 'wp_opcache_invalidate' ) ) :
168 wp_opcache_invalidate( $file );
169 elseif ( function_exists( 'opcache_invalidate' ) ) :
170 opcache_invalidate( $file );
171 endif;
172 }
173
174
175 /**
176 * Tiny function to filter user permissions
177 *
178 * @since 3.3.0
179 * @access public
180 *
181 * @param integer $user_id The WordPress user ID to check.
182 *
183 * @return boolean TRUE if granted or FALSE
184 */
185 final public function user_allowed( $user_id = 0 ) {
186 if ( ! $user_id ) :
187 $user_id = get_current_user_id();
188 endif;
189 if ( ! $user_id ) :
190 // Disable *Guest* users.
191 return false;
192 endif;
193 $user = new \WP_User( $user_id );
194 if ( $user && ! $user->has_cap( 'edit_posts' ) ) :
195 // Disable *Subscribers* or users without edit permissions.
196 return false;
197 endif;
198 return true;
199 }
200
201
202 /**
203 * Tiny utility to decode json in a custom way
204 *
205 * @since 3.17.0
206 * @access public
207 *
208 * @param string $file Absolute path with file name.
209 * @param boolean $associative To return the data an associative array, object otherwise.
210 *
211 * @return array|object The JSON decoded data
212 */
213 final public function json_decode( $file, $associative ) {
214 if ( ! isset( $file ) || empty( $file ) ) :
215 return false;
216 endif;
217 $my_data = $this->filesystem->get_contents( $file );
218 if ( ! isset( $my_data ) || empty( $my_data ) ) :
219 return false;
220 endif;
221 if ( substr( $my_data, 0, 3 ) === "\xEF\xBB\xBF" ) :
222 $my_data = substr( $my_data, 3 );
223 endif;
224 return json_decode( $my_data, ! isset( $associative ) ? false : $associative );
225 }
226
227
228 /**
229 * Initialize an instance of the WordPress file system utility
230 * that can be used to perform various operations with files
231 *
232 * @since 3.19
233 * @access private
234 *
235 * @return boolean TRUE if $wp_filesystem can be used or false
236 */
237 private function check_filesystem() {
238 if ( ! $this->filesystem_ready ) :
239 if ( ! function_exists( 'wp_filesystem' ) ) :
240 require_once ABSPATH . 'wp-admin/includes/file.php';
241 endif;
242 $this->filesystem_ready = WP_Filesystem() ? 1 : -1;
243 if ( $this->filesystem_ready > 0 ) :
244 global $wp_filesystem;
245 $this->filesystem = clone $wp_filesystem;
246 else :
247 $this->file_system_ready = -1; // Silent failed.
248 endif;
249 endif;
250 return $this->filesystem_ready > 0 ? true : false;
251 }
252
253
254 /**
255 * Helper to check if a file already exists
256 *
257 * @since 3.19
258 * @access public
259 *
260 * @param string $item Target file or directory.
261 *
262 * @return boolean TRUE in case of success, FALSE otherwise
263 */
264 final public function exists( $item ) {
265 if ( ! $this->check_filesystem() || ! isset( $item ) || empty( $item ) ) :
266 return false;
267 endif;
268 return $this->filesystem->exists( $item );
269 }
270
271
272 /**
273 * Helper to retrieve the raw content of a file
274 *
275 * @since 3.19
276 * @access private
277 *
278 * @param string $file Name of the file to read.
279 *
280 * @return string|FALSE Content of the file as a string on success, FALSE otherwise
281 */
282 final public function get_contents( $file ) {
283 if ( ! $this->check_filesystem() || ! isset( $file ) || empty( $file ) ) :
284 return false;
285 endif;
286 return $this->filesystem->get_contents( $file );
287 }
288
289
290 /**
291 * Helper to create a folder on the server
292 *
293 * @since 3.19
294 * @access public
295 *
296 * @param string $dir Target directory path.
297 *
298 * @return boolean TRUE in case of success, FALSE otherwise
299 */
300 final public function mkdir( $dir ) {
301 if ( ! $this->check_filesystem() || ! isset( $dir ) || empty( $dir ) ) :
302 return false;
303 endif;
304 return $this->filesystem->mkdir( $dir, FS_CHMOD_DIR );
305 }
306
307
308 /**
309 * Helper to move a file / folder
310 *
311 * @since 3.19
312 * @access public
313 *
314 * @param string $src Source of the item.
315 * @param string $dest Target of the item.
316 *
317 * @return boolean TRUE in case of success, FALSE otherwise
318 */
319 final public function move( $src, $dest ) {
320 if ( ! $this->check_filesystem() ) :
321 return false;
322 endif;
323 if ( ! isset( $src ) || empty( $src ) || ! isset( $dest ) || empty( $desc ) ) :
324 return false;
325 endif;
326 return $this->filesystem->move( $src, $dest, true );
327 }
328
329
330 /**
331 * Helper to write data into a file
332 *
333 * @since 3.19
334 * @access public
335 *
336 * @param string $file Target file.
337 * @param string $contents Data to write to the target file.
338 *
339 * @return boolean TRUE in case of success, FALSE otherwise
340 */
341 final public function put_contents( $file, $contents ) {
342 if ( ! $this->check_filesystem() ) :
343 return false;
344 endif;
345 if ( ! isset( $file ) || empty( $file ) || ! isset( $contents ) || empty( $contents ) ) :
346 return false;
347 endif;
348 return $this->filesystem->put_contents( $file, $contents );
349 }
350
351
352 /**
353 * Helper to create a file
354 *
355 * @since 3.19
356 * @access public
357 *
358 * @param string $file Target file.
359 *
360 * @return boolean TRUE in case of success, FALSE otherwise
361 */
362 final public function touch( $file ) {
363 if ( ! $this->check_filesystem() ) :
364 return false;
365 endif;
366 if ( ! isset( $file ) || empty( $file ) ) :
367 return false;
368 endif;
369 return $this->filesystem->touch( $file );
370 }
371
372
373 /**
374 * Helper to delete a file
375 *
376 * @since 3.26
377 * @access public
378 *
379 * @param string $file Target file.
380 *
381 * @return boolean TRUE in case of success, FALSE otherwise
382 */
383 final public function unlink( $file ) {
384 if ( ! $this->check_filesystem() ) :
385 return false;
386 endif;
387 if ( ! isset( $file ) || empty( $file ) ) :
388 return false;
389 endif;
390 return $this->filesystem->delete( $file, false, false );
391 }
392 }
393
394
395 /**
396 * Allow developers to access public properties and methods of the instance.
397 */
398 final class Markup_Markdown_Instance {
399
400 /**
401 * The handler of the primary instance
402 *
403 * @var Object
404 */
405 private static $instance;
406
407 /**
408 * Initialize if need be a new instance of Markup Markdown
409 *
410 * @return Object The static instance
411 */
412 public static function instance() {
413 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Markup_Markdown_Instance ) ) :
414 self::$instance = new Markup_Markdown();
415 new \MarkupMarkdown\Core\Activation();
416 endif;
417 return self::$instance;
418 }
419 }
420
421
422 if ( ! function_exists( 'markup_markdown' ) ) :
423 /**
424 * Global available helper
425 *
426 * @function markup_markdown
427 */
428 function markup_markdown() {
429 return Markup_Markdown_Instance::instance();
430 }
431 // Run !
432 markup_markdown();
433
434 if ( ! function_exists( 'mmd' ) ) :
435 /**
436 * Previous global available helper
437 *
438 * @function mmd
439 */
440 function mmd() {
441 return markup_markdown();
442 }
443
444 endif;
445
446 endif;
447
448 else :
449
450 die( "Don't call Markup Markdown Twice ! ! !" );
451
452 endif;
453