@@ -18,145 +18,145 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Geomap_Shortcode extends Wordlift_Shortcode { |
| 20 | 20 | |
| 21 | - const SHORTCODE = 'wl_geomap'; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Create a {@link Wordlift_Geomap_Shortcode} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.5.4 |
|
| 27 | - */ |
|
| 28 | - public function __construct() { |
|
| 29 | - parent::__construct(); |
|
| 30 | - |
|
| 31 | - // Hook to the `amp_post_template_css` to hide ourselves when in AMP |
|
| 32 | - // rendering. |
|
| 33 | - add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css', ) ); |
|
| 34 | - $this->register_block_type(); |
|
| 35 | - |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Render the shortcode. |
|
| 40 | - * |
|
| 41 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 42 | - * |
|
| 43 | - * @return string The output html code. |
|
| 44 | - * @since 3.5.4 |
|
| 45 | - * |
|
| 46 | - */ |
|
| 47 | - public function render( $atts ) { |
|
| 48 | - |
|
| 49 | - // Extract attributes and set default values. |
|
| 50 | - $geomap_atts = shortcode_atts( array( |
|
| 51 | - 'width' => '100%', |
|
| 52 | - 'height' => '300px', |
|
| 53 | - 'global' => false, |
|
| 54 | - ), $atts ); |
|
| 55 | - |
|
| 56 | - // Get id of the post |
|
| 57 | - $post_id = get_the_ID(); |
|
| 58 | - |
|
| 59 | - if ( $geomap_atts['global'] || is_null( $post_id ) ) { |
|
| 60 | - // Global geomap |
|
| 61 | - $geomap_id = 'wl_geomap_global'; |
|
| 62 | - $post_id = null; |
|
| 63 | - } else { |
|
| 64 | - // Post-specific geomap |
|
| 65 | - $geomap_id = 'wl_geomap_' . $post_id; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - wl_enqueue_leaflet( true ); |
|
| 70 | - |
|
| 71 | - // Use the registered style which define an optional dependency to font-awesome. |
|
| 72 | - // |
|
| 73 | - // @see https://github.com/insideout10/wordlift-plugin/issues/699 |
|
| 74 | - // wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
|
| 75 | - wp_enqueue_style( 'wordlift-ui' ); |
|
| 76 | - |
|
| 77 | - $this->enqueue_scripts(); |
|
| 78 | - |
|
| 79 | - wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array( |
|
| 80 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), // Global param |
|
| 81 | - 'action' => 'wl_geomap' // Global param |
|
| 82 | - ) ); |
|
| 83 | - |
|
| 84 | - // Escaping atts. |
|
| 85 | - $esc_id = esc_attr( $geomap_id ); |
|
| 86 | - $esc_width = esc_attr( $geomap_atts['width'] ); |
|
| 87 | - $esc_height = esc_attr( $geomap_atts['height'] ); |
|
| 88 | - $esc_post_id = esc_attr( $post_id ); |
|
| 89 | - |
|
| 90 | - // Return HTML template. |
|
| 91 | - return <<<EOF |
|
| 21 | + const SHORTCODE = 'wl_geomap'; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Create a {@link Wordlift_Geomap_Shortcode} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.5.4 |
|
| 27 | + */ |
|
| 28 | + public function __construct() { |
|
| 29 | + parent::__construct(); |
|
| 30 | + |
|
| 31 | + // Hook to the `amp_post_template_css` to hide ourselves when in AMP |
|
| 32 | + // rendering. |
|
| 33 | + add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css', ) ); |
|
| 34 | + $this->register_block_type(); |
|
| 35 | + |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Render the shortcode. |
|
| 40 | + * |
|
| 41 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 42 | + * |
|
| 43 | + * @return string The output html code. |
|
| 44 | + * @since 3.5.4 |
|
| 45 | + * |
|
| 46 | + */ |
|
| 47 | + public function render( $atts ) { |
|
| 48 | + |
|
| 49 | + // Extract attributes and set default values. |
|
| 50 | + $geomap_atts = shortcode_atts( array( |
|
| 51 | + 'width' => '100%', |
|
| 52 | + 'height' => '300px', |
|
| 53 | + 'global' => false, |
|
| 54 | + ), $atts ); |
|
| 55 | + |
|
| 56 | + // Get id of the post |
|
| 57 | + $post_id = get_the_ID(); |
|
| 58 | + |
|
| 59 | + if ( $geomap_atts['global'] || is_null( $post_id ) ) { |
|
| 60 | + // Global geomap |
|
| 61 | + $geomap_id = 'wl_geomap_global'; |
|
| 62 | + $post_id = null; |
|
| 63 | + } else { |
|
| 64 | + // Post-specific geomap |
|
| 65 | + $geomap_id = 'wl_geomap_' . $post_id; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + wl_enqueue_leaflet( true ); |
|
| 70 | + |
|
| 71 | + // Use the registered style which define an optional dependency to font-awesome. |
|
| 72 | + // |
|
| 73 | + // @see https://github.com/insideout10/wordlift-plugin/issues/699 |
|
| 74 | + // wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
|
| 75 | + wp_enqueue_style( 'wordlift-ui' ); |
|
| 76 | + |
|
| 77 | + $this->enqueue_scripts(); |
|
| 78 | + |
|
| 79 | + wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array( |
|
| 80 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), // Global param |
|
| 81 | + 'action' => 'wl_geomap' // Global param |
|
| 82 | + ) ); |
|
| 83 | + |
|
| 84 | + // Escaping atts. |
|
| 85 | + $esc_id = esc_attr( $geomap_id ); |
|
| 86 | + $esc_width = esc_attr( $geomap_atts['width'] ); |
|
| 87 | + $esc_height = esc_attr( $geomap_atts['height'] ); |
|
| 88 | + $esc_post_id = esc_attr( $post_id ); |
|
| 89 | + |
|
| 90 | + // Return HTML template. |
|
| 91 | + return <<<EOF |
|
| 92 | 92 | <div class="wl-geomap" id="$esc_id" data-post-id="$esc_post_id" |
| 93 | 93 | style="width:$esc_width; height:$esc_height; background-color: gray;"> |
| 94 | 94 | </div> |
| 95 | 95 | EOF; |
| 96 | - } |
|
| 97 | - |
|
| 98 | - private function register_block_type() { |
|
| 99 | - |
|
| 100 | - $scope = $this; |
|
| 101 | - |
|
| 102 | - add_action( 'init', function () use ( $scope ) { |
|
| 103 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 104 | - // Gutenberg is not active. |
|
| 105 | - return; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - register_block_type( 'wordlift/geomap', array( |
|
| 109 | - 'editor_script' => 'wl-block-editor', |
|
| 110 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 111 | - $attr_code = ''; |
|
| 112 | - foreach ( $attributes as $key => $value ) { |
|
| 113 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 117 | - }, |
|
| 118 | - 'attributes' => array( |
|
| 119 | - 'width' => array( |
|
| 120 | - 'type' => 'string', |
|
| 121 | - 'default' => '100%' |
|
| 122 | - ), |
|
| 123 | - 'height' => array( |
|
| 124 | - 'type' => 'string', |
|
| 125 | - 'default' => '300px' |
|
| 126 | - ), |
|
| 127 | - 'global' => array( |
|
| 128 | - 'type' => 'boolean', |
|
| 129 | - 'default' => false |
|
| 130 | - ), |
|
| 131 | - 'preview' => array( |
|
| 132 | - 'type' => 'boolean', |
|
| 133 | - 'default' => false, |
|
| 134 | - ), |
|
| 135 | - 'preview_src' => array( |
|
| 136 | - 'type' => 'string', |
|
| 137 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/geomap.png', |
|
| 138 | - ), |
|
| 139 | - ), |
|
| 140 | - ) ); |
|
| 141 | - } ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Customize the CSS when in AMP. |
|
| 146 | - * |
|
| 147 | - * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css |
|
| 148 | - * |
|
| 149 | - * @param object $amp_template The template. |
|
| 150 | - * |
|
| 151 | - * @since 3.13.0 |
|
| 152 | - * |
|
| 153 | - */ |
|
| 154 | - public function amp_post_template_css( $amp_template ) { |
|
| 155 | - |
|
| 156 | - // Hide the `wl-geomap` when in AMP. |
|
| 157 | - ?> |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + private function register_block_type() { |
|
| 99 | + |
|
| 100 | + $scope = $this; |
|
| 101 | + |
|
| 102 | + add_action( 'init', function () use ( $scope ) { |
|
| 103 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 104 | + // Gutenberg is not active. |
|
| 105 | + return; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + register_block_type( 'wordlift/geomap', array( |
|
| 109 | + 'editor_script' => 'wl-block-editor', |
|
| 110 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 111 | + $attr_code = ''; |
|
| 112 | + foreach ( $attributes as $key => $value ) { |
|
| 113 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 117 | + }, |
|
| 118 | + 'attributes' => array( |
|
| 119 | + 'width' => array( |
|
| 120 | + 'type' => 'string', |
|
| 121 | + 'default' => '100%' |
|
| 122 | + ), |
|
| 123 | + 'height' => array( |
|
| 124 | + 'type' => 'string', |
|
| 125 | + 'default' => '300px' |
|
| 126 | + ), |
|
| 127 | + 'global' => array( |
|
| 128 | + 'type' => 'boolean', |
|
| 129 | + 'default' => false |
|
| 130 | + ), |
|
| 131 | + 'preview' => array( |
|
| 132 | + 'type' => 'boolean', |
|
| 133 | + 'default' => false, |
|
| 134 | + ), |
|
| 135 | + 'preview_src' => array( |
|
| 136 | + 'type' => 'string', |
|
| 137 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/geomap.png', |
|
| 138 | + ), |
|
| 139 | + ), |
|
| 140 | + ) ); |
|
| 141 | + } ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Customize the CSS when in AMP. |
|
| 146 | + * |
|
| 147 | + * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css |
|
| 148 | + * |
|
| 149 | + * @param object $amp_template The template. |
|
| 150 | + * |
|
| 151 | + * @since 3.13.0 |
|
| 152 | + * |
|
| 153 | + */ |
|
| 154 | + public function amp_post_template_css( $amp_template ) { |
|
| 155 | + |
|
| 156 | + // Hide the `wl-geomap` when in AMP. |
|
| 157 | + ?> |
|
| 158 | 158 | .wl-geomap { display: none; } |
| 159 | 159 | <?php |
| 160 | - } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | 162 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | // Hook to the `amp_post_template_css` to hide ourselves when in AMP |
| 32 | 32 | // rendering. |
| 33 | - add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css', ) ); |
|
| 33 | + add_action('amp_post_template_css', array($this, 'amp_post_template_css',)); |
|
| 34 | 34 | $this->register_block_type(); |
| 35 | 35 | |
| 36 | 36 | } |
@@ -44,48 +44,48 @@ discard block |
||
| 44 | 44 | * @since 3.5.4 |
| 45 | 45 | * |
| 46 | 46 | */ |
| 47 | - public function render( $atts ) { |
|
| 47 | + public function render($atts) { |
|
| 48 | 48 | |
| 49 | 49 | // Extract attributes and set default values. |
| 50 | - $geomap_atts = shortcode_atts( array( |
|
| 50 | + $geomap_atts = shortcode_atts(array( |
|
| 51 | 51 | 'width' => '100%', |
| 52 | 52 | 'height' => '300px', |
| 53 | 53 | 'global' => false, |
| 54 | - ), $atts ); |
|
| 54 | + ), $atts); |
|
| 55 | 55 | |
| 56 | 56 | // Get id of the post |
| 57 | 57 | $post_id = get_the_ID(); |
| 58 | 58 | |
| 59 | - if ( $geomap_atts['global'] || is_null( $post_id ) ) { |
|
| 59 | + if ($geomap_atts['global'] || is_null($post_id)) { |
|
| 60 | 60 | // Global geomap |
| 61 | 61 | $geomap_id = 'wl_geomap_global'; |
| 62 | 62 | $post_id = null; |
| 63 | 63 | } else { |
| 64 | 64 | // Post-specific geomap |
| 65 | - $geomap_id = 'wl_geomap_' . $post_id; |
|
| 65 | + $geomap_id = 'wl_geomap_'.$post_id; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | |
| 69 | - wl_enqueue_leaflet( true ); |
|
| 69 | + wl_enqueue_leaflet(true); |
|
| 70 | 70 | |
| 71 | 71 | // Use the registered style which define an optional dependency to font-awesome. |
| 72 | 72 | // |
| 73 | 73 | // @see https://github.com/insideout10/wordlift-plugin/issues/699 |
| 74 | 74 | // wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
| 75 | - wp_enqueue_style( 'wordlift-ui' ); |
|
| 75 | + wp_enqueue_style('wordlift-ui'); |
|
| 76 | 76 | |
| 77 | 77 | $this->enqueue_scripts(); |
| 78 | 78 | |
| 79 | - wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array( |
|
| 80 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), // Global param |
|
| 79 | + wp_localize_script('wordlift-ui', 'wl_geomap_params', array( |
|
| 80 | + 'ajax_url' => admin_url('admin-ajax.php'), // Global param |
|
| 81 | 81 | 'action' => 'wl_geomap' // Global param |
| 82 | - ) ); |
|
| 82 | + )); |
|
| 83 | 83 | |
| 84 | 84 | // Escaping atts. |
| 85 | - $esc_id = esc_attr( $geomap_id ); |
|
| 86 | - $esc_width = esc_attr( $geomap_atts['width'] ); |
|
| 87 | - $esc_height = esc_attr( $geomap_atts['height'] ); |
|
| 88 | - $esc_post_id = esc_attr( $post_id ); |
|
| 85 | + $esc_id = esc_attr($geomap_id); |
|
| 86 | + $esc_width = esc_attr($geomap_atts['width']); |
|
| 87 | + $esc_height = esc_attr($geomap_atts['height']); |
|
| 88 | + $esc_post_id = esc_attr($post_id); |
|
| 89 | 89 | |
| 90 | 90 | // Return HTML template. |
| 91 | 91 | return <<<EOF |
@@ -99,21 +99,21 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | $scope = $this; |
| 101 | 101 | |
| 102 | - add_action( 'init', function () use ( $scope ) { |
|
| 103 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 102 | + add_action('init', function() use ($scope) { |
|
| 103 | + if ( ! function_exists('register_block_type')) { |
|
| 104 | 104 | // Gutenberg is not active. |
| 105 | 105 | return; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - register_block_type( 'wordlift/geomap', array( |
|
| 108 | + register_block_type('wordlift/geomap', array( |
|
| 109 | 109 | 'editor_script' => 'wl-block-editor', |
| 110 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 110 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 111 | 111 | $attr_code = ''; |
| 112 | - foreach ( $attributes as $key => $value ) { |
|
| 113 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 112 | + foreach ($attributes as $key => $value) { |
|
| 113 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 116 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 117 | 117 | }, |
| 118 | 118 | 'attributes' => array( |
| 119 | 119 | 'width' => array( |
@@ -134,10 +134,10 @@ discard block |
||
| 134 | 134 | ), |
| 135 | 135 | 'preview_src' => array( |
| 136 | 136 | 'type' => 'string', |
| 137 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/geomap.png', |
|
| 137 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/geomap.png', |
|
| 138 | 138 | ), |
| 139 | 139 | ), |
| 140 | - ) ); |
|
| 140 | + )); |
|
| 141 | 141 | } ); |
| 142 | 142 | } |
| 143 | 143 | |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * @since 3.13.0 |
| 152 | 152 | * |
| 153 | 153 | */ |
| 154 | - public function amp_post_template_css( $amp_template ) { |
|
| 154 | + public function amp_post_template_css($amp_template) { |
|
| 155 | 155 | |
| 156 | 156 | // Hide the `wl-geomap` when in AMP. |
| 157 | 157 | ?> |
@@ -16,344 +16,344 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Timeline_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.1.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The number of words to use for the excerpt, set in the `to_json` function |
|
| 30 | - * and used by a filter. |
|
| 31 | - * |
|
| 32 | - * @since 3.7.0 |
|
| 33 | - * @access private |
|
| 34 | - * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 35 | - */ |
|
| 36 | - private $excerpt_length; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * A singleton instance of the Timeline service (useful for unit tests). |
|
| 40 | - * |
|
| 41 | - * @since 3.1.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 44 | - */ |
|
| 45 | - private static $instance; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Create a Wordlift_Timeline_Service instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.1.0 |
|
| 51 | - * |
|
| 52 | - */ |
|
| 53 | - public function __construct() { |
|
| 54 | - |
|
| 55 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 56 | - |
|
| 57 | - self::$instance = $this; |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 63 | - * |
|
| 64 | - * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 65 | - * @since 3.1.0 |
|
| 66 | - * |
|
| 67 | - */ |
|
| 68 | - public static function get_instance() { |
|
| 69 | - |
|
| 70 | - return self::$instance; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Retrieve timeline events and output them in JSON. |
|
| 75 | - * |
|
| 76 | - * @since 3.1.0 |
|
| 77 | - */ |
|
| 78 | - public function ajax_timeline() { |
|
| 79 | - |
|
| 80 | - // Get the ID of the post who requested the timeline. |
|
| 81 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 82 | - |
|
| 83 | - // Get the events and transform them for the JSON response, then send them to the client. |
|
| 84 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Retrieve timeline events. |
|
| 90 | - * |
|
| 91 | - * @param int $post_id The post ID. |
|
| 92 | - * |
|
| 93 | - * @return array An array of event posts. |
|
| 94 | - * @since 3.1.0 |
|
| 95 | - * |
|
| 96 | - * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 97 | - * |
|
| 98 | - */ |
|
| 99 | - public function get_events( $post_id = null ) { |
|
| 100 | - |
|
| 101 | - // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 102 | - // posts if no post has been specified. |
|
| 103 | - $ids = ( is_numeric( $post_id ) |
|
| 104 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 105 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 106 | - |
|
| 107 | - // Add the post itself if it's an entity. |
|
| 108 | - if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 109 | - $ids[] = $post_id; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // If there's no entities, return an empty array right away. |
|
| 113 | - if ( 0 === sizeof( $ids ) ) { |
|
| 114 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 115 | - |
|
| 116 | - return array(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 120 | - |
|
| 121 | - $args = array( |
|
| 122 | - 'post__in' => $ids, |
|
| 123 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 124 | - 'post_status' => 'publish', |
|
| 125 | - 'posts_per_page' => - 1, |
|
| 126 | - 'meta_query' => array( |
|
| 127 | - 'relation' => 'AND', |
|
| 128 | - array( |
|
| 129 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 130 | - 'value' => null, |
|
| 131 | - 'compare' => '!=', |
|
| 132 | - ), |
|
| 133 | - array( |
|
| 134 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 135 | - 'value' => null, |
|
| 136 | - 'compare' => '!=', |
|
| 137 | - ), |
|
| 138 | - ), |
|
| 139 | - 'tax_query' => array( |
|
| 140 | - array( |
|
| 141 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 142 | - 'field' => 'slug', |
|
| 143 | - 'terms' => 'event', |
|
| 144 | - ), |
|
| 145 | - ), |
|
| 146 | - 'orderby' => 'meta_value', |
|
| 147 | - 'order' => 'DESC', |
|
| 148 | - 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - return get_posts( $args ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Convert timeline events to JSON. This function sets the global post in order |
|
| 156 | - * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 157 | - * we're not taking care of restoring any previous post: there isn't any. |
|
| 158 | - * |
|
| 159 | - * @param array $posts An array of posts. |
|
| 160 | - * |
|
| 161 | - * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 162 | - * @since 3.1.0 |
|
| 163 | - * |
|
| 164 | - */ |
|
| 165 | - public function to_json( $posts ) { |
|
| 166 | - |
|
| 167 | - // If there are no events, return empty JSON |
|
| 168 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 169 | - return ''; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 173 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 174 | - |
|
| 175 | - // The number of words for the excerpt (by default 55, as WordPress). |
|
| 176 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 177 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 178 | - |
|
| 179 | - // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 180 | - // a link to the post itself. |
|
| 181 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 182 | - |
|
| 183 | - // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 184 | - $start_at_slide = 0; |
|
| 185 | - $event_index = - 1; |
|
| 186 | - $now = time(); |
|
| 187 | - |
|
| 188 | - // Prepare the timeline variable. |
|
| 189 | - $timeline = array(); |
|
| 190 | - |
|
| 191 | - // Populate the arrays. |
|
| 192 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 193 | - |
|
| 194 | - // Get the start and end dates. |
|
| 195 | - // We have to remove double quotes from date to make timeline work properly |
|
| 196 | - $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
| 197 | - $end_date = strtotime( str_replace( '"', '',get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
| 198 | - |
|
| 199 | - // Set the starting slide. |
|
| 200 | - $event_index ++; |
|
| 201 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 202 | - $start_at_slide = $event_index; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - // Load thumbnail |
|
| 206 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 207 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 209 | - ) { |
|
| 210 | - |
|
| 211 | - // Set the thumbnail URL. |
|
| 212 | - if ( 'background' === $display_images_as ) { |
|
| 213 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 214 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 215 | - } else { |
|
| 216 | - $date['media'] = array( |
|
| 217 | - 'url' => $attachment[0], |
|
| 218 | - 'thumbnail' => $attachment[0], |
|
| 219 | - ); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // Set the start/end dates by converting them to TimelineJS required format. |
|
| 224 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 225 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 226 | - |
|
| 227 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 228 | - |
|
| 229 | - $more_link_text = sprintf( |
|
| 230 | - '<span aria-label="%1$s">%2$s</span>', |
|
| 231 | - sprintf( |
|
| 232 | - /* translators: %s: Name of current post */ |
|
| 233 | - __( 'Continue reading %s' ), |
|
| 234 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 235 | - ), |
|
| 236 | - __( '(more…)' ) |
|
| 237 | - ); |
|
| 238 | - |
|
| 239 | - // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 240 | - $date['text'] = array( |
|
| 241 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 242 | - ); |
|
| 243 | - |
|
| 244 | - // If we have an excerpt, set it. |
|
| 245 | - if ( 0 < $excerpt_length ) { |
|
| 246 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $date; |
|
| 250 | - |
|
| 251 | - }, $posts ); |
|
| 252 | - |
|
| 253 | - // Finally remove the excerpt filter. |
|
| 254 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 255 | - |
|
| 256 | - // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 257 | - return array( |
|
| 258 | - 'timeline' => $timeline, |
|
| 259 | - 'start_at_slide' => $start_at_slide, |
|
| 260 | - ); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * This function filters {@link excerpt_more} by removing it, since we're |
|
| 265 | - * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 266 | - * |
|
| 267 | - * @param string $excerpt_more The excerpt more preset. |
|
| 268 | - * |
|
| 269 | - * @return string An empty string. |
|
| 270 | - * @since 3.7.0 |
|
| 271 | - * |
|
| 272 | - */ |
|
| 273 | - public function excerpt_more( $excerpt_more ) { |
|
| 274 | - |
|
| 275 | - return ''; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 280 | - * how many words to return according to the client setting. |
|
| 281 | - * |
|
| 282 | - * @param int $length The preset number of words. |
|
| 283 | - * |
|
| 284 | - * @return int The number of words for the preset. |
|
| 285 | - * @since 3.7.0 |
|
| 286 | - * |
|
| 287 | - */ |
|
| 288 | - public function excerpt_length( $length ) { |
|
| 289 | - |
|
| 290 | - return $this->excerpt_length; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Convert the date to a date array. |
|
| 296 | - * |
|
| 297 | - * @param $value int A date value. |
|
| 298 | - * |
|
| 299 | - * @return array An array containing year, month and day values. |
|
| 300 | - * @since 3.7.0 |
|
| 301 | - * |
|
| 302 | - */ |
|
| 303 | - public static function date( $value ) { |
|
| 304 | - |
|
| 305 | - return array( |
|
| 306 | - 'year' => (int) date( 'Y', $value ), |
|
| 307 | - 'month' => (int) date( 'm', $value ), |
|
| 308 | - 'day' => (int) date( 'd', $value ), |
|
| 309 | - |
|
| 310 | - ); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 315 | - * its specific function). |
|
| 316 | - * |
|
| 317 | - * @return array An array of post IDs. |
|
| 318 | - * @since 3.1.0 |
|
| 319 | - * |
|
| 320 | - */ |
|
| 321 | - public function get_all_related_to_last_50_published_posts() { |
|
| 322 | - |
|
| 323 | - // Global timeline. Get entities from the latest posts. |
|
| 324 | - $latest_posts_ids = get_posts( array( |
|
| 325 | - 'numberposts' => 50, |
|
| 326 | - 'fields' => 'ids', //only get post IDs |
|
| 327 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 328 | - 'tax_query' => array( |
|
| 329 | - 'relation' => 'OR', |
|
| 330 | - array( |
|
| 331 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 332 | - 'operator' => 'NOT EXISTS', |
|
| 333 | - ), |
|
| 334 | - array( |
|
| 335 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 336 | - 'field' => 'slug', |
|
| 337 | - 'terms' => 'article', |
|
| 338 | - ), |
|
| 339 | - ), |
|
| 340 | - 'post_status' => 'publish', |
|
| 341 | - ) ); |
|
| 342 | - |
|
| 343 | - if ( empty( $latest_posts_ids ) ) { |
|
| 344 | - // There are no posts. |
|
| 345 | - return array(); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - // Collect entities related to latest posts |
|
| 349 | - $entity_ids = array(); |
|
| 350 | - foreach ( $latest_posts_ids as $id ) { |
|
| 351 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 352 | - 'status' => 'publish', |
|
| 353 | - ) ) ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - return $entity_ids; |
|
| 357 | - } |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.1.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The number of words to use for the excerpt, set in the `to_json` function |
|
| 30 | + * and used by a filter. |
|
| 31 | + * |
|
| 32 | + * @since 3.7.0 |
|
| 33 | + * @access private |
|
| 34 | + * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 35 | + */ |
|
| 36 | + private $excerpt_length; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * A singleton instance of the Timeline service (useful for unit tests). |
|
| 40 | + * |
|
| 41 | + * @since 3.1.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 44 | + */ |
|
| 45 | + private static $instance; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Create a Wordlift_Timeline_Service instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.1.0 |
|
| 51 | + * |
|
| 52 | + */ |
|
| 53 | + public function __construct() { |
|
| 54 | + |
|
| 55 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 56 | + |
|
| 57 | + self::$instance = $this; |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 63 | + * |
|
| 64 | + * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 65 | + * @since 3.1.0 |
|
| 66 | + * |
|
| 67 | + */ |
|
| 68 | + public static function get_instance() { |
|
| 69 | + |
|
| 70 | + return self::$instance; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Retrieve timeline events and output them in JSON. |
|
| 75 | + * |
|
| 76 | + * @since 3.1.0 |
|
| 77 | + */ |
|
| 78 | + public function ajax_timeline() { |
|
| 79 | + |
|
| 80 | + // Get the ID of the post who requested the timeline. |
|
| 81 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 82 | + |
|
| 83 | + // Get the events and transform them for the JSON response, then send them to the client. |
|
| 84 | + wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Retrieve timeline events. |
|
| 90 | + * |
|
| 91 | + * @param int $post_id The post ID. |
|
| 92 | + * |
|
| 93 | + * @return array An array of event posts. |
|
| 94 | + * @since 3.1.0 |
|
| 95 | + * |
|
| 96 | + * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 97 | + * |
|
| 98 | + */ |
|
| 99 | + public function get_events( $post_id = null ) { |
|
| 100 | + |
|
| 101 | + // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 102 | + // posts if no post has been specified. |
|
| 103 | + $ids = ( is_numeric( $post_id ) |
|
| 104 | + ? wl_core_get_related_entity_ids( $post_id ) |
|
| 105 | + : $this->get_all_related_to_last_50_published_posts() ); |
|
| 106 | + |
|
| 107 | + // Add the post itself if it's an entity. |
|
| 108 | + if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 109 | + $ids[] = $post_id; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // If there's no entities, return an empty array right away. |
|
| 113 | + if ( 0 === sizeof( $ids ) ) { |
|
| 114 | + $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 115 | + |
|
| 116 | + return array(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 120 | + |
|
| 121 | + $args = array( |
|
| 122 | + 'post__in' => $ids, |
|
| 123 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 124 | + 'post_status' => 'publish', |
|
| 125 | + 'posts_per_page' => - 1, |
|
| 126 | + 'meta_query' => array( |
|
| 127 | + 'relation' => 'AND', |
|
| 128 | + array( |
|
| 129 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 130 | + 'value' => null, |
|
| 131 | + 'compare' => '!=', |
|
| 132 | + ), |
|
| 133 | + array( |
|
| 134 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 135 | + 'value' => null, |
|
| 136 | + 'compare' => '!=', |
|
| 137 | + ), |
|
| 138 | + ), |
|
| 139 | + 'tax_query' => array( |
|
| 140 | + array( |
|
| 141 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 142 | + 'field' => 'slug', |
|
| 143 | + 'terms' => 'event', |
|
| 144 | + ), |
|
| 145 | + ), |
|
| 146 | + 'orderby' => 'meta_value', |
|
| 147 | + 'order' => 'DESC', |
|
| 148 | + 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + return get_posts( $args ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Convert timeline events to JSON. This function sets the global post in order |
|
| 156 | + * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 157 | + * we're not taking care of restoring any previous post: there isn't any. |
|
| 158 | + * |
|
| 159 | + * @param array $posts An array of posts. |
|
| 160 | + * |
|
| 161 | + * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 162 | + * @since 3.1.0 |
|
| 163 | + * |
|
| 164 | + */ |
|
| 165 | + public function to_json( $posts ) { |
|
| 166 | + |
|
| 167 | + // If there are no events, return empty JSON |
|
| 168 | + if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 169 | + return ''; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 173 | + $display_images_as = isset( $_REQUEST['display_images_as'] ) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 174 | + |
|
| 175 | + // The number of words for the excerpt (by default 55, as WordPress). |
|
| 176 | + $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 177 | + add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 178 | + |
|
| 179 | + // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 180 | + // a link to the post itself. |
|
| 181 | + add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 182 | + |
|
| 183 | + // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 184 | + $start_at_slide = 0; |
|
| 185 | + $event_index = - 1; |
|
| 186 | + $now = time(); |
|
| 187 | + |
|
| 188 | + // Prepare the timeline variable. |
|
| 189 | + $timeline = array(); |
|
| 190 | + |
|
| 191 | + // Populate the arrays. |
|
| 192 | + $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 193 | + |
|
| 194 | + // Get the start and end dates. |
|
| 195 | + // We have to remove double quotes from date to make timeline work properly |
|
| 196 | + $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
| 197 | + $end_date = strtotime( str_replace( '"', '',get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
| 198 | + |
|
| 199 | + // Set the starting slide. |
|
| 200 | + $event_index ++; |
|
| 201 | + if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 202 | + $start_at_slide = $event_index; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + // Load thumbnail |
|
| 206 | + $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 207 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | + && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 209 | + ) { |
|
| 210 | + |
|
| 211 | + // Set the thumbnail URL. |
|
| 212 | + if ( 'background' === $display_images_as ) { |
|
| 213 | + $date['background'] = array( 'url' => $attachment[0] ); |
|
| 214 | + $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 215 | + } else { |
|
| 216 | + $date['media'] = array( |
|
| 217 | + 'url' => $attachment[0], |
|
| 218 | + 'thumbnail' => $attachment[0], |
|
| 219 | + ); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // Set the start/end dates by converting them to TimelineJS required format. |
|
| 224 | + $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 225 | + $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 226 | + |
|
| 227 | + setup_postdata( $GLOBALS['post'] = $item ); |
|
| 228 | + |
|
| 229 | + $more_link_text = sprintf( |
|
| 230 | + '<span aria-label="%1$s">%2$s</span>', |
|
| 231 | + sprintf( |
|
| 232 | + /* translators: %s: Name of current post */ |
|
| 233 | + __( 'Continue reading %s' ), |
|
| 234 | + the_title_attribute( array( 'echo' => false ) ) |
|
| 235 | + ), |
|
| 236 | + __( '(more…)' ) |
|
| 237 | + ); |
|
| 238 | + |
|
| 239 | + // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 240 | + $date['text'] = array( |
|
| 241 | + 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 242 | + ); |
|
| 243 | + |
|
| 244 | + // If we have an excerpt, set it. |
|
| 245 | + if ( 0 < $excerpt_length ) { |
|
| 246 | + $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $date; |
|
| 250 | + |
|
| 251 | + }, $posts ); |
|
| 252 | + |
|
| 253 | + // Finally remove the excerpt filter. |
|
| 254 | + remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 255 | + |
|
| 256 | + // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 257 | + return array( |
|
| 258 | + 'timeline' => $timeline, |
|
| 259 | + 'start_at_slide' => $start_at_slide, |
|
| 260 | + ); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * This function filters {@link excerpt_more} by removing it, since we're |
|
| 265 | + * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 266 | + * |
|
| 267 | + * @param string $excerpt_more The excerpt more preset. |
|
| 268 | + * |
|
| 269 | + * @return string An empty string. |
|
| 270 | + * @since 3.7.0 |
|
| 271 | + * |
|
| 272 | + */ |
|
| 273 | + public function excerpt_more( $excerpt_more ) { |
|
| 274 | + |
|
| 275 | + return ''; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 280 | + * how many words to return according to the client setting. |
|
| 281 | + * |
|
| 282 | + * @param int $length The preset number of words. |
|
| 283 | + * |
|
| 284 | + * @return int The number of words for the preset. |
|
| 285 | + * @since 3.7.0 |
|
| 286 | + * |
|
| 287 | + */ |
|
| 288 | + public function excerpt_length( $length ) { |
|
| 289 | + |
|
| 290 | + return $this->excerpt_length; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Convert the date to a date array. |
|
| 296 | + * |
|
| 297 | + * @param $value int A date value. |
|
| 298 | + * |
|
| 299 | + * @return array An array containing year, month and day values. |
|
| 300 | + * @since 3.7.0 |
|
| 301 | + * |
|
| 302 | + */ |
|
| 303 | + public static function date( $value ) { |
|
| 304 | + |
|
| 305 | + return array( |
|
| 306 | + 'year' => (int) date( 'Y', $value ), |
|
| 307 | + 'month' => (int) date( 'm', $value ), |
|
| 308 | + 'day' => (int) date( 'd', $value ), |
|
| 309 | + |
|
| 310 | + ); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 315 | + * its specific function). |
|
| 316 | + * |
|
| 317 | + * @return array An array of post IDs. |
|
| 318 | + * @since 3.1.0 |
|
| 319 | + * |
|
| 320 | + */ |
|
| 321 | + public function get_all_related_to_last_50_published_posts() { |
|
| 322 | + |
|
| 323 | + // Global timeline. Get entities from the latest posts. |
|
| 324 | + $latest_posts_ids = get_posts( array( |
|
| 325 | + 'numberposts' => 50, |
|
| 326 | + 'fields' => 'ids', //only get post IDs |
|
| 327 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 328 | + 'tax_query' => array( |
|
| 329 | + 'relation' => 'OR', |
|
| 330 | + array( |
|
| 331 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 332 | + 'operator' => 'NOT EXISTS', |
|
| 333 | + ), |
|
| 334 | + array( |
|
| 335 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 336 | + 'field' => 'slug', |
|
| 337 | + 'terms' => 'article', |
|
| 338 | + ), |
|
| 339 | + ), |
|
| 340 | + 'post_status' => 'publish', |
|
| 341 | + ) ); |
|
| 342 | + |
|
| 343 | + if ( empty( $latest_posts_ids ) ) { |
|
| 344 | + // There are no posts. |
|
| 345 | + return array(); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + // Collect entities related to latest posts |
|
| 349 | + $entity_ids = array(); |
|
| 350 | + foreach ( $latest_posts_ids as $id ) { |
|
| 351 | + $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 352 | + 'status' => 'publish', |
|
| 353 | + ) ) ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + return $entity_ids; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | 359 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function __construct() { |
| 54 | 54 | |
| 55 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 55 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service'); |
|
| 56 | 56 | |
| 57 | 57 | self::$instance = $this; |
| 58 | 58 | |
@@ -78,10 +78,10 @@ discard block |
||
| 78 | 78 | public function ajax_timeline() { |
| 79 | 79 | |
| 80 | 80 | // Get the ID of the post who requested the timeline. |
| 81 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 81 | + $post_id = (isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : null); |
|
| 82 | 82 | |
| 83 | 83 | // Get the events and transform them for the JSON response, then send them to the client. |
| 84 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 84 | + wp_send_json($this->to_json($this->get_events($post_id))); |
|
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | |
@@ -96,33 +96,33 @@ discard block |
||
| 96 | 96 | * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
| 97 | 97 | * |
| 98 | 98 | */ |
| 99 | - public function get_events( $post_id = null ) { |
|
| 99 | + public function get_events($post_id = null) { |
|
| 100 | 100 | |
| 101 | 101 | // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
| 102 | 102 | // posts if no post has been specified. |
| 103 | - $ids = ( is_numeric( $post_id ) |
|
| 104 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 105 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 103 | + $ids = (is_numeric($post_id) |
|
| 104 | + ? wl_core_get_related_entity_ids($post_id) |
|
| 105 | + : $this->get_all_related_to_last_50_published_posts()); |
|
| 106 | 106 | |
| 107 | 107 | // Add the post itself if it's an entity. |
| 108 | - if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 108 | + if (is_numeric($post_id) && Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
| 109 | 109 | $ids[] = $post_id; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // If there's no entities, return an empty array right away. |
| 113 | - if ( 0 === sizeof( $ids ) ) { |
|
| 114 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 113 | + if (0 === sizeof($ids)) { |
|
| 114 | + $this->log->trace("No events found [ post id :: $post_id ]"); |
|
| 115 | 115 | |
| 116 | 116 | return array(); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 119 | + $this->log->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]'); |
|
| 120 | 120 | |
| 121 | 121 | $args = array( |
| 122 | 122 | 'post__in' => $ids, |
| 123 | 123 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 124 | 124 | 'post_status' => 'publish', |
| 125 | - 'posts_per_page' => - 1, |
|
| 125 | + 'posts_per_page' => -1, |
|
| 126 | 126 | 'meta_query' => array( |
| 127 | 127 | 'relation' => 'AND', |
| 128 | 128 | array( |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
| 149 | 149 | ); |
| 150 | 150 | |
| 151 | - return get_posts( $args ); |
|
| 151 | + return get_posts($args); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -162,23 +162,23 @@ discard block |
||
| 162 | 162 | * @since 3.1.0 |
| 163 | 163 | * |
| 164 | 164 | */ |
| 165 | - public function to_json( $posts ) { |
|
| 165 | + public function to_json($posts) { |
|
| 166 | 166 | |
| 167 | 167 | // If there are no events, return empty JSON |
| 168 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 168 | + if (empty($posts) || is_null($posts)) { |
|
| 169 | 169 | return ''; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
| 173 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 173 | + $display_images_as = isset($_REQUEST['display_images_as']) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 174 | 174 | |
| 175 | 175 | // The number of words for the excerpt (by default 55, as WordPress). |
| 176 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 177 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 176 | + $this->excerpt_length = $excerpt_length = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? $_REQUEST['excerpt_length'] : 55; |
|
| 177 | + add_filter('excerpt_length', array($this, 'excerpt_length')); |
|
| 178 | 178 | |
| 179 | 179 | // Add a filter to remove the [...] after excerpts, since we're adding |
| 180 | 180 | // a link to the post itself. |
| 181 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 181 | + add_filter('excerpt_more', array($this, 'excerpt_more')); |
|
| 182 | 182 | |
| 183 | 183 | // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
| 184 | 184 | $start_at_slide = 0; |
@@ -189,29 +189,29 @@ discard block |
||
| 189 | 189 | $timeline = array(); |
| 190 | 190 | |
| 191 | 191 | // Populate the arrays. |
| 192 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 192 | + $timeline['events'] = array_map(function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) { |
|
| 193 | 193 | |
| 194 | 194 | // Get the start and end dates. |
| 195 | 195 | // We have to remove double quotes from date to make timeline work properly |
| 196 | - $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
| 197 | - $end_date = strtotime( str_replace( '"', '',get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
| 196 | + $start_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true))); |
|
| 197 | + $end_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true))); |
|
| 198 | 198 | |
| 199 | 199 | // Set the starting slide. |
| 200 | - $event_index ++; |
|
| 201 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 200 | + $event_index++; |
|
| 201 | + if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) { |
|
| 202 | 202 | $start_at_slide = $event_index; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | // Load thumbnail |
| 206 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 207 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 206 | + $thumbnail_id = get_post_thumbnail_id($item->ID); |
|
| 207 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | + && false !== ($attachment = wp_get_attachment_image_src($thumbnail_id)) |
|
| 209 | 209 | ) { |
| 210 | 210 | |
| 211 | 211 | // Set the thumbnail URL. |
| 212 | - if ( 'background' === $display_images_as ) { |
|
| 213 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 214 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 212 | + if ('background' === $display_images_as) { |
|
| 213 | + $date['background'] = array('url' => $attachment[0]); |
|
| 214 | + $date['media'] = array('thumbnail' => $attachment[0]); |
|
| 215 | 215 | } else { |
| 216 | 216 | $date['media'] = array( |
| 217 | 217 | 'url' => $attachment[0], |
@@ -221,37 +221,37 @@ discard block |
||
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | // Set the start/end dates by converting them to TimelineJS required format. |
| 224 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 225 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 224 | + $date['start_date'] = Wordlift_Timeline_Service::date($start_date); |
|
| 225 | + $date['end_date'] = Wordlift_Timeline_Service::date($end_date); |
|
| 226 | 226 | |
| 227 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 227 | + setup_postdata($GLOBALS['post'] = $item); |
|
| 228 | 228 | |
| 229 | 229 | $more_link_text = sprintf( |
| 230 | 230 | '<span aria-label="%1$s">%2$s</span>', |
| 231 | 231 | sprintf( |
| 232 | 232 | /* translators: %s: Name of current post */ |
| 233 | - __( 'Continue reading %s' ), |
|
| 234 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 233 | + __('Continue reading %s'), |
|
| 234 | + the_title_attribute(array('echo' => false)) |
|
| 235 | 235 | ), |
| 236 | - __( '(more…)' ) |
|
| 236 | + __('(more…)') |
|
| 237 | 237 | ); |
| 238 | 238 | |
| 239 | 239 | // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
| 240 | 240 | $date['text'] = array( |
| 241 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 241 | + 'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>', |
|
| 242 | 242 | ); |
| 243 | 243 | |
| 244 | 244 | // If we have an excerpt, set it. |
| 245 | - if ( 0 < $excerpt_length ) { |
|
| 246 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 245 | + if (0 < $excerpt_length) { |
|
| 246 | + $date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | return $date; |
| 250 | 250 | |
| 251 | - }, $posts ); |
|
| 251 | + }, $posts); |
|
| 252 | 252 | |
| 253 | 253 | // Finally remove the excerpt filter. |
| 254 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 254 | + remove_filter('excerpt_length', array($this, 'excerpt_length')); |
|
| 255 | 255 | |
| 256 | 256 | // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
| 257 | 257 | return array( |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | * @since 3.7.0 |
| 271 | 271 | * |
| 272 | 272 | */ |
| 273 | - public function excerpt_more( $excerpt_more ) { |
|
| 273 | + public function excerpt_more($excerpt_more) { |
|
| 274 | 274 | |
| 275 | 275 | return ''; |
| 276 | 276 | } |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * @since 3.7.0 |
| 286 | 286 | * |
| 287 | 287 | */ |
| 288 | - public function excerpt_length( $length ) { |
|
| 288 | + public function excerpt_length($length) { |
|
| 289 | 289 | |
| 290 | 290 | return $this->excerpt_length; |
| 291 | 291 | } |
@@ -300,12 +300,12 @@ discard block |
||
| 300 | 300 | * @since 3.7.0 |
| 301 | 301 | * |
| 302 | 302 | */ |
| 303 | - public static function date( $value ) { |
|
| 303 | + public static function date($value) { |
|
| 304 | 304 | |
| 305 | 305 | return array( |
| 306 | - 'year' => (int) date( 'Y', $value ), |
|
| 307 | - 'month' => (int) date( 'm', $value ), |
|
| 308 | - 'day' => (int) date( 'd', $value ), |
|
| 306 | + 'year' => (int) date('Y', $value), |
|
| 307 | + 'month' => (int) date('m', $value), |
|
| 308 | + 'day' => (int) date('d', $value), |
|
| 309 | 309 | |
| 310 | 310 | ); |
| 311 | 311 | } |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | public function get_all_related_to_last_50_published_posts() { |
| 322 | 322 | |
| 323 | 323 | // Global timeline. Get entities from the latest posts. |
| 324 | - $latest_posts_ids = get_posts( array( |
|
| 324 | + $latest_posts_ids = get_posts(array( |
|
| 325 | 325 | 'numberposts' => 50, |
| 326 | 326 | 'fields' => 'ids', //only get post IDs |
| 327 | 327 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
@@ -338,19 +338,19 @@ discard block |
||
| 338 | 338 | ), |
| 339 | 339 | ), |
| 340 | 340 | 'post_status' => 'publish', |
| 341 | - ) ); |
|
| 341 | + )); |
|
| 342 | 342 | |
| 343 | - if ( empty( $latest_posts_ids ) ) { |
|
| 343 | + if (empty($latest_posts_ids)) { |
|
| 344 | 344 | // There are no posts. |
| 345 | 345 | return array(); |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | // Collect entities related to latest posts |
| 349 | 349 | $entity_ids = array(); |
| 350 | - foreach ( $latest_posts_ids as $id ) { |
|
| 351 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 350 | + foreach ($latest_posts_ids as $id) { |
|
| 351 | + $entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array( |
|
| 352 | 352 | 'status' => 'publish', |
| 353 | - ) ) ); |
|
| 353 | + ))); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | return $entity_ids; |
@@ -16,9 +16,9 @@ discard block |
||
| 16 | 16 | * @deprecated use Wordlift_Log_Service::get_instance()->info( $log ); |
| 17 | 17 | * |
| 18 | 18 | */ |
| 19 | -function wl_write_log( $log ) { |
|
| 19 | +function wl_write_log($log) { |
|
| 20 | 20 | |
| 21 | - Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 21 | + Wordlift_Log_Service::get_instance()->debug($log); |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | function wordlift_allowed_post_tags() { |
| 30 | 30 | global $allowedposttags; |
| 31 | 31 | |
| 32 | - $tags = array( 'span' ); |
|
| 32 | + $tags = array('span'); |
|
| 33 | 33 | $new_attributes = array( |
| 34 | 34 | 'itemscope' => array(), |
| 35 | 35 | 'itemtype' => array(), |
@@ -37,15 +37,15 @@ discard block |
||
| 37 | 37 | 'itemid' => array(), |
| 38 | 38 | ); |
| 39 | 39 | |
| 40 | - foreach ( $tags as $tag ) { |
|
| 41 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 42 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 40 | + foreach ($tags as $tag) { |
|
| 41 | + if (isset($allowedposttags[$tag]) && is_array($allowedposttags[$tag])) { |
|
| 42 | + $allowedposttags[$tag] = array_merge($allowedposttags[$tag], $new_attributes); |
|
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // add allowed post tags. |
| 48 | -add_action( 'init', 'wordlift_allowed_post_tags' ); |
|
| 48 | +add_action('init', 'wordlift_allowed_post_tags'); |
|
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * Register additional scripts for the admin UI. |
@@ -53,21 +53,21 @@ discard block |
||
| 53 | 53 | function wordlift_admin_enqueue_scripts() { |
| 54 | 54 | |
| 55 | 55 | // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
| 56 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 57 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 56 | + wp_enqueue_script('wpdialogs'); |
|
| 57 | + wp_enqueue_style('wp-jquery-ui-dialog'); |
|
| 58 | 58 | |
| 59 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 59 | + wp_enqueue_style('wordlift-reloaded', plugin_dir_url(__FILE__).'css/wordlift-reloaded.min.css'); |
|
| 60 | 60 | |
| 61 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 61 | + wp_enqueue_script('jquery-ui-autocomplete'); |
|
| 62 | 62 | |
| 63 | 63 | // Disable auto-save for custom entity posts only |
| 64 | - if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 65 | - wp_dequeue_script( 'autosave' ); |
|
| 64 | + if (Wordlift_Entity_Service::TYPE_NAME === get_post_type()) { |
|
| 65 | + wp_dequeue_script('autosave'); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | -add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' ); |
|
| 70 | +add_action('admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts'); |
|
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * Hooked to *wp_kses_allowed_html* filter, adds microdata attributes. |
@@ -77,23 +77,23 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @return array An array which contains allowed microdata attributes. |
| 79 | 79 | */ |
| 80 | -function wordlift_allowed_html( $allowedtags, $context ) { |
|
| 80 | +function wordlift_allowed_html($allowedtags, $context) { |
|
| 81 | 81 | |
| 82 | - if ( 'post' !== $context ) { |
|
| 82 | + if ('post' !== $context) { |
|
| 83 | 83 | return $allowedtags; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - return array_merge_recursive( $allowedtags, array( |
|
| 86 | + return array_merge_recursive($allowedtags, array( |
|
| 87 | 87 | 'span' => array( |
| 88 | 88 | 'itemscope' => true, |
| 89 | 89 | 'itemtype' => true, |
| 90 | 90 | 'itemid' => true, |
| 91 | 91 | 'itemprop' => true, |
| 92 | 92 | ), |
| 93 | - ) ); |
|
| 93 | + )); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | -add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
|
| 96 | +add_filter('wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2); |
|
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | 99 | * Get all the images bound to a post. |
@@ -104,11 +104,11 @@ discard block |
||
| 104 | 104 | * @deprecated use Wordlift_Storage_Factory::get_instance()->post_images()->get( $post_id ) |
| 105 | 105 | * |
| 106 | 106 | */ |
| 107 | -function wl_get_image_urls( $post_id ) { |
|
| 107 | +function wl_get_image_urls($post_id) { |
|
| 108 | 108 | |
| 109 | 109 | return Wordlift_Storage_Factory::get_instance() |
| 110 | 110 | ->post_images() |
| 111 | - ->get( $post_id ); |
|
| 111 | + ->get($post_id); |
|
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | |
@@ -120,21 +120,21 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @return WP_Post|null A post instance or null if not found. |
| 122 | 122 | */ |
| 123 | -function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
|
| 123 | +function wl_get_attachment_for_source_url($parent_post_id, $source_url) { |
|
| 124 | 124 | |
| 125 | 125 | // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
| 126 | 126 | |
| 127 | - $posts = get_posts( array( |
|
| 127 | + $posts = get_posts(array( |
|
| 128 | 128 | 'post_type' => 'attachment', |
| 129 | 129 | 'posts_per_page' => 1, |
| 130 | 130 | 'post_status' => 'any', |
| 131 | 131 | 'post_parent' => $parent_post_id, |
| 132 | 132 | 'meta_key' => 'wl_source_url', |
| 133 | 133 | 'meta_value' => $source_url, |
| 134 | - ) ); |
|
| 134 | + )); |
|
| 135 | 135 | |
| 136 | 136 | // Return the found post. |
| 137 | - if ( 1 === count( $posts ) ) { |
|
| 137 | + if (1 === count($posts)) { |
|
| 138 | 138 | return $posts[0]; |
| 139 | 139 | } |
| 140 | 140 | |
@@ -148,10 +148,10 @@ discard block |
||
| 148 | 148 | * @param int $post_id The post ID. |
| 149 | 149 | * @param string $source_url The source URL. |
| 150 | 150 | */ |
| 151 | -function wl_set_source_url( $post_id, $source_url ) { |
|
| 151 | +function wl_set_source_url($post_id, $source_url) { |
|
| 152 | 152 | |
| 153 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 154 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 153 | + delete_post_meta($post_id, 'wl_source_url'); |
|
| 154 | + add_post_meta($post_id, 'wl_source_url', $source_url); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -166,9 +166,9 @@ discard block |
||
| 166 | 166 | * @see https://codex.wordpress.org/Function_Reference/sanitize_title |
| 167 | 167 | * |
| 168 | 168 | */ |
| 169 | -function wl_sanitize_uri_path( $path, $char = '_' ) { |
|
| 169 | +function wl_sanitize_uri_path($path, $char = '_') { |
|
| 170 | 170 | |
| 171 | - return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 171 | + return Wordlift_Uri_Service::get_instance()->sanitize_path($path, $char); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -178,95 +178,95 @@ discard block |
||
| 178 | 178 | * |
| 179 | 179 | * @return string The updated post content. |
| 180 | 180 | */ |
| 181 | -function wl_replace_item_id_with_uri( $content ) { |
|
| 181 | +function wl_replace_item_id_with_uri($content) { |
|
| 182 | 182 | |
| 183 | - $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 184 | - $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 183 | + $log = Wordlift_Log_Service::get_logger('wl_replace_item_id_with_uri'); |
|
| 184 | + $log->trace('Replacing item IDs with URIs...'); |
|
| 185 | 185 | |
| 186 | 186 | // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
| 187 | - $content = stripslashes( $content ); |
|
| 187 | + $content = stripslashes($content); |
|
| 188 | 188 | |
| 189 | 189 | // If any match are found. |
| 190 | 190 | $matches = array(); |
| 191 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 191 | + if (0 < preg_match_all('/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER)) { |
|
| 192 | 192 | |
| 193 | - foreach ( $matches as $match ) { |
|
| 193 | + foreach ($matches as $match) { |
|
| 194 | 194 | |
| 195 | 195 | // Get the item ID. |
| 196 | 196 | $item_id = $match[1]; |
| 197 | 197 | |
| 198 | 198 | // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
| 199 | 199 | $post = Wordlift_Entity_Service::get_instance() |
| 200 | - ->get_entity_post_by_uri( $item_id ); |
|
| 200 | + ->get_entity_post_by_uri($item_id); |
|
| 201 | 201 | |
| 202 | 202 | // If no entity is found, continue to the next one. |
| 203 | - if ( null === $post ) { |
|
| 203 | + if (null === $post) { |
|
| 204 | 204 | continue; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // Get the URI for that post. |
| 208 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 208 | + $uri = wl_get_entity_uri($post->ID); |
|
| 209 | 209 | |
| 210 | 210 | // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
| 211 | 211 | |
| 212 | 212 | // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
| 213 | - if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 214 | - $uri_e = esc_html( $uri ); |
|
| 215 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 213 | + if ( ! empty($uri) && $item_id !== $uri) { |
|
| 214 | + $uri_e = esc_html($uri); |
|
| 215 | + $content = str_replace(" itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content); |
|
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | // Reapply slashes. |
| 221 | - $content = addslashes( $content ); |
|
| 221 | + $content = addslashes($content); |
|
| 222 | 222 | |
| 223 | 223 | return $content; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | -add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
|
| 226 | +add_filter('content_save_pre', 'wl_replace_item_id_with_uri', 1, 1); |
|
| 227 | 227 | |
| 228 | -require_once( 'wordlift_entity_functions.php' ); |
|
| 228 | +require_once('wordlift_entity_functions.php'); |
|
| 229 | 229 | |
| 230 | 230 | // add editor related methods. |
| 231 | -require_once( 'wordlift_editor.php' ); |
|
| 231 | +require_once('wordlift_editor.php'); |
|
| 232 | 232 | |
| 233 | 233 | // add the WordLift entity custom type. |
| 234 | -require_once( 'wordlift_entity_type.php' ); |
|
| 234 | +require_once('wordlift_entity_type.php'); |
|
| 235 | 235 | |
| 236 | -require_once( 'modules/common/load.php' ); |
|
| 237 | -require_once( 'modules/configuration/wordlift_configuration_settings.php' ); |
|
| 236 | +require_once('modules/common/load.php'); |
|
| 237 | +require_once('modules/configuration/wordlift_configuration_settings.php'); |
|
| 238 | 238 | |
| 239 | 239 | // Load modules |
| 240 | -require_once( 'modules/analyzer/wordlift_analyzer.php' ); |
|
| 241 | -require_once( 'modules/linked_data/wordlift_linked_data.php' ); |
|
| 242 | -require_once( 'modules/acf4so/load.php' ); |
|
| 240 | +require_once('modules/analyzer/wordlift_analyzer.php'); |
|
| 241 | +require_once('modules/linked_data/wordlift_linked_data.php'); |
|
| 242 | +require_once('modules/acf4so/load.php'); |
|
| 243 | 243 | |
| 244 | 244 | // Shortcodes |
| 245 | -require_once( 'shortcodes/class-wordlift-shortcode-rest.php' ); |
|
| 246 | -require_once( 'shortcodes/wordlift_shortcode_chord.php' ); |
|
| 247 | -require_once( 'shortcodes/wordlift_shortcode_geomap.php' ); |
|
| 248 | -require_once( 'shortcodes/wordlift_shortcode_field.php' ); |
|
| 249 | -require_once( 'shortcodes/wordlift_shortcode_faceted_search.php' ); |
|
| 250 | -require_once( 'shortcodes/wordlift_shortcode_navigator.php' ); |
|
| 251 | -require_once( 'shortcodes/class-wordlift-products-navigator-shortcode-rest.php' ); |
|
| 252 | - |
|
| 253 | -require_once( 'widgets/wordlift_widget_geo.php' ); |
|
| 254 | -require_once( 'widgets/class-wordlift-chord-widget.php' ); |
|
| 255 | -require_once( 'widgets/wordlift_widget_timeline.php' ); |
|
| 245 | +require_once('shortcodes/class-wordlift-shortcode-rest.php'); |
|
| 246 | +require_once('shortcodes/wordlift_shortcode_chord.php'); |
|
| 247 | +require_once('shortcodes/wordlift_shortcode_geomap.php'); |
|
| 248 | +require_once('shortcodes/wordlift_shortcode_field.php'); |
|
| 249 | +require_once('shortcodes/wordlift_shortcode_faceted_search.php'); |
|
| 250 | +require_once('shortcodes/wordlift_shortcode_navigator.php'); |
|
| 251 | +require_once('shortcodes/class-wordlift-products-navigator-shortcode-rest.php'); |
|
| 252 | + |
|
| 253 | +require_once('widgets/wordlift_widget_geo.php'); |
|
| 254 | +require_once('widgets/class-wordlift-chord-widget.php'); |
|
| 255 | +require_once('widgets/wordlift_widget_timeline.php'); |
|
| 256 | 256 | |
| 257 | 257 | // Add admin functions. |
| 258 | 258 | // TODO: find a way to make 'admin' UI tests work. |
| 259 | 259 | //if ( is_admin() ) { |
| 260 | 260 | |
| 261 | -require_once( 'admin/wordlift_admin.php' ); |
|
| 262 | -require_once( 'admin/wordlift_admin_edit_post.php' ); |
|
| 263 | -require_once( 'admin/wordlift_admin_save_post.php' ); |
|
| 261 | +require_once('admin/wordlift_admin.php'); |
|
| 262 | +require_once('admin/wordlift_admin_edit_post.php'); |
|
| 263 | +require_once('admin/wordlift_admin_save_post.php'); |
|
| 264 | 264 | |
| 265 | 265 | // add the entities meta box. |
| 266 | -require_once( 'admin/wordlift_admin_meta_box_entities.php' ); |
|
| 266 | +require_once('admin/wordlift_admin_meta_box_entities.php'); |
|
| 267 | 267 | |
| 268 | 268 | // add the entity creation AJAX. |
| 269 | -require_once( 'admin/wordlift_admin_ajax_related_posts.php' ); |
|
| 269 | +require_once('admin/wordlift_admin_ajax_related_posts.php'); |
|
| 270 | 270 | |
| 271 | 271 | // Load the wl_chord TinyMCE button and configuration dialog. |
| 272 | -require_once( 'admin/wordlift_admin_shortcodes.php' ); |
|
| 272 | +require_once('admin/wordlift_admin_shortcodes.php'); |
|
@@ -11,58 +11,58 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class Key_Validation_Notice { |
| 13 | 13 | |
| 14 | - const CACHE_KEY = 'is_key_valid'; |
|
| 14 | + const CACHE_KEY = 'is_key_valid'; |
|
| 15 | 15 | |
| 16 | - const KEY_VALIDATION_NONCE_ACTION = 'wl_key_validation_notice_nonce'; |
|
| 16 | + const KEY_VALIDATION_NONCE_ACTION = 'wl_key_validation_notice_nonce'; |
|
| 17 | 17 | |
| 18 | - const KEY_VALIDATION_NONCE_PARAM = '_wl_key_validation_notice_nonce'; |
|
| 18 | + const KEY_VALIDATION_NONCE_PARAM = '_wl_key_validation_notice_nonce'; |
|
| 19 | 19 | |
| 20 | - const KEY_VALIDATION_NOTICE_PARAM = 'wl_key_validation_notice'; |
|
| 20 | + const KEY_VALIDATION_NOTICE_PARAM = 'wl_key_validation_notice'; |
|
| 21 | 21 | |
| 22 | - const NOTIFICATION_OPTION_KEY = 'wordlift_key_validation_notification_shown'; |
|
| 22 | + const NOTIFICATION_OPTION_KEY = 'wordlift_key_validation_notification_shown'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var \Wordlift_Key_Validation_Service |
|
| 26 | - */ |
|
| 27 | - private $key_validation_service; |
|
| 24 | + /** |
|
| 25 | + * @var \Wordlift_Key_Validation_Service |
|
| 26 | + */ |
|
| 27 | + private $key_validation_service; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @var Ttl_Cache |
|
| 31 | - */ |
|
| 32 | - private $ttl_cache_service; |
|
| 29 | + /** |
|
| 30 | + * @var Ttl_Cache |
|
| 31 | + */ |
|
| 32 | + private $ttl_cache_service; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var Wordlift_Configuration_Service |
|
| 36 | - */ |
|
| 37 | - private $configuration_service; |
|
| 34 | + /** |
|
| 35 | + * @var Wordlift_Configuration_Service |
|
| 36 | + */ |
|
| 37 | + private $configuration_service; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Key_Validation_Notice constructor. |
|
| 41 | - * |
|
| 42 | - * @param \Wordlift_Key_Validation_Service $key_validation_service |
|
| 43 | - * @param Wordlift_Configuration_Service $configuration_service |
|
| 44 | - */ |
|
| 45 | - public function __construct( $key_validation_service, $configuration_service ) { |
|
| 39 | + /** |
|
| 40 | + * Key_Validation_Notice constructor. |
|
| 41 | + * |
|
| 42 | + * @param \Wordlift_Key_Validation_Service $key_validation_service |
|
| 43 | + * @param Wordlift_Configuration_Service $configuration_service |
|
| 44 | + */ |
|
| 45 | + public function __construct( $key_validation_service, $configuration_service ) { |
|
| 46 | 46 | |
| 47 | - $this->key_validation_service = $key_validation_service; |
|
| 48 | - $this->configuration_service = $configuration_service; |
|
| 47 | + $this->key_validation_service = $key_validation_service; |
|
| 48 | + $this->configuration_service = $configuration_service; |
|
| 49 | 49 | |
| 50 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification', 60 * 60 * 8 ); |
|
| 50 | + $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification', 60 * 60 * 8 ); |
|
| 51 | 51 | |
| 52 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 53 | - $this->display_key_validation_notice(); |
|
| 54 | - } |
|
| 52 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 53 | + $this->display_key_validation_notice(); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - $that = $this; |
|
| 57 | - add_action( 'plugins_loaded', function () use ( $that ) { |
|
| 58 | - $that->notification_close_handler(); |
|
| 59 | - } ); |
|
| 60 | - } |
|
| 56 | + $that = $this; |
|
| 57 | + add_action( 'plugins_loaded', function () use ( $that ) { |
|
| 58 | + $that->notification_close_handler(); |
|
| 59 | + } ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | |
| 63 | - public function show_notification() { |
|
| 64 | - $settings_url = admin_url( 'admin.php?page=wl_configuration_admin_menu' ); |
|
| 65 | - ?> |
|
| 63 | + public function show_notification() { |
|
| 64 | + $settings_url = admin_url( 'admin.php?page=wl_configuration_admin_menu' ); |
|
| 65 | + ?> |
|
| 66 | 66 | <div class="wl-notice notice is-dismissible error"> |
| 67 | 67 | <p> |
| 68 | 68 | <?php echo wp_kses( __( "Your WordLift key is not valid, please update the key in <a href='$settings_url'>WordLift Settings</a> or contact our support at [email protected].", 'wordlift' ), array( 'a' => array( 'href' ) ) ); ?> |
@@ -71,70 +71,70 @@ discard block |
||
| 71 | 71 | </button> |
| 72 | 72 | </div> |
| 73 | 73 | <?php |
| 74 | - } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - private function is_key_valid() { |
|
| 76 | + private function is_key_valid() { |
|
| 77 | 77 | |
| 78 | - $key = $this->configuration_service->get_key(); |
|
| 78 | + $key = $this->configuration_service->get_key(); |
|
| 79 | 79 | |
| 80 | - // Check cache if the result is present, if not get the results |
|
| 81 | - // save it and return the data. |
|
| 82 | - if ( $this->ttl_cache_service->get( self::CACHE_KEY ) !== null ) { |
|
| 83 | - return $this->ttl_cache_service->get( self::CACHE_KEY ); |
|
| 84 | - } |
|
| 80 | + // Check cache if the result is present, if not get the results |
|
| 81 | + // save it and return the data. |
|
| 82 | + if ( $this->ttl_cache_service->get( self::CACHE_KEY ) !== null ) { |
|
| 83 | + return $this->ttl_cache_service->get( self::CACHE_KEY ); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - $is_valid = $this->key_validation_service->is_key_valid( $key ); |
|
| 86 | + $is_valid = $this->key_validation_service->is_key_valid( $key ); |
|
| 87 | 87 | |
| 88 | - // when the cache is set, clear the notification flag. |
|
| 89 | - delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
| 88 | + // when the cache is set, clear the notification flag. |
|
| 89 | + delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
| 90 | 90 | |
| 91 | - $this->ttl_cache_service->put( self::CACHE_KEY, $is_valid ); |
|
| 91 | + $this->ttl_cache_service->put( self::CACHE_KEY, $is_valid ); |
|
| 92 | 92 | |
| 93 | - return $is_valid; |
|
| 94 | - } |
|
| 93 | + return $is_valid; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - private function display_key_validation_notice() { |
|
| 97 | - $that = $this; |
|
| 98 | - add_action( 'admin_notices', function () use ( $that ) { |
|
| 96 | + private function display_key_validation_notice() { |
|
| 97 | + $that = $this; |
|
| 98 | + add_action( 'admin_notices', function () use ( $that ) { |
|
| 99 | 99 | |
| 100 | - $is_notification_shown = get_option( Key_Validation_Notice::NOTIFICATION_OPTION_KEY, false ); |
|
| 100 | + $is_notification_shown = get_option( Key_Validation_Notice::NOTIFICATION_OPTION_KEY, false ); |
|
| 101 | 101 | |
| 102 | - $key = $that->configuration_service->get_key(); |
|
| 102 | + $key = $that->configuration_service->get_key(); |
|
| 103 | 103 | |
| 104 | - if ( ! $key ) { |
|
| 105 | - // Dont show warning or make API call, return early. |
|
| 106 | - return; |
|
| 107 | - } |
|
| 104 | + if ( ! $key ) { |
|
| 105 | + // Dont show warning or make API call, return early. |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - if ( $that->is_key_valid() ) { |
|
| 110 | - return; |
|
| 111 | - } |
|
| 109 | + if ( $that->is_key_valid() ) { |
|
| 110 | + return; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - if ( $is_notification_shown ) { |
|
| 114 | - return; |
|
| 115 | - } |
|
| 113 | + if ( $is_notification_shown ) { |
|
| 114 | + return; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - $that->show_notification(); |
|
| 117 | + $that->show_notification(); |
|
| 118 | 118 | |
| 119 | - } ); |
|
| 120 | - } |
|
| 119 | + } ); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - public function notification_close_handler() { |
|
| 123 | - if ( ! isset( $_GET['wl_key_validation_notice'] ) |
|
| 124 | - || ! isset( $_GET['_wl_key_validation_notice_nonce'] ) ) { |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - $nonce = sanitize_text_field( wp_unslash( (string) $_GET['_wl_key_validation_notice_nonce'] ) ); |
|
| 128 | - if ( wp_verify_nonce( $nonce, self::KEY_VALIDATION_NONCE_ACTION ) |
|
| 129 | - && current_user_can( 'manage_options' ) ) { |
|
| 130 | - // close the notification. |
|
| 131 | - update_option( self::NOTIFICATION_OPTION_KEY, true ); |
|
| 132 | - } |
|
| 133 | - } |
|
| 122 | + public function notification_close_handler() { |
|
| 123 | + if ( ! isset( $_GET['wl_key_validation_notice'] ) |
|
| 124 | + || ! isset( $_GET['_wl_key_validation_notice_nonce'] ) ) { |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + $nonce = sanitize_text_field( wp_unslash( (string) $_GET['_wl_key_validation_notice_nonce'] ) ); |
|
| 128 | + if ( wp_verify_nonce( $nonce, self::KEY_VALIDATION_NONCE_ACTION ) |
|
| 129 | + && current_user_can( 'manage_options' ) ) { |
|
| 130 | + // close the notification. |
|
| 131 | + update_option( self::NOTIFICATION_OPTION_KEY, true ); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - public static function remove_notification_flag() { |
|
| 136 | - delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
| 137 | - } |
|
| 135 | + public static function remove_notification_flag() { |
|
| 136 | + delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
@@ -42,30 +42,30 @@ discard block |
||
| 42 | 42 | * @param \Wordlift_Key_Validation_Service $key_validation_service |
| 43 | 43 | * @param Wordlift_Configuration_Service $configuration_service |
| 44 | 44 | */ |
| 45 | - public function __construct( $key_validation_service, $configuration_service ) { |
|
| 45 | + public function __construct($key_validation_service, $configuration_service) { |
|
| 46 | 46 | |
| 47 | 47 | $this->key_validation_service = $key_validation_service; |
| 48 | 48 | $this->configuration_service = $configuration_service; |
| 49 | 49 | |
| 50 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification', 60 * 60 * 8 ); |
|
| 50 | + $this->ttl_cache_service = new Ttl_Cache('key-validation-notification', 60 * 60 * 8); |
|
| 51 | 51 | |
| 52 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 52 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
| 53 | 53 | $this->display_key_validation_notice(); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | $that = $this; |
| 57 | - add_action( 'plugins_loaded', function () use ( $that ) { |
|
| 57 | + add_action('plugins_loaded', function() use ($that) { |
|
| 58 | 58 | $that->notification_close_handler(); |
| 59 | 59 | } ); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | |
| 63 | 63 | public function show_notification() { |
| 64 | - $settings_url = admin_url( 'admin.php?page=wl_configuration_admin_menu' ); |
|
| 64 | + $settings_url = admin_url('admin.php?page=wl_configuration_admin_menu'); |
|
| 65 | 65 | ?> |
| 66 | 66 | <div class="wl-notice notice is-dismissible error"> |
| 67 | 67 | <p> |
| 68 | - <?php echo wp_kses( __( "Your WordLift key is not valid, please update the key in <a href='$settings_url'>WordLift Settings</a> or contact our support at [email protected].", 'wordlift' ), array( 'a' => array( 'href' ) ) ); ?> |
|
| 68 | + <?php echo wp_kses(__("Your WordLift key is not valid, please update the key in <a href='$settings_url'>WordLift Settings</a> or contact our support at [email protected].", 'wordlift'), array('a' => array('href'))); ?> |
|
| 69 | 69 | </p> |
| 70 | 70 | <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span> |
| 71 | 71 | </button> |
@@ -79,38 +79,38 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | // Check cache if the result is present, if not get the results |
| 81 | 81 | // save it and return the data. |
| 82 | - if ( $this->ttl_cache_service->get( self::CACHE_KEY ) !== null ) { |
|
| 83 | - return $this->ttl_cache_service->get( self::CACHE_KEY ); |
|
| 82 | + if ($this->ttl_cache_service->get(self::CACHE_KEY) !== null) { |
|
| 83 | + return $this->ttl_cache_service->get(self::CACHE_KEY); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - $is_valid = $this->key_validation_service->is_key_valid( $key ); |
|
| 86 | + $is_valid = $this->key_validation_service->is_key_valid($key); |
|
| 87 | 87 | |
| 88 | 88 | // when the cache is set, clear the notification flag. |
| 89 | - delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
| 89 | + delete_option(self::NOTIFICATION_OPTION_KEY); |
|
| 90 | 90 | |
| 91 | - $this->ttl_cache_service->put( self::CACHE_KEY, $is_valid ); |
|
| 91 | + $this->ttl_cache_service->put(self::CACHE_KEY, $is_valid); |
|
| 92 | 92 | |
| 93 | 93 | return $is_valid; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | private function display_key_validation_notice() { |
| 97 | 97 | $that = $this; |
| 98 | - add_action( 'admin_notices', function () use ( $that ) { |
|
| 98 | + add_action('admin_notices', function() use ($that) { |
|
| 99 | 99 | |
| 100 | - $is_notification_shown = get_option( Key_Validation_Notice::NOTIFICATION_OPTION_KEY, false ); |
|
| 100 | + $is_notification_shown = get_option(Key_Validation_Notice::NOTIFICATION_OPTION_KEY, false); |
|
| 101 | 101 | |
| 102 | 102 | $key = $that->configuration_service->get_key(); |
| 103 | 103 | |
| 104 | - if ( ! $key ) { |
|
| 104 | + if ( ! $key) { |
|
| 105 | 105 | // Dont show warning or make API call, return early. |
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if ( $that->is_key_valid() ) { |
|
| 109 | + if ($that->is_key_valid()) { |
|
| 110 | 110 | return; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - if ( $is_notification_shown ) { |
|
| 113 | + if ($is_notification_shown) { |
|
| 114 | 114 | return; |
| 115 | 115 | } |
| 116 | 116 | |
@@ -120,20 +120,20 @@ discard block |
||
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | public function notification_close_handler() { |
| 123 | - if ( ! isset( $_GET['wl_key_validation_notice'] ) |
|
| 124 | - || ! isset( $_GET['_wl_key_validation_notice_nonce'] ) ) { |
|
| 123 | + if ( ! isset($_GET['wl_key_validation_notice']) |
|
| 124 | + || ! isset($_GET['_wl_key_validation_notice_nonce'])) { |
|
| 125 | 125 | return false; |
| 126 | 126 | } |
| 127 | - $nonce = sanitize_text_field( wp_unslash( (string) $_GET['_wl_key_validation_notice_nonce'] ) ); |
|
| 128 | - if ( wp_verify_nonce( $nonce, self::KEY_VALIDATION_NONCE_ACTION ) |
|
| 129 | - && current_user_can( 'manage_options' ) ) { |
|
| 127 | + $nonce = sanitize_text_field(wp_unslash((string) $_GET['_wl_key_validation_notice_nonce'])); |
|
| 128 | + if (wp_verify_nonce($nonce, self::KEY_VALIDATION_NONCE_ACTION) |
|
| 129 | + && current_user_can('manage_options')) { |
|
| 130 | 130 | // close the notification. |
| 131 | - update_option( self::NOTIFICATION_OPTION_KEY, true ); |
|
| 131 | + update_option(self::NOTIFICATION_OPTION_KEY, true); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | public static function remove_notification_flag() { |
| 136 | - delete_option( self::NOTIFICATION_OPTION_KEY ); |
|
| 136 | + delete_option(self::NOTIFICATION_OPTION_KEY); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | } |
@@ -7,55 +7,55 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Admin_User_Option { |
| 9 | 9 | |
| 10 | - const WORDLIFT_ADMIN = 'wl_is_wordlift_admin'; |
|
| 11 | - |
|
| 12 | - public static function is_wordlift_admin() { |
|
| 13 | - return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
| 14 | - } |
|
| 15 | - |
|
| 16 | - public function connect_hook() { |
|
| 17 | - add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) ); |
|
| 18 | - add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) ); |
|
| 19 | - add_action( 'personal_options_update', array( $this, 'save_checkbox' ) ); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function save_checkbox() { |
|
| 23 | - |
|
| 24 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 25 | - return; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) { |
|
| 29 | - delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN ); |
|
| 30 | - |
|
| 31 | - return; |
|
| 32 | - } |
|
| 33 | - update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 ); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - public function render_checkbox() { |
|
| 37 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 38 | - return; |
|
| 39 | - } |
|
| 40 | - $is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
| 41 | - $allowed_html = array( |
|
| 42 | - 'tr' => array(), |
|
| 43 | - 'th' => array(), |
|
| 44 | - 'td' => array(), |
|
| 45 | - 'input' => array( |
|
| 46 | - 'type' => array(), |
|
| 47 | - 'name' => array(), |
|
| 48 | - 'checked' => array(), |
|
| 49 | - ) |
|
| 50 | - ); |
|
| 51 | - echo wp_kses( $this->get_checkbox( $is_checked ), $allowed_html ); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public static function get_checkbox( $is_checked ) { |
|
| 55 | - $checked = $is_checked ? "checked='checked'" : ''; |
|
| 56 | - $admin_text = __( 'Wordlift Admin', 'wordlift' ); |
|
| 57 | - |
|
| 58 | - return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>"; |
|
| 59 | - } |
|
| 10 | + const WORDLIFT_ADMIN = 'wl_is_wordlift_admin'; |
|
| 11 | + |
|
| 12 | + public static function is_wordlift_admin() { |
|
| 13 | + return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
| 14 | + } |
|
| 15 | + |
|
| 16 | + public function connect_hook() { |
|
| 17 | + add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) ); |
|
| 18 | + add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) ); |
|
| 19 | + add_action( 'personal_options_update', array( $this, 'save_checkbox' ) ); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function save_checkbox() { |
|
| 23 | + |
|
| 24 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 25 | + return; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) { |
|
| 29 | + delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN ); |
|
| 30 | + |
|
| 31 | + return; |
|
| 32 | + } |
|
| 33 | + update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 ); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + public function render_checkbox() { |
|
| 37 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 38 | + return; |
|
| 39 | + } |
|
| 40 | + $is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
| 41 | + $allowed_html = array( |
|
| 42 | + 'tr' => array(), |
|
| 43 | + 'th' => array(), |
|
| 44 | + 'td' => array(), |
|
| 45 | + 'input' => array( |
|
| 46 | + 'type' => array(), |
|
| 47 | + 'name' => array(), |
|
| 48 | + 'checked' => array(), |
|
| 49 | + ) |
|
| 50 | + ); |
|
| 51 | + echo wp_kses( $this->get_checkbox( $is_checked ), $allowed_html ); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public static function get_checkbox( $is_checked ) { |
|
| 55 | + $checked = $is_checked ? "checked='checked'" : ''; |
|
| 56 | + $admin_text = __( 'Wordlift Admin', 'wordlift' ); |
|
| 57 | + |
|
| 58 | + return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>"; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | } |
| 62 | 62 | \ No newline at end of file |
@@ -10,34 +10,34 @@ discard block |
||
| 10 | 10 | const WORDLIFT_ADMIN = 'wl_is_wordlift_admin'; |
| 11 | 11 | |
| 12 | 12 | public static function is_wordlift_admin() { |
| 13 | - return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
| 13 | + return intval(get_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN, true)) === 1; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function connect_hook() { |
| 17 | - add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) ); |
|
| 18 | - add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) ); |
|
| 19 | - add_action( 'personal_options_update', array( $this, 'save_checkbox' ) ); |
|
| 17 | + add_action('wordlift_user_settings_page', array($this, 'render_checkbox')); |
|
| 18 | + add_action('edit_user_profile_update', array($this, 'save_checkbox')); |
|
| 19 | + add_action('personal_options_update', array($this, 'save_checkbox')); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function save_checkbox() { |
| 23 | 23 | |
| 24 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 24 | + if ( ! current_user_can('manage_options')) { |
|
| 25 | 25 | return; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) { |
|
| 29 | - delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN ); |
|
| 28 | + if ( ! isset($_POST[self::WORDLIFT_ADMIN])) { |
|
| 29 | + delete_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN); |
|
| 30 | 30 | |
| 31 | 31 | return; |
| 32 | 32 | } |
| 33 | - update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 ); |
|
| 33 | + update_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN, 1); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | public function render_checkbox() { |
| 37 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 37 | + if ( ! current_user_can('manage_options')) { |
|
| 38 | 38 | return; |
| 39 | 39 | } |
| 40 | - $is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
| 40 | + $is_checked = intval(get_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN, true)) === 1; |
|
| 41 | 41 | $allowed_html = array( |
| 42 | 42 | 'tr' => array(), |
| 43 | 43 | 'th' => array(), |
@@ -48,12 +48,12 @@ discard block |
||
| 48 | 48 | 'checked' => array(), |
| 49 | 49 | ) |
| 50 | 50 | ); |
| 51 | - echo wp_kses( $this->get_checkbox( $is_checked ), $allowed_html ); |
|
| 51 | + echo wp_kses($this->get_checkbox($is_checked), $allowed_html); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - public static function get_checkbox( $is_checked ) { |
|
| 54 | + public static function get_checkbox($is_checked) { |
|
| 55 | 55 | $checked = $is_checked ? "checked='checked'" : ''; |
| 56 | - $admin_text = __( 'Wordlift Admin', 'wordlift' ); |
|
| 56 | + $admin_text = __('Wordlift Admin', 'wordlift'); |
|
| 57 | 57 | |
| 58 | 58 | return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>"; |
| 59 | 59 | } |
@@ -18,367 +18,367 @@ |
||
| 18 | 18 | * @subpackage Wordlift/admin/WL_Metabox |
| 19 | 19 | */ |
| 20 | 20 | class Wl_Abstract_Metabox { |
| 21 | - /** |
|
| 22 | - * The metabox custom fields for the current {@link WP_Post}. |
|
| 23 | - * |
|
| 24 | - * @since 3.1.0 |
|
| 25 | - * @access public |
|
| 26 | - * @var array $fields The metabox custom fields. |
|
| 27 | - */ |
|
| 28 | - public $fields; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Log_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.4 |
|
| 34 | - * |
|
| 35 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $log; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * WL_Metabox constructor. |
|
| 41 | - * |
|
| 42 | - * @since 3.1.0 |
|
| 43 | - */ |
|
| 44 | - public function __construct() { |
|
| 45 | - |
|
| 46 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Add a callback to print the metabox in page. |
|
| 51 | - * Wordpress will fire the $this->html() callback at the right time. |
|
| 52 | - */ |
|
| 53 | - public function add_main_metabox() { |
|
| 54 | - |
|
| 55 | - // Build the fields we need to print. |
|
| 56 | - $this->instantiate_fields( get_the_ID(), Object_Type_Enum::POST ); |
|
| 57 | - |
|
| 58 | - // Bailout if there are no actual fields, we do not need a metabox in that case. |
|
| 59 | - if ( empty( $this->fields ) ) { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Add main metabox (will print also the inner fields). |
|
| 64 | - $id = uniqid( 'wl-metabox-' ); |
|
| 65 | - $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 66 | - |
|
| 67 | - // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit. |
|
| 68 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) { |
|
| 69 | - add_meta_box( $id, esc_html( $title ), array( |
|
| 70 | - $this, |
|
| 71 | - 'html', |
|
| 72 | - ), $screen, 'normal', 'high' ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Add filter to change the metabox CSS class. |
|
| 76 | - // |
|
| 77 | - // @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen` |
|
| 78 | - // dynamically according to the `get_current_screen()` function. |
|
| 79 | - $current_screen = get_current_screen(); |
|
| 80 | - $screen = $current_screen ? $current_screen->post_type : 'entity'; |
|
| 81 | - add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 82 | - |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Render the metabox html. |
|
| 87 | - * |
|
| 88 | - * @since 3.1.0 |
|
| 89 | - * |
|
| 90 | - */ |
|
| 91 | - public function html() { |
|
| 92 | - |
|
| 93 | - // Loop over the fields. |
|
| 94 | - foreach ( $this->fields as $field ) { |
|
| 95 | - |
|
| 96 | - // load data from DB (values will be available in $field->data). |
|
| 97 | - $field->get_data(); |
|
| 98 | - |
|
| 99 | - // print field HTML (nonce included). |
|
| 100 | - echo $field->html(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Read the WL <-> Schema mapping and build the Fields for the entity being edited. |
|
| 107 | - * |
|
| 108 | - * Note: the first function that calls this method will instantiate the fields. |
|
| 109 | - * Why it isn't called from the constructor? Because we need to hook this process as late as possible. |
|
| 110 | - * |
|
| 111 | - * @param int $id | $term_id The post id or term id. |
|
| 112 | - * |
|
| 113 | - * @param $type int Post or Term |
|
| 114 | - * |
|
| 115 | - * @since 3.1.0 |
|
| 116 | - */ |
|
| 117 | - public function instantiate_fields( $id, $type ) { |
|
| 118 | - |
|
| 119 | - $this->log->trace( "Instantiating fields for entity post $id..." ); |
|
| 120 | - |
|
| 121 | - // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
|
| 122 | - if ( isset( $this->fields ) ) { |
|
| 123 | - return; |
|
| 124 | - } |
|
| 125 | - if ( $type === Object_Type_Enum::POST ) { |
|
| 126 | - $entity_type = wl_entity_taxonomy_get_custom_fields( $id ); |
|
| 127 | - } else if ( $type === Object_Type_Enum::TERM ) { |
|
| 128 | - $term_entity_types = get_term_meta( $id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 129 | - $term_entity_types = array_map( function ( $term ) { |
|
| 130 | - return get_term_by( |
|
| 131 | - 'slug', |
|
| 132 | - $term, |
|
| 133 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME |
|
| 134 | - ); |
|
| 135 | - }, $term_entity_types ); |
|
| 136 | - $entity_type = wl_get_custom_fields_by_entity_type( $term_entity_types ); |
|
| 137 | - } |
|
| 138 | - if ( isset( $entity_type ) ) { |
|
| 139 | - |
|
| 140 | - /* |
|
| 21 | + /** |
|
| 22 | + * The metabox custom fields for the current {@link WP_Post}. |
|
| 23 | + * |
|
| 24 | + * @since 3.1.0 |
|
| 25 | + * @access public |
|
| 26 | + * @var array $fields The metabox custom fields. |
|
| 27 | + */ |
|
| 28 | + public $fields; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Log_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.4 |
|
| 34 | + * |
|
| 35 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $log; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * WL_Metabox constructor. |
|
| 41 | + * |
|
| 42 | + * @since 3.1.0 |
|
| 43 | + */ |
|
| 44 | + public function __construct() { |
|
| 45 | + |
|
| 46 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Add a callback to print the metabox in page. |
|
| 51 | + * Wordpress will fire the $this->html() callback at the right time. |
|
| 52 | + */ |
|
| 53 | + public function add_main_metabox() { |
|
| 54 | + |
|
| 55 | + // Build the fields we need to print. |
|
| 56 | + $this->instantiate_fields( get_the_ID(), Object_Type_Enum::POST ); |
|
| 57 | + |
|
| 58 | + // Bailout if there are no actual fields, we do not need a metabox in that case. |
|
| 59 | + if ( empty( $this->fields ) ) { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Add main metabox (will print also the inner fields). |
|
| 64 | + $id = uniqid( 'wl-metabox-' ); |
|
| 65 | + $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 66 | + |
|
| 67 | + // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit. |
|
| 68 | + foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) { |
|
| 69 | + add_meta_box( $id, esc_html( $title ), array( |
|
| 70 | + $this, |
|
| 71 | + 'html', |
|
| 72 | + ), $screen, 'normal', 'high' ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Add filter to change the metabox CSS class. |
|
| 76 | + // |
|
| 77 | + // @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen` |
|
| 78 | + // dynamically according to the `get_current_screen()` function. |
|
| 79 | + $current_screen = get_current_screen(); |
|
| 80 | + $screen = $current_screen ? $current_screen->post_type : 'entity'; |
|
| 81 | + add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 82 | + |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Render the metabox html. |
|
| 87 | + * |
|
| 88 | + * @since 3.1.0 |
|
| 89 | + * |
|
| 90 | + */ |
|
| 91 | + public function html() { |
|
| 92 | + |
|
| 93 | + // Loop over the fields. |
|
| 94 | + foreach ( $this->fields as $field ) { |
|
| 95 | + |
|
| 96 | + // load data from DB (values will be available in $field->data). |
|
| 97 | + $field->get_data(); |
|
| 98 | + |
|
| 99 | + // print field HTML (nonce included). |
|
| 100 | + echo $field->html(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Read the WL <-> Schema mapping and build the Fields for the entity being edited. |
|
| 107 | + * |
|
| 108 | + * Note: the first function that calls this method will instantiate the fields. |
|
| 109 | + * Why it isn't called from the constructor? Because we need to hook this process as late as possible. |
|
| 110 | + * |
|
| 111 | + * @param int $id | $term_id The post id or term id. |
|
| 112 | + * |
|
| 113 | + * @param $type int Post or Term |
|
| 114 | + * |
|
| 115 | + * @since 3.1.0 |
|
| 116 | + */ |
|
| 117 | + public function instantiate_fields( $id, $type ) { |
|
| 118 | + |
|
| 119 | + $this->log->trace( "Instantiating fields for entity post $id..." ); |
|
| 120 | + |
|
| 121 | + // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
|
| 122 | + if ( isset( $this->fields ) ) { |
|
| 123 | + return; |
|
| 124 | + } |
|
| 125 | + if ( $type === Object_Type_Enum::POST ) { |
|
| 126 | + $entity_type = wl_entity_taxonomy_get_custom_fields( $id ); |
|
| 127 | + } else if ( $type === Object_Type_Enum::TERM ) { |
|
| 128 | + $term_entity_types = get_term_meta( $id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 129 | + $term_entity_types = array_map( function ( $term ) { |
|
| 130 | + return get_term_by( |
|
| 131 | + 'slug', |
|
| 132 | + $term, |
|
| 133 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME |
|
| 134 | + ); |
|
| 135 | + }, $term_entity_types ); |
|
| 136 | + $entity_type = wl_get_custom_fields_by_entity_type( $term_entity_types ); |
|
| 137 | + } |
|
| 138 | + if ( isset( $entity_type ) ) { |
|
| 139 | + |
|
| 140 | + /* |
|
| 141 | 141 | * Might not have any relevant meta box field, for example for articles, |
| 142 | 142 | * therefor make sure fields are at least an empty array to help the considered |
| 143 | 143 | * in other functions using it. |
| 144 | 144 | */ |
| 145 | - $this->fields = array(); |
|
| 145 | + $this->fields = array(); |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods. |
|
| 149 | - * We must divide fields in two groups: |
|
| 150 | - * - simple: accept values for one property |
|
| 151 | - * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
|
| 152 | - */ |
|
| 153 | - $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 154 | - $simple_metaboxes = $metaboxes[0]; |
|
| 155 | - $grouped_metaboxes = $metaboxes[1]; |
|
| 156 | - |
|
| 157 | - // Loop over simple entity properties. |
|
| 158 | - foreach ( $simple_metaboxes as $key => $property ) { |
|
| 159 | - |
|
| 160 | - // Info passed to the metabox. |
|
| 161 | - $info = array(); |
|
| 162 | - $info[ $key ] = $property; |
|
| 163 | - |
|
| 164 | - // Build the requested field as WL_Metabox_Field_ object. |
|
| 165 | - $this->add_field( $info, false, $type, $id ); |
|
| 166 | - |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // Loop over grouped properties. |
|
| 170 | - foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 147 | + /** |
|
| 148 | + * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods. |
|
| 149 | + * We must divide fields in two groups: |
|
| 150 | + * - simple: accept values for one property |
|
| 151 | + * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
|
| 152 | + */ |
|
| 153 | + $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 154 | + $simple_metaboxes = $metaboxes[0]; |
|
| 155 | + $grouped_metaboxes = $metaboxes[1]; |
|
| 171 | 156 | |
| 172 | - // Info passed to the metabox. |
|
| 173 | - $info = array(); |
|
| 174 | - $info[ $key ] = $property; |
|
| 157 | + // Loop over simple entity properties. |
|
| 158 | + foreach ( $simple_metaboxes as $key => $property ) { |
|
| 175 | 159 | |
| 176 | - // Build the requested field group as WL_Metabox_Field_ object. |
|
| 177 | - $this->add_field( $info, true, $type, $id ); |
|
| 160 | + // Info passed to the metabox. |
|
| 161 | + $info = array(); |
|
| 162 | + $info[ $key ] = $property; |
|
| 178 | 163 | |
| 179 | - } |
|
| 180 | - } |
|
| 164 | + // Build the requested field as WL_Metabox_Field_ object. |
|
| 165 | + $this->add_field( $info, false, $type, $id ); |
|
| 181 | 166 | |
| 182 | - } |
|
| 167 | + } |
|
| 183 | 168 | |
| 184 | - /** |
|
| 185 | - * Separates metaboxes in simple and grouped. |
|
| 186 | - * |
|
| 187 | - * @param array $custom_fields Information on the entity type. |
|
| 188 | - * |
|
| 189 | - * @return array |
|
| 190 | - */ |
|
| 191 | - public function group_properties_by_input_field( $custom_fields ) { |
|
| 169 | + // Loop over grouped properties. |
|
| 170 | + foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 192 | 171 | |
| 193 | - $simple_properties = array(); |
|
| 194 | - $grouped_properties = array(); |
|
| 172 | + // Info passed to the metabox. |
|
| 173 | + $info = array(); |
|
| 174 | + $info[ $key ] = $property; |
|
| 195 | 175 | |
| 196 | - // Loop over possible entity properties. |
|
| 197 | - foreach ( $custom_fields as $key => $property ) { |
|
| 198 | - |
|
| 199 | - // Check presence of predicate and type. |
|
| 200 | - if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 201 | - |
|
| 202 | - // Check if input_field is defined. |
|
| 203 | - if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 204 | - |
|
| 205 | - $grouped_key = $property['input_field']; |
|
| 206 | - |
|
| 207 | - // Update list of grouped properties. |
|
| 208 | - $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 209 | - |
|
| 210 | - } else { |
|
| 211 | - |
|
| 212 | - // input_field not defined, add simple metabox. |
|
| 213 | - $simple_properties[ $key ] = $property; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return array( $simple_properties, $grouped_properties ); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Add a Field to the current Metabox, based on the description of the Field. |
|
| 223 | - * This method is a rude factory for Field objects. |
|
| 224 | - * |
|
| 225 | - * @param array $args The field's information. |
|
| 226 | - * @param bool $grouped Flag to distinguish between simple and grouped fields. |
|
| 227 | - * @param int $type Post or Term, based on the correct decorator would be selected. |
|
| 228 | - * @param int $id Identifier for the type. |
|
| 229 | - */ |
|
| 230 | - public function add_field( $args, $grouped, $type, $id ) { |
|
| 176 | + // Build the requested field group as WL_Metabox_Field_ object. |
|
| 177 | + $this->add_field( $info, true, $type, $id ); |
|
| 231 | 178 | |
| 232 | - if ( $grouped ) { |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + } |
|
| 233 | 183 | |
| 234 | - // Special fields (sameas, coordinates, etc.). |
|
| 235 | - // |
|
| 236 | - // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
|
| 237 | - $field_class = 'Wl_Metabox_Field_' . key( $args ); |
|
| 184 | + /** |
|
| 185 | + * Separates metaboxes in simple and grouped. |
|
| 186 | + * |
|
| 187 | + * @param array $custom_fields Information on the entity type. |
|
| 188 | + * |
|
| 189 | + * @return array |
|
| 190 | + */ |
|
| 191 | + public function group_properties_by_input_field( $custom_fields ) { |
|
| 192 | + |
|
| 193 | + $simple_properties = array(); |
|
| 194 | + $grouped_properties = array(); |
|
| 195 | + |
|
| 196 | + // Loop over possible entity properties. |
|
| 197 | + foreach ( $custom_fields as $key => $property ) { |
|
| 198 | + |
|
| 199 | + // Check presence of predicate and type. |
|
| 200 | + if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 201 | + |
|
| 202 | + // Check if input_field is defined. |
|
| 203 | + if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 204 | + |
|
| 205 | + $grouped_key = $property['input_field']; |
|
| 206 | + |
|
| 207 | + // Update list of grouped properties. |
|
| 208 | + $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 209 | + |
|
| 210 | + } else { |
|
| 211 | + |
|
| 212 | + // input_field not defined, add simple metabox. |
|
| 213 | + $simple_properties[ $key ] = $property; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return array( $simple_properties, $grouped_properties ); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Add a Field to the current Metabox, based on the description of the Field. |
|
| 223 | + * This method is a rude factory for Field objects. |
|
| 224 | + * |
|
| 225 | + * @param array $args The field's information. |
|
| 226 | + * @param bool $grouped Flag to distinguish between simple and grouped fields. |
|
| 227 | + * @param int $type Post or Term, based on the correct decorator would be selected. |
|
| 228 | + * @param int $id Identifier for the type. |
|
| 229 | + */ |
|
| 230 | + public function add_field( $args, $grouped, $type, $id ) { |
|
| 238 | 231 | |
| 239 | - } else { |
|
| 240 | - |
|
| 241 | - // Simple fields (string, uri, boolean, etc.). |
|
| 242 | - // |
|
| 243 | - // Which field? We want to use the class that is specific for the field. |
|
| 244 | - $meta = key( $args ); |
|
| 245 | - $this_meta = $args[ $meta ]; |
|
| 246 | - |
|
| 247 | - // If the field declares what metabox it wants, use that one. |
|
| 248 | - if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 249 | - |
|
| 250 | - $field_class = $this_meta['metabox']['class']; |
|
| 251 | - |
|
| 252 | - } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 253 | - |
|
| 254 | - // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 255 | - // When they will remove this. |
|
| 256 | - // |
|
| 257 | - // Use default Wl_Metabox_Field (manages strings). |
|
| 258 | - $field_class = 'Wl_Metabox_Field'; |
|
| 259 | - |
|
| 260 | - } else { |
|
| 261 | - |
|
| 262 | - // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 263 | - // When they will remove this. |
|
| 264 | - // |
|
| 265 | - // Build Field with a custom class (e.g. Wl_Metabox_Field_date). |
|
| 266 | - $field_class = 'Wl_Metabox_Field_' . $this_meta['type']; |
|
| 267 | - |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - } |
|
| 271 | - /** |
|
| 272 | - * @since 3.31.6 |
|
| 273 | - * Add namespace to initialize class. |
|
| 274 | - */ |
|
| 275 | - /** |
|
| 276 | - * @since 3.31.6 |
|
| 277 | - * Add namespace to initialize class. |
|
| 278 | - */ |
|
| 279 | - if ( substr( $field_class, 0, 1 ) !== '\\' ) { |
|
| 280 | - $field_class = 'Wordlift\Metabox\Field\\' . $field_class; |
|
| 281 | - // End if(). |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - if ( class_exists( $field_class ) ) { |
|
| 285 | - // Get decorator and use it as wrapper for save_data and get_data methods. |
|
| 286 | - $instance = new $field_class( $args, $id, $type ); |
|
| 287 | - // Call apropriate constructor (e.g. Wl_Metabox_Field... ). |
|
| 288 | - $this->fields[] = $instance; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Save the form data for the specified entity {@link WP_Post}'s id. |
|
| 295 | - * |
|
| 296 | - * @param int $id The entity's {@link WP_Post}'s id. |
|
| 297 | - * |
|
| 298 | - * @param $type int Post or term |
|
| 299 | - * |
|
| 300 | - * @since 3.5.4 |
|
| 301 | - */ |
|
| 302 | - public function save_form_data( $id, $type ) { |
|
| 303 | - |
|
| 304 | - $this->log->trace( "Saving form data for entity post $id..." ); |
|
| 305 | - |
|
| 306 | - // Skip saving if the save is called for a different post. |
|
| 307 | - if ( isset( $_POST['post_ID'] ) && (int) $_POST['post_ID'] !== $id && $type === Object_Type_Enum::POST ) { |
|
| 308 | - $this->log->debug( "`wl_metaboxes`, skipping because the post id from request doesnt match the id from filter." ); |
|
| 309 | - return; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - |
|
| 313 | - // Build Field objects. |
|
| 314 | - $this->instantiate_fields( $id, $type ); |
|
| 315 | - |
|
| 316 | - // Check if WL metabox form was posted. |
|
| 317 | - if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 318 | - $this->log->debug( "`wl_metaboxes`, skipping..." ); |
|
| 319 | - |
|
| 320 | - return; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - $posted_data = $_POST['wl_metaboxes']; |
|
| 324 | - |
|
| 325 | - foreach ( $this->fields as $field ) { |
|
| 326 | - |
|
| 327 | - // Verify nonce. |
|
| 328 | - $valid_nonce = $field->verify_nonce(); |
|
| 329 | - |
|
| 330 | - if ( $valid_nonce ) { |
|
| 331 | - $field_name = $field->meta_name; |
|
| 332 | - // Each Filed only deals with its values. |
|
| 333 | - if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 334 | - |
|
| 335 | - $values = $posted_data[ $field_name ]; |
|
| 336 | - if ( ! is_array( $values ) ) { |
|
| 337 | - $values = array( $values ); |
|
| 338 | - } |
|
| 339 | - // Save data permanently |
|
| 340 | - $field->save_data( $values ); |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right |
|
| 347 | - * before the triples are pushed to the linked dataset. |
|
| 348 | - * |
|
| 349 | - * @param int $id The entity id. |
|
| 350 | - * @param int $id The post data. |
|
| 351 | - * |
|
| 352 | - * @since 3.18.2 |
|
| 353 | - * |
|
| 354 | - */ |
|
| 355 | - do_action( 'wl_save_form_pre_push_entity', $id, $_POST ); |
|
| 356 | - |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Enqueue scripts and styles. |
|
| 361 | - * |
|
| 362 | - * @since 3.0.0 |
|
| 363 | - */ |
|
| 364 | - public function enqueue_scripts_and_styles() { |
|
| 365 | - |
|
| 366 | - // Use the minified version if PW_DEBUG isn't set. |
|
| 367 | - $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 368 | - |
|
| 369 | - // Load the jquery-ui-timepicker-addon library. |
|
| 370 | - wp_enqueue_style( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 371 | - wp_enqueue_script( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 372 | - |
|
| 373 | - wl_enqueue_leaflet(); |
|
| 374 | - |
|
| 375 | - // Add AJAX autocomplete to facilitate metabox editing. |
|
| 376 | - wp_enqueue_script( 'wl-entity-metabox-utility', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/admin/js/wl_entity_metabox_utilities.js' ); |
|
| 377 | - wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 378 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 379 | - 'action' => 'entity_by_title', |
|
| 380 | - ) |
|
| 381 | - ); |
|
| 382 | - |
|
| 383 | - } |
|
| 232 | + if ( $grouped ) { |
|
| 233 | + |
|
| 234 | + // Special fields (sameas, coordinates, etc.). |
|
| 235 | + // |
|
| 236 | + // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
|
| 237 | + $field_class = 'Wl_Metabox_Field_' . key( $args ); |
|
| 238 | + |
|
| 239 | + } else { |
|
| 240 | + |
|
| 241 | + // Simple fields (string, uri, boolean, etc.). |
|
| 242 | + // |
|
| 243 | + // Which field? We want to use the class that is specific for the field. |
|
| 244 | + $meta = key( $args ); |
|
| 245 | + $this_meta = $args[ $meta ]; |
|
| 246 | + |
|
| 247 | + // If the field declares what metabox it wants, use that one. |
|
| 248 | + if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 249 | + |
|
| 250 | + $field_class = $this_meta['metabox']['class']; |
|
| 251 | + |
|
| 252 | + } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 253 | + |
|
| 254 | + // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 255 | + // When they will remove this. |
|
| 256 | + // |
|
| 257 | + // Use default Wl_Metabox_Field (manages strings). |
|
| 258 | + $field_class = 'Wl_Metabox_Field'; |
|
| 259 | + |
|
| 260 | + } else { |
|
| 261 | + |
|
| 262 | + // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 263 | + // When they will remove this. |
|
| 264 | + // |
|
| 265 | + // Build Field with a custom class (e.g. Wl_Metabox_Field_date). |
|
| 266 | + $field_class = 'Wl_Metabox_Field_' . $this_meta['type']; |
|
| 267 | + |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + } |
|
| 271 | + /** |
|
| 272 | + * @since 3.31.6 |
|
| 273 | + * Add namespace to initialize class. |
|
| 274 | + */ |
|
| 275 | + /** |
|
| 276 | + * @since 3.31.6 |
|
| 277 | + * Add namespace to initialize class. |
|
| 278 | + */ |
|
| 279 | + if ( substr( $field_class, 0, 1 ) !== '\\' ) { |
|
| 280 | + $field_class = 'Wordlift\Metabox\Field\\' . $field_class; |
|
| 281 | + // End if(). |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + if ( class_exists( $field_class ) ) { |
|
| 285 | + // Get decorator and use it as wrapper for save_data and get_data methods. |
|
| 286 | + $instance = new $field_class( $args, $id, $type ); |
|
| 287 | + // Call apropriate constructor (e.g. Wl_Metabox_Field... ). |
|
| 288 | + $this->fields[] = $instance; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Save the form data for the specified entity {@link WP_Post}'s id. |
|
| 295 | + * |
|
| 296 | + * @param int $id The entity's {@link WP_Post}'s id. |
|
| 297 | + * |
|
| 298 | + * @param $type int Post or term |
|
| 299 | + * |
|
| 300 | + * @since 3.5.4 |
|
| 301 | + */ |
|
| 302 | + public function save_form_data( $id, $type ) { |
|
| 303 | + |
|
| 304 | + $this->log->trace( "Saving form data for entity post $id..." ); |
|
| 305 | + |
|
| 306 | + // Skip saving if the save is called for a different post. |
|
| 307 | + if ( isset( $_POST['post_ID'] ) && (int) $_POST['post_ID'] !== $id && $type === Object_Type_Enum::POST ) { |
|
| 308 | + $this->log->debug( "`wl_metaboxes`, skipping because the post id from request doesnt match the id from filter." ); |
|
| 309 | + return; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + |
|
| 313 | + // Build Field objects. |
|
| 314 | + $this->instantiate_fields( $id, $type ); |
|
| 315 | + |
|
| 316 | + // Check if WL metabox form was posted. |
|
| 317 | + if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 318 | + $this->log->debug( "`wl_metaboxes`, skipping..." ); |
|
| 319 | + |
|
| 320 | + return; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + $posted_data = $_POST['wl_metaboxes']; |
|
| 324 | + |
|
| 325 | + foreach ( $this->fields as $field ) { |
|
| 326 | + |
|
| 327 | + // Verify nonce. |
|
| 328 | + $valid_nonce = $field->verify_nonce(); |
|
| 329 | + |
|
| 330 | + if ( $valid_nonce ) { |
|
| 331 | + $field_name = $field->meta_name; |
|
| 332 | + // Each Filed only deals with its values. |
|
| 333 | + if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 334 | + |
|
| 335 | + $values = $posted_data[ $field_name ]; |
|
| 336 | + if ( ! is_array( $values ) ) { |
|
| 337 | + $values = array( $values ); |
|
| 338 | + } |
|
| 339 | + // Save data permanently |
|
| 340 | + $field->save_data( $values ); |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right |
|
| 347 | + * before the triples are pushed to the linked dataset. |
|
| 348 | + * |
|
| 349 | + * @param int $id The entity id. |
|
| 350 | + * @param int $id The post data. |
|
| 351 | + * |
|
| 352 | + * @since 3.18.2 |
|
| 353 | + * |
|
| 354 | + */ |
|
| 355 | + do_action( 'wl_save_form_pre_push_entity', $id, $_POST ); |
|
| 356 | + |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Enqueue scripts and styles. |
|
| 361 | + * |
|
| 362 | + * @since 3.0.0 |
|
| 363 | + */ |
|
| 364 | + public function enqueue_scripts_and_styles() { |
|
| 365 | + |
|
| 366 | + // Use the minified version if PW_DEBUG isn't set. |
|
| 367 | + $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 368 | + |
|
| 369 | + // Load the jquery-ui-timepicker-addon library. |
|
| 370 | + wp_enqueue_style( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 371 | + wp_enqueue_script( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 372 | + |
|
| 373 | + wl_enqueue_leaflet(); |
|
| 374 | + |
|
| 375 | + // Add AJAX autocomplete to facilitate metabox editing. |
|
| 376 | + wp_enqueue_script( 'wl-entity-metabox-utility', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/admin/js/wl_entity_metabox_utilities.js' ); |
|
| 377 | + wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 378 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 379 | + 'action' => 'entity_by_title', |
|
| 380 | + ) |
|
| 381 | + ); |
|
| 382 | + |
|
| 383 | + } |
|
| 384 | 384 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function __construct() { |
| 45 | 45 | |
| 46 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 46 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
@@ -53,23 +53,23 @@ discard block |
||
| 53 | 53 | public function add_main_metabox() { |
| 54 | 54 | |
| 55 | 55 | // Build the fields we need to print. |
| 56 | - $this->instantiate_fields( get_the_ID(), Object_Type_Enum::POST ); |
|
| 56 | + $this->instantiate_fields(get_the_ID(), Object_Type_Enum::POST); |
|
| 57 | 57 | |
| 58 | 58 | // Bailout if there are no actual fields, we do not need a metabox in that case. |
| 59 | - if ( empty( $this->fields ) ) { |
|
| 59 | + if (empty($this->fields)) { |
|
| 60 | 60 | return; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // Add main metabox (will print also the inner fields). |
| 64 | - $id = uniqid( 'wl-metabox-' ); |
|
| 65 | - $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 64 | + $id = uniqid('wl-metabox-'); |
|
| 65 | + $title = get_the_title().' '.__('properties', 'wordlift'); |
|
| 66 | 66 | |
| 67 | 67 | // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit. |
| 68 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) { |
|
| 69 | - add_meta_box( $id, esc_html( $title ), array( |
|
| 68 | + foreach (Wordlift_Entity_Service::valid_entity_post_types() as $screen) { |
|
| 69 | + add_meta_box($id, esc_html($title), array( |
|
| 70 | 70 | $this, |
| 71 | 71 | 'html', |
| 72 | - ), $screen, 'normal', 'high' ); |
|
| 72 | + ), $screen, 'normal', 'high'); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Add filter to change the metabox CSS class. |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | // dynamically according to the `get_current_screen()` function. |
| 79 | 79 | $current_screen = get_current_screen(); |
| 80 | 80 | $screen = $current_screen ? $current_screen->post_type : 'entity'; |
| 81 | - add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 81 | + add_filter("postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class'); |
|
| 82 | 82 | |
| 83 | 83 | } |
| 84 | 84 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | public function html() { |
| 92 | 92 | |
| 93 | 93 | // Loop over the fields. |
| 94 | - foreach ( $this->fields as $field ) { |
|
| 94 | + foreach ($this->fields as $field) { |
|
| 95 | 95 | |
| 96 | 96 | // load data from DB (values will be available in $field->data). |
| 97 | 97 | $field->get_data(); |
@@ -114,28 +114,28 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @since 3.1.0 |
| 116 | 116 | */ |
| 117 | - public function instantiate_fields( $id, $type ) { |
|
| 117 | + public function instantiate_fields($id, $type) { |
|
| 118 | 118 | |
| 119 | - $this->log->trace( "Instantiating fields for entity post $id..." ); |
|
| 119 | + $this->log->trace("Instantiating fields for entity post $id..."); |
|
| 120 | 120 | |
| 121 | 121 | // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
| 122 | - if ( isset( $this->fields ) ) { |
|
| 122 | + if (isset($this->fields)) { |
|
| 123 | 123 | return; |
| 124 | 124 | } |
| 125 | - if ( $type === Object_Type_Enum::POST ) { |
|
| 126 | - $entity_type = wl_entity_taxonomy_get_custom_fields( $id ); |
|
| 127 | - } else if ( $type === Object_Type_Enum::TERM ) { |
|
| 128 | - $term_entity_types = get_term_meta( $id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 129 | - $term_entity_types = array_map( function ( $term ) { |
|
| 125 | + if ($type === Object_Type_Enum::POST) { |
|
| 126 | + $entity_type = wl_entity_taxonomy_get_custom_fields($id); |
|
| 127 | + } else if ($type === Object_Type_Enum::TERM) { |
|
| 128 | + $term_entity_types = get_term_meta($id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 129 | + $term_entity_types = array_map(function($term) { |
|
| 130 | 130 | return get_term_by( |
| 131 | 131 | 'slug', |
| 132 | 132 | $term, |
| 133 | 133 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME |
| 134 | 134 | ); |
| 135 | - }, $term_entity_types ); |
|
| 136 | - $entity_type = wl_get_custom_fields_by_entity_type( $term_entity_types ); |
|
| 135 | + }, $term_entity_types); |
|
| 136 | + $entity_type = wl_get_custom_fields_by_entity_type($term_entity_types); |
|
| 137 | 137 | } |
| 138 | - if ( isset( $entity_type ) ) { |
|
| 138 | + if (isset($entity_type)) { |
|
| 139 | 139 | |
| 140 | 140 | /* |
| 141 | 141 | * Might not have any relevant meta box field, for example for articles, |
@@ -150,31 +150,31 @@ discard block |
||
| 150 | 150 | * - simple: accept values for one property |
| 151 | 151 | * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
| 152 | 152 | */ |
| 153 | - $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 153 | + $metaboxes = $this->group_properties_by_input_field($entity_type); |
|
| 154 | 154 | $simple_metaboxes = $metaboxes[0]; |
| 155 | 155 | $grouped_metaboxes = $metaboxes[1]; |
| 156 | 156 | |
| 157 | 157 | // Loop over simple entity properties. |
| 158 | - foreach ( $simple_metaboxes as $key => $property ) { |
|
| 158 | + foreach ($simple_metaboxes as $key => $property) { |
|
| 159 | 159 | |
| 160 | 160 | // Info passed to the metabox. |
| 161 | 161 | $info = array(); |
| 162 | - $info[ $key ] = $property; |
|
| 162 | + $info[$key] = $property; |
|
| 163 | 163 | |
| 164 | 164 | // Build the requested field as WL_Metabox_Field_ object. |
| 165 | - $this->add_field( $info, false, $type, $id ); |
|
| 165 | + $this->add_field($info, false, $type, $id); |
|
| 166 | 166 | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | // Loop over grouped properties. |
| 170 | - foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 170 | + foreach ($grouped_metaboxes as $key => $property) { |
|
| 171 | 171 | |
| 172 | 172 | // Info passed to the metabox. |
| 173 | 173 | $info = array(); |
| 174 | - $info[ $key ] = $property; |
|
| 174 | + $info[$key] = $property; |
|
| 175 | 175 | |
| 176 | 176 | // Build the requested field group as WL_Metabox_Field_ object. |
| 177 | - $this->add_field( $info, true, $type, $id ); |
|
| 177 | + $this->add_field($info, true, $type, $id); |
|
| 178 | 178 | |
| 179 | 179 | } |
| 180 | 180 | } |
@@ -188,34 +188,34 @@ discard block |
||
| 188 | 188 | * |
| 189 | 189 | * @return array |
| 190 | 190 | */ |
| 191 | - public function group_properties_by_input_field( $custom_fields ) { |
|
| 191 | + public function group_properties_by_input_field($custom_fields) { |
|
| 192 | 192 | |
| 193 | 193 | $simple_properties = array(); |
| 194 | 194 | $grouped_properties = array(); |
| 195 | 195 | |
| 196 | 196 | // Loop over possible entity properties. |
| 197 | - foreach ( $custom_fields as $key => $property ) { |
|
| 197 | + foreach ($custom_fields as $key => $property) { |
|
| 198 | 198 | |
| 199 | 199 | // Check presence of predicate and type. |
| 200 | - if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 200 | + if (isset($property['predicate']) && isset($property['type'])) { |
|
| 201 | 201 | |
| 202 | 202 | // Check if input_field is defined. |
| 203 | - if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 203 | + if (isset($property['input_field']) && '' !== $property['input_field']) { |
|
| 204 | 204 | |
| 205 | 205 | $grouped_key = $property['input_field']; |
| 206 | 206 | |
| 207 | 207 | // Update list of grouped properties. |
| 208 | - $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 208 | + $grouped_properties[$grouped_key][$key] = $property; |
|
| 209 | 209 | |
| 210 | 210 | } else { |
| 211 | 211 | |
| 212 | 212 | // input_field not defined, add simple metabox. |
| 213 | - $simple_properties[ $key ] = $property; |
|
| 213 | + $simple_properties[$key] = $property; |
|
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - return array( $simple_properties, $grouped_properties ); |
|
| 218 | + return array($simple_properties, $grouped_properties); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -227,29 +227,29 @@ discard block |
||
| 227 | 227 | * @param int $type Post or Term, based on the correct decorator would be selected. |
| 228 | 228 | * @param int $id Identifier for the type. |
| 229 | 229 | */ |
| 230 | - public function add_field( $args, $grouped, $type, $id ) { |
|
| 230 | + public function add_field($args, $grouped, $type, $id) { |
|
| 231 | 231 | |
| 232 | - if ( $grouped ) { |
|
| 232 | + if ($grouped) { |
|
| 233 | 233 | |
| 234 | 234 | // Special fields (sameas, coordinates, etc.). |
| 235 | 235 | // |
| 236 | 236 | // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
| 237 | - $field_class = 'Wl_Metabox_Field_' . key( $args ); |
|
| 237 | + $field_class = 'Wl_Metabox_Field_'.key($args); |
|
| 238 | 238 | |
| 239 | 239 | } else { |
| 240 | 240 | |
| 241 | 241 | // Simple fields (string, uri, boolean, etc.). |
| 242 | 242 | // |
| 243 | 243 | // Which field? We want to use the class that is specific for the field. |
| 244 | - $meta = key( $args ); |
|
| 245 | - $this_meta = $args[ $meta ]; |
|
| 244 | + $meta = key($args); |
|
| 245 | + $this_meta = $args[$meta]; |
|
| 246 | 246 | |
| 247 | 247 | // If the field declares what metabox it wants, use that one. |
| 248 | - if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 248 | + if (isset($this_meta['metabox']['class'])) { |
|
| 249 | 249 | |
| 250 | 250 | $field_class = $this_meta['metabox']['class']; |
| 251 | 251 | |
| 252 | - } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 252 | + } elseif ( ! isset($this_meta['type']) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type']) { |
|
| 253 | 253 | |
| 254 | 254 | // TODO: all fields should explicitly declare the required WL_Metabox. |
| 255 | 255 | // When they will remove this. |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | // When they will remove this. |
| 264 | 264 | // |
| 265 | 265 | // Build Field with a custom class (e.g. Wl_Metabox_Field_date). |
| 266 | - $field_class = 'Wl_Metabox_Field_' . $this_meta['type']; |
|
| 266 | + $field_class = 'Wl_Metabox_Field_'.$this_meta['type']; |
|
| 267 | 267 | |
| 268 | 268 | } |
| 269 | 269 | |
@@ -276,14 +276,14 @@ discard block |
||
| 276 | 276 | * @since 3.31.6 |
| 277 | 277 | * Add namespace to initialize class. |
| 278 | 278 | */ |
| 279 | - if ( substr( $field_class, 0, 1 ) !== '\\' ) { |
|
| 280 | - $field_class = 'Wordlift\Metabox\Field\\' . $field_class; |
|
| 279 | + if (substr($field_class, 0, 1) !== '\\') { |
|
| 280 | + $field_class = 'Wordlift\Metabox\Field\\'.$field_class; |
|
| 281 | 281 | // End if(). |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | - if ( class_exists( $field_class ) ) { |
|
| 284 | + if (class_exists($field_class)) { |
|
| 285 | 285 | // Get decorator and use it as wrapper for save_data and get_data methods. |
| 286 | - $instance = new $field_class( $args, $id, $type ); |
|
| 286 | + $instance = new $field_class($args, $id, $type); |
|
| 287 | 287 | // Call apropriate constructor (e.g. Wl_Metabox_Field... ). |
| 288 | 288 | $this->fields[] = $instance; |
| 289 | 289 | } |
@@ -299,45 +299,45 @@ discard block |
||
| 299 | 299 | * |
| 300 | 300 | * @since 3.5.4 |
| 301 | 301 | */ |
| 302 | - public function save_form_data( $id, $type ) { |
|
| 302 | + public function save_form_data($id, $type) { |
|
| 303 | 303 | |
| 304 | - $this->log->trace( "Saving form data for entity post $id..." ); |
|
| 304 | + $this->log->trace("Saving form data for entity post $id..."); |
|
| 305 | 305 | |
| 306 | 306 | // Skip saving if the save is called for a different post. |
| 307 | - if ( isset( $_POST['post_ID'] ) && (int) $_POST['post_ID'] !== $id && $type === Object_Type_Enum::POST ) { |
|
| 308 | - $this->log->debug( "`wl_metaboxes`, skipping because the post id from request doesnt match the id from filter." ); |
|
| 307 | + if (isset($_POST['post_ID']) && (int) $_POST['post_ID'] !== $id && $type === Object_Type_Enum::POST) { |
|
| 308 | + $this->log->debug("`wl_metaboxes`, skipping because the post id from request doesnt match the id from filter."); |
|
| 309 | 309 | return; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | |
| 313 | 313 | // Build Field objects. |
| 314 | - $this->instantiate_fields( $id, $type ); |
|
| 314 | + $this->instantiate_fields($id, $type); |
|
| 315 | 315 | |
| 316 | 316 | // Check if WL metabox form was posted. |
| 317 | - if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 318 | - $this->log->debug( "`wl_metaboxes`, skipping..." ); |
|
| 317 | + if ( ! isset($_POST['wl_metaboxes'])) { |
|
| 318 | + $this->log->debug("`wl_metaboxes`, skipping..."); |
|
| 319 | 319 | |
| 320 | 320 | return; |
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | $posted_data = $_POST['wl_metaboxes']; |
| 324 | 324 | |
| 325 | - foreach ( $this->fields as $field ) { |
|
| 325 | + foreach ($this->fields as $field) { |
|
| 326 | 326 | |
| 327 | 327 | // Verify nonce. |
| 328 | 328 | $valid_nonce = $field->verify_nonce(); |
| 329 | 329 | |
| 330 | - if ( $valid_nonce ) { |
|
| 330 | + if ($valid_nonce) { |
|
| 331 | 331 | $field_name = $field->meta_name; |
| 332 | 332 | // Each Filed only deals with its values. |
| 333 | - if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 333 | + if (isset($posted_data[$field_name])) { |
|
| 334 | 334 | |
| 335 | - $values = $posted_data[ $field_name ]; |
|
| 336 | - if ( ! is_array( $values ) ) { |
|
| 337 | - $values = array( $values ); |
|
| 335 | + $values = $posted_data[$field_name]; |
|
| 336 | + if ( ! is_array($values)) { |
|
| 337 | + $values = array($values); |
|
| 338 | 338 | } |
| 339 | 339 | // Save data permanently |
| 340 | - $field->save_data( $values ); |
|
| 340 | + $field->save_data($values); |
|
| 341 | 341 | } |
| 342 | 342 | } |
| 343 | 343 | } |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * @since 3.18.2 |
| 353 | 353 | * |
| 354 | 354 | */ |
| 355 | - do_action( 'wl_save_form_pre_push_entity', $id, $_POST ); |
|
| 355 | + do_action('wl_save_form_pre_push_entity', $id, $_POST); |
|
| 356 | 356 | |
| 357 | 357 | } |
| 358 | 358 | |
@@ -364,18 +364,18 @@ discard block |
||
| 364 | 364 | public function enqueue_scripts_and_styles() { |
| 365 | 365 | |
| 366 | 366 | // Use the minified version if PW_DEBUG isn't set. |
| 367 | - $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 367 | + $min = ! defined('WP_DEBUG') || ! WP_DEBUG ? '.min' : ''; |
|
| 368 | 368 | |
| 369 | 369 | // Load the jquery-ui-timepicker-addon library. |
| 370 | - wp_enqueue_style( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 371 | - wp_enqueue_script( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 370 | + wp_enqueue_style('wl-flatpickr', dirname(dirname(plugin_dir_url(__FILE__)))."/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6'); |
|
| 371 | + wp_enqueue_script('wl-flatpickr', dirname(dirname(plugin_dir_url(__FILE__)))."/admin/js/flatpickr/flatpickr$min.js", array('jquery'), '3.0.6', true); |
|
| 372 | 372 | |
| 373 | 373 | wl_enqueue_leaflet(); |
| 374 | 374 | |
| 375 | 375 | // Add AJAX autocomplete to facilitate metabox editing. |
| 376 | - wp_enqueue_script( 'wl-entity-metabox-utility', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/admin/js/wl_entity_metabox_utilities.js' ); |
|
| 377 | - wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 378 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 376 | + wp_enqueue_script('wl-entity-metabox-utility', dirname(dirname(plugin_dir_url(__FILE__))).'/admin/js/wl_entity_metabox_utilities.js'); |
|
| 377 | + wp_localize_script('wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 378 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 379 | 379 | 'action' => 'entity_by_title', |
| 380 | 380 | ) |
| 381 | 381 | ); |
@@ -10,102 +10,102 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Recipe_Maker_Warning { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Recipe_Maker_Validation_Service |
|
| 15 | - */ |
|
| 16 | - private $recipe_maker_validation_service; |
|
| 17 | - |
|
| 18 | - public function __construct( $recipe_maker_validation_service ) { |
|
| 19 | - $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
|
| 20 | - /** |
|
| 21 | - * Filter: wl_feature__enable__notices. |
|
| 22 | - * |
|
| 23 | - * @param bool whether the notices needs to be enabled or not. |
|
| 24 | - * |
|
| 25 | - * @return bool |
|
| 26 | - * @since 3.27.6 |
|
| 27 | - */ |
|
| 28 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 29 | - add_action( 'admin_notices', array( $this, 'display_image_size_warning' ) ); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Show the warning after applying the conditions. |
|
| 35 | - */ |
|
| 36 | - public function display_image_size_warning() { |
|
| 37 | - |
|
| 38 | - // Check if we are on the post. |
|
| 39 | - if ( ! get_post() instanceof \WP_Post ) { |
|
| 40 | - return false; |
|
| 41 | - } |
|
| 42 | - if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available() ) { |
|
| 43 | - return false; |
|
| 44 | - } |
|
| 45 | - $post_id = get_the_ID(); |
|
| 46 | - |
|
| 47 | - // Dont show notification if there is no recipes referred by the post. |
|
| 48 | - if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $recipe_with_image_warnings = $this->get_warnings( $post_id ); |
|
| 53 | - |
|
| 54 | - if ( count( $recipe_with_image_warnings ) > 0 ) { |
|
| 55 | - // Show notification. |
|
| 56 | - ?> |
|
| 13 | + /** |
|
| 14 | + * @var Recipe_Maker_Validation_Service |
|
| 15 | + */ |
|
| 16 | + private $recipe_maker_validation_service; |
|
| 17 | + |
|
| 18 | + public function __construct( $recipe_maker_validation_service ) { |
|
| 19 | + $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
|
| 20 | + /** |
|
| 21 | + * Filter: wl_feature__enable__notices. |
|
| 22 | + * |
|
| 23 | + * @param bool whether the notices needs to be enabled or not. |
|
| 24 | + * |
|
| 25 | + * @return bool |
|
| 26 | + * @since 3.27.6 |
|
| 27 | + */ |
|
| 28 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 29 | + add_action( 'admin_notices', array( $this, 'display_image_size_warning' ) ); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Show the warning after applying the conditions. |
|
| 35 | + */ |
|
| 36 | + public function display_image_size_warning() { |
|
| 37 | + |
|
| 38 | + // Check if we are on the post. |
|
| 39 | + if ( ! get_post() instanceof \WP_Post ) { |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | + if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available() ) { |
|
| 43 | + return false; |
|
| 44 | + } |
|
| 45 | + $post_id = get_the_ID(); |
|
| 46 | + |
|
| 47 | + // Dont show notification if there is no recipes referred by the post. |
|
| 48 | + if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $recipe_with_image_warnings = $this->get_warnings( $post_id ); |
|
| 53 | + |
|
| 54 | + if ( count( $recipe_with_image_warnings ) > 0 ) { |
|
| 55 | + // Show notification. |
|
| 56 | + ?> |
|
| 57 | 57 | <div class="notice notice-warning is-dismissible"> |
| 58 | 58 | <p><?php echo wp_kses( __( 'The following recipes didnt have minimum image size of 1200 x 1200 px', 'wordlift' ), array() ); ?></p> |
| 59 | 59 | <ol> |
| 60 | 60 | <?php |
| 61 | - foreach ( $recipe_with_image_warnings as $post_id ) { |
|
| 62 | - echo "<li>" . esc_html( get_the_title( $post_id ) ) . "</li>"; |
|
| 63 | - } |
|
| 64 | - ?> |
|
| 61 | + foreach ( $recipe_with_image_warnings as $post_id ) { |
|
| 62 | + echo "<li>" . esc_html( get_the_title( $post_id ) ) . "</li>"; |
|
| 63 | + } |
|
| 64 | + ?> |
|
| 65 | 65 | </ol> |
| 66 | 66 | </div> |
| 67 | 67 | <?php |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param $post_id |
|
| 74 | - * |
|
| 75 | - * @return array |
|
| 76 | - */ |
|
| 77 | - private function get_warnings( $post_id ) { |
|
| 72 | + /** |
|
| 73 | + * @param $post_id |
|
| 74 | + * |
|
| 75 | + * @return array |
|
| 76 | + */ |
|
| 77 | + private function get_warnings( $post_id ) { |
|
| 78 | 78 | |
| 79 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 79 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 80 | 80 | |
| 81 | - // Dont show duplicate warnings. |
|
| 82 | - $recipe_ids = array_unique( $recipe_ids ); |
|
| 81 | + // Dont show duplicate warnings. |
|
| 82 | + $recipe_ids = array_unique( $recipe_ids ); |
|
| 83 | 83 | |
| 84 | - $recipe_with_image_warnings = array(); |
|
| 84 | + $recipe_with_image_warnings = array(); |
|
| 85 | 85 | |
| 86 | - foreach ( $recipe_ids as $recipe_id ) { |
|
| 87 | - $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
| 86 | + foreach ( $recipe_ids as $recipe_id ) { |
|
| 87 | + $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
| 88 | 88 | if ( ! $recipe ) { |
| 89 | 89 | continue; |
| 90 | 90 | } |
| 91 | - $image_id = $recipe->image_id(); |
|
| 92 | - $image_data = wp_get_attachment_image_src( $image_id, array( 1200, 1200 ) ); |
|
| 93 | - if ( ! is_array( $image_data ) ) { |
|
| 94 | - continue; |
|
| 95 | - } |
|
| 96 | - $image_width = array_key_exists( 1, $image_data ) ? $image_data [1] : false; |
|
| 97 | - $image_height = array_key_exists( 2, $image_data ) ? $image_data [2] : false; |
|
| 98 | - if ( ! ( $image_height && $image_width ) ) { |
|
| 99 | - continue; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ( $image_height < 1200 || $image_width < 1200 ) { |
|
| 103 | - // Image size not present in 1200 * 1200, show a warning. |
|
| 104 | - $recipe_with_image_warnings[] = $recipe_id; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return $recipe_with_image_warnings; |
|
| 109 | - } |
|
| 91 | + $image_id = $recipe->image_id(); |
|
| 92 | + $image_data = wp_get_attachment_image_src( $image_id, array( 1200, 1200 ) ); |
|
| 93 | + if ( ! is_array( $image_data ) ) { |
|
| 94 | + continue; |
|
| 95 | + } |
|
| 96 | + $image_width = array_key_exists( 1, $image_data ) ? $image_data [1] : false; |
|
| 97 | + $image_height = array_key_exists( 2, $image_data ) ? $image_data [2] : false; |
|
| 98 | + if ( ! ( $image_height && $image_width ) ) { |
|
| 99 | + continue; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ( $image_height < 1200 || $image_width < 1200 ) { |
|
| 103 | + // Image size not present in 1200 * 1200, show a warning. |
|
| 104 | + $recipe_with_image_warnings[] = $recipe_id; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return $recipe_with_image_warnings; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | private $recipe_maker_validation_service; |
| 17 | 17 | |
| 18 | - public function __construct( $recipe_maker_validation_service ) { |
|
| 18 | + public function __construct($recipe_maker_validation_service) { |
|
| 19 | 19 | $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
| 20 | 20 | /** |
| 21 | 21 | * Filter: wl_feature__enable__notices. |
@@ -25,8 +25,8 @@ discard block |
||
| 25 | 25 | * @return bool |
| 26 | 26 | * @since 3.27.6 |
| 27 | 27 | */ |
| 28 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 29 | - add_action( 'admin_notices', array( $this, 'display_image_size_warning' ) ); |
|
| 28 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
| 29 | + add_action('admin_notices', array($this, 'display_image_size_warning')); |
|
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
@@ -36,30 +36,30 @@ discard block |
||
| 36 | 36 | public function display_image_size_warning() { |
| 37 | 37 | |
| 38 | 38 | // Check if we are on the post. |
| 39 | - if ( ! get_post() instanceof \WP_Post ) { |
|
| 39 | + if ( ! get_post() instanceof \WP_Post) { |
|
| 40 | 40 | return false; |
| 41 | 41 | } |
| 42 | - if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available() ) { |
|
| 42 | + if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available()) { |
|
| 43 | 43 | return false; |
| 44 | 44 | } |
| 45 | 45 | $post_id = get_the_ID(); |
| 46 | 46 | |
| 47 | 47 | // Dont show notification if there is no recipes referred by the post. |
| 48 | - if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 48 | + if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post($post_id)) { |
|
| 49 | 49 | return false; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - $recipe_with_image_warnings = $this->get_warnings( $post_id ); |
|
| 52 | + $recipe_with_image_warnings = $this->get_warnings($post_id); |
|
| 53 | 53 | |
| 54 | - if ( count( $recipe_with_image_warnings ) > 0 ) { |
|
| 54 | + if (count($recipe_with_image_warnings) > 0) { |
|
| 55 | 55 | // Show notification. |
| 56 | 56 | ?> |
| 57 | 57 | <div class="notice notice-warning is-dismissible"> |
| 58 | - <p><?php echo wp_kses( __( 'The following recipes didnt have minimum image size of 1200 x 1200 px', 'wordlift' ), array() ); ?></p> |
|
| 58 | + <p><?php echo wp_kses(__('The following recipes didnt have minimum image size of 1200 x 1200 px', 'wordlift'), array()); ?></p> |
|
| 59 | 59 | <ol> |
| 60 | 60 | <?php |
| 61 | - foreach ( $recipe_with_image_warnings as $post_id ) { |
|
| 62 | - echo "<li>" . esc_html( get_the_title( $post_id ) ) . "</li>"; |
|
| 61 | + foreach ($recipe_with_image_warnings as $post_id) { |
|
| 62 | + echo "<li>".esc_html(get_the_title($post_id))."</li>"; |
|
| 63 | 63 | } |
| 64 | 64 | ?> |
| 65 | 65 | </ol> |
@@ -74,32 +74,32 @@ discard block |
||
| 74 | 74 | * |
| 75 | 75 | * @return array |
| 76 | 76 | */ |
| 77 | - private function get_warnings( $post_id ) { |
|
| 77 | + private function get_warnings($post_id) { |
|
| 78 | 78 | |
| 79 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 79 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post($post_id); |
|
| 80 | 80 | |
| 81 | 81 | // Dont show duplicate warnings. |
| 82 | - $recipe_ids = array_unique( $recipe_ids ); |
|
| 82 | + $recipe_ids = array_unique($recipe_ids); |
|
| 83 | 83 | |
| 84 | 84 | $recipe_with_image_warnings = array(); |
| 85 | 85 | |
| 86 | - foreach ( $recipe_ids as $recipe_id ) { |
|
| 87 | - $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
| 88 | - if ( ! $recipe ) { |
|
| 86 | + foreach ($recipe_ids as $recipe_id) { |
|
| 87 | + $recipe = \WPRM_Recipe_Manager::get_recipe($recipe_id); |
|
| 88 | + if ( ! $recipe) { |
|
| 89 | 89 | continue; |
| 90 | 90 | } |
| 91 | 91 | $image_id = $recipe->image_id(); |
| 92 | - $image_data = wp_get_attachment_image_src( $image_id, array( 1200, 1200 ) ); |
|
| 93 | - if ( ! is_array( $image_data ) ) { |
|
| 92 | + $image_data = wp_get_attachment_image_src($image_id, array(1200, 1200)); |
|
| 93 | + if ( ! is_array($image_data)) { |
|
| 94 | 94 | continue; |
| 95 | 95 | } |
| 96 | - $image_width = array_key_exists( 1, $image_data ) ? $image_data [1] : false; |
|
| 97 | - $image_height = array_key_exists( 2, $image_data ) ? $image_data [2] : false; |
|
| 98 | - if ( ! ( $image_height && $image_width ) ) { |
|
| 96 | + $image_width = array_key_exists(1, $image_data) ? $image_data [1] : false; |
|
| 97 | + $image_height = array_key_exists(2, $image_data) ? $image_data [2] : false; |
|
| 98 | + if ( ! ($image_height && $image_width)) { |
|
| 99 | 99 | continue; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - if ( $image_height < 1200 || $image_width < 1200 ) { |
|
| 102 | + if ($image_height < 1200 || $image_width < 1200) { |
|
| 103 | 103 | // Image size not present in 1200 * 1200, show a warning. |
| 104 | 104 | $recipe_with_image_warnings[] = $recipe_id; |
| 105 | 105 | } |
@@ -4,186 +4,186 @@ |
||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | class Config { |
| 7 | - /** |
|
| 8 | - * @var \Wordlift_Admin_Setup |
|
| 9 | - */ |
|
| 10 | - private $admin_setup; |
|
| 11 | - /** |
|
| 12 | - * @var \Wordlift_Key_Validation_Service |
|
| 13 | - */ |
|
| 14 | - private $key_validation_service; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * Config constructor. |
|
| 18 | - * |
|
| 19 | - * @param $admin_setup \Wordlift_Admin_Setup |
|
| 20 | - * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 21 | - */ |
|
| 22 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
| 23 | - |
|
| 24 | - $this->admin_setup = $admin_setup; |
|
| 25 | - $this->key_validation_service = $key_validation_service; |
|
| 26 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 27 | - |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Check if the key is valid and also not bound to any domain. |
|
| 32 | - * |
|
| 33 | - * @param $key string |
|
| 34 | - * |
|
| 35 | - * @return bool |
|
| 36 | - */ |
|
| 37 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 38 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * we need to check if the key is not associated with any account |
|
| 42 | - * before setting it, we should check if the url is null. |
|
| 43 | - */ |
|
| 44 | - if ( is_wp_error( $account_info ) |
|
| 45 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $account_info_json = $account_info['body']; |
|
| 50 | - |
|
| 51 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 52 | - |
|
| 53 | - if ( ! $account_info_data ) { |
|
| 54 | - // Invalid json returned by api. |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - if ( $account_info_data['url'] === null ) { |
|
| 62 | - return true; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Check if the key belongs to same site. |
|
| 66 | - if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 67 | - // key already associated with another account. |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Return true if the key domain and site domain are the same. |
|
| 72 | - return true; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - public function config() { |
|
| 77 | - |
|
| 78 | - // Perform validation check for all the parameters. |
|
| 79 | - $required_fields = array( |
|
| 80 | - 'diagnostic', |
|
| 81 | - 'vocabulary', |
|
| 7 | + /** |
|
| 8 | + * @var \Wordlift_Admin_Setup |
|
| 9 | + */ |
|
| 10 | + private $admin_setup; |
|
| 11 | + /** |
|
| 12 | + * @var \Wordlift_Key_Validation_Service |
|
| 13 | + */ |
|
| 14 | + private $key_validation_service; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * Config constructor. |
|
| 18 | + * |
|
| 19 | + * @param $admin_setup \Wordlift_Admin_Setup |
|
| 20 | + * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 21 | + */ |
|
| 22 | + public function __construct( $admin_setup, $key_validation_service ) { |
|
| 23 | + |
|
| 24 | + $this->admin_setup = $admin_setup; |
|
| 25 | + $this->key_validation_service = $key_validation_service; |
|
| 26 | + add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 27 | + |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Check if the key is valid and also not bound to any domain. |
|
| 32 | + * |
|
| 33 | + * @param $key string |
|
| 34 | + * |
|
| 35 | + * @return bool |
|
| 36 | + */ |
|
| 37 | + private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 38 | + $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * we need to check if the key is not associated with any account |
|
| 42 | + * before setting it, we should check if the url is null. |
|
| 43 | + */ |
|
| 44 | + if ( is_wp_error( $account_info ) |
|
| 45 | + || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $account_info_json = $account_info['body']; |
|
| 50 | + |
|
| 51 | + $account_info_data = json_decode( $account_info_json, true ); |
|
| 52 | + |
|
| 53 | + if ( ! $account_info_data ) { |
|
| 54 | + // Invalid json returned by api. |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + if ( $account_info_data['url'] === null ) { |
|
| 62 | + return true; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Check if the key belongs to same site. |
|
| 66 | + if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 67 | + // key already associated with another account. |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Return true if the key domain and site domain are the same. |
|
| 72 | + return true; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + public function config() { |
|
| 77 | + |
|
| 78 | + // Perform validation check for all the parameters. |
|
| 79 | + $required_fields = array( |
|
| 80 | + 'diagnostic', |
|
| 81 | + 'vocabulary', |
|
| 82 | 82 | // Don't ask for language from webapp. |
| 83 | 83 | // 'language', |
| 84 | - 'country', |
|
| 85 | - 'publisherName', |
|
| 86 | - 'publisher', |
|
| 87 | - 'license' |
|
| 88 | - ); |
|
| 84 | + 'country', |
|
| 85 | + 'publisherName', |
|
| 86 | + 'publisher', |
|
| 87 | + 'license' |
|
| 88 | + ); |
|
| 89 | 89 | |
| 90 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 90 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 91 | 91 | |
| 92 | - // validate all the fields before processing |
|
| 93 | - foreach ( $required_fields as $field ) { |
|
| 94 | - if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 95 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 92 | + // validate all the fields before processing |
|
| 93 | + foreach ( $required_fields as $field ) { |
|
| 94 | + if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 95 | + wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 96 | 96 | |
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - } |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - $key = wp_unslash( (string) $_POST['license'] ); |
|
| 101 | + $key = wp_unslash( (string) $_POST['license'] ); |
|
| 102 | 102 | |
| 103 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 104 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 103 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 104 | + wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 105 | 105 | |
| 106 | - // exit if not valid. |
|
| 107 | - return; |
|
| 108 | - } |
|
| 106 | + // exit if not valid. |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | - // check if key is already configured, if yes then dont save settings. |
|
| 112 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 113 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 111 | + // check if key is already configured, if yes then dont save settings. |
|
| 112 | + if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 113 | + wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 114 | 114 | |
| 115 | - // key already configured |
|
| 116 | - return; |
|
| 117 | - } |
|
| 115 | + // key already configured |
|
| 116 | + return; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 119 | + $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 120 | 120 | |
| 121 | 121 | |
| 122 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 123 | - } |
|
| 122 | + wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - private function get_params() { |
|
| 125 | + /** |
|
| 126 | + * |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + private function get_params() { |
|
| 130 | 130 | |
| 131 | - $attachment_id = $this->may_be_get_attachment_id(); |
|
| 131 | + $attachment_id = $this->may_be_get_attachment_id(); |
|
| 132 | 132 | |
| 133 | - $params = array( |
|
| 134 | - 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', |
|
| 135 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', |
|
| 136 | - 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', |
|
| 137 | - 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', |
|
| 138 | - 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', |
|
| 139 | - 'logo' => $attachment_id |
|
| 140 | - ); |
|
| 133 | + $params = array( |
|
| 134 | + 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', |
|
| 135 | + 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', |
|
| 136 | + 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', |
|
| 137 | + 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', |
|
| 138 | + 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', |
|
| 139 | + 'logo' => $attachment_id |
|
| 140 | + ); |
|
| 141 | 141 | |
| 142 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | - if ( $diagnostic ) { |
|
| 144 | - $params['share-diagnostic'] = 'on'; |
|
| 145 | - } |
|
| 142 | + $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | + if ( $diagnostic ) { |
|
| 144 | + $params['share-diagnostic'] = 'on'; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - return $params; |
|
| 148 | - } |
|
| 147 | + return $params; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * @return int | bool |
|
| 152 | - */ |
|
| 153 | - private function may_be_get_attachment_id() { |
|
| 150 | + /** |
|
| 151 | + * @return int | bool |
|
| 152 | + */ |
|
| 153 | + private function may_be_get_attachment_id() { |
|
| 154 | 154 | |
| 155 | - // if image or image extension not posted then return false. |
|
| 156 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 155 | + // if image or image extension not posted then return false. |
|
| 156 | + if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 161 | - $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); |
|
| 162 | - $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); |
|
| 160 | + $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 161 | + $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); |
|
| 162 | + $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); |
|
| 163 | 163 | |
| 164 | - if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 164 | + if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 168 | + $image_decoded_string = base64_decode( $image_string ); |
|
| 169 | 169 | |
| 170 | - $upload_dir = wp_upload_dir(); |
|
| 170 | + $upload_dir = wp_upload_dir(); |
|
| 171 | 171 | |
| 172 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 172 | + $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 173 | 173 | |
| 174 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 174 | + file_put_contents( $file_path, $image_decoded_string ); |
|
| 175 | 175 | |
| 176 | - $attachment_id = wp_insert_attachment( array( |
|
| 177 | - 'post_status' => 'inherit', |
|
| 178 | - 'post_mime_type' => "image/$image_ext" |
|
| 179 | - ), $file_path ); |
|
| 176 | + $attachment_id = wp_insert_attachment( array( |
|
| 177 | + 'post_status' => 'inherit', |
|
| 178 | + 'post_mime_type' => "image/$image_ext" |
|
| 179 | + ), $file_path ); |
|
| 180 | 180 | |
| 181 | - // Generate the metadata for the attachment, and update the database record. |
|
| 182 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 183 | - // Update the attachment metadata. |
|
| 184 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 181 | + // Generate the metadata for the attachment, and update the database record. |
|
| 182 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 183 | + // Update the attachment metadata. |
|
| 184 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 185 | 185 | |
| 186 | - return $attachment_id; |
|
| 187 | - } |
|
| 186 | + return $attachment_id; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | } |
@@ -19,11 +19,11 @@ discard block |
||
| 19 | 19 | * @param $admin_setup \Wordlift_Admin_Setup |
| 20 | 20 | * @param $key_validation_service \Wordlift_Key_Validation_Service |
| 21 | 21 | */ |
| 22 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
| 22 | + public function __construct($admin_setup, $key_validation_service) { |
|
| 23 | 23 | |
| 24 | 24 | $this->admin_setup = $admin_setup; |
| 25 | 25 | $this->key_validation_service = $key_validation_service; |
| 26 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 26 | + add_action('wp_ajax_nopriv_wl_config_plugin', array($this, 'config')); |
|
| 27 | 27 | |
| 28 | 28 | } |
| 29 | 29 | |
@@ -34,36 +34,36 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @return bool |
| 36 | 36 | */ |
| 37 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 38 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 37 | + private function is_key_valid_and_not_bound_to_any_domain($key) { |
|
| 38 | + $account_info = $this->key_validation_service->get_account_info($key); |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * we need to check if the key is not associated with any account |
| 42 | 42 | * before setting it, we should check if the url is null. |
| 43 | 43 | */ |
| 44 | - if ( is_wp_error( $account_info ) |
|
| 45 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 44 | + if (is_wp_error($account_info) |
|
| 45 | + || wp_remote_retrieve_response_code($account_info) !== 200) { |
|
| 46 | 46 | return false; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $account_info_json = $account_info['body']; |
| 50 | 50 | |
| 51 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 51 | + $account_info_data = json_decode($account_info_json, true); |
|
| 52 | 52 | |
| 53 | - if ( ! $account_info_data ) { |
|
| 53 | + if ( ! $account_info_data) { |
|
| 54 | 54 | // Invalid json returned by api. |
| 55 | 55 | return false; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 58 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit(get_option('home'))); |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - if ( $account_info_data['url'] === null ) { |
|
| 61 | + if ($account_info_data['url'] === null) { |
|
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | // Check if the key belongs to same site. |
| 66 | - if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 66 | + if ($site_url !== untrailingslashit($account_info_data['url'])) { |
|
| 67 | 67 | // key already associated with another account. |
| 68 | 68 | return false; |
| 69 | 69 | } |
@@ -87,21 +87,21 @@ discard block |
||
| 87 | 87 | 'license' |
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 90 | + header('Access-Control-Allow-Origin: *'); |
|
| 91 | 91 | |
| 92 | 92 | // validate all the fields before processing |
| 93 | - foreach ( $required_fields as $field ) { |
|
| 94 | - if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 95 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 93 | + foreach ($required_fields as $field) { |
|
| 94 | + if ( ! array_key_exists($field, $_POST)) { |
|
| 95 | + wp_send_json_error(sprintf(__('Field %s is required', 'wordlift'), $field), 422); |
|
| 96 | 96 | |
| 97 | 97 | return; |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - $key = wp_unslash( (string) $_POST['license'] ); |
|
| 101 | + $key = wp_unslash((string) $_POST['license']); |
|
| 102 | 102 | |
| 103 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 104 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 103 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain($key)) { |
|
| 104 | + wp_send_json_error(__('Key is not valid or associated with other domain', 'wordlift'), 403); |
|
| 105 | 105 | |
| 106 | 106 | // exit if not valid. |
| 107 | 107 | return; |
@@ -109,17 +109,17 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | |
| 111 | 111 | // check if key is already configured, if yes then dont save settings. |
| 112 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 113 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 112 | + if (\Wordlift_Configuration_Service::get_instance()->get_key()) { |
|
| 113 | + wp_send_json_error(__('Key already configured.', 'wordlift'), 403); |
|
| 114 | 114 | |
| 115 | 115 | // key already configured |
| 116 | 116 | return; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 119 | + $this->admin_setup->save_configuration($this->get_params()); |
|
| 120 | 120 | |
| 121 | 121 | |
| 122 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 122 | + wp_send_json_success(__('Configuration Saved', 'wordlift')); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -131,16 +131,16 @@ discard block |
||
| 131 | 131 | $attachment_id = $this->may_be_get_attachment_id(); |
| 132 | 132 | |
| 133 | 133 | $params = array( |
| 134 | - 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', |
|
| 135 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', |
|
| 136 | - 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', |
|
| 137 | - 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', |
|
| 138 | - 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', |
|
| 134 | + 'key' => isset($_POST['license']) ? sanitize_text_field(wp_unslash((string) $_POST['license'])) : '', |
|
| 135 | + 'vocabulary' => isset($_POST['vocabulary']) ? sanitize_text_field(wp_unslash((string) $_POST['vocabulary'])) : '', |
|
| 136 | + 'wl-country-code' => isset($_POST['country']) ? sanitize_text_field(wp_unslash((string) $_POST['country'])) : '', |
|
| 137 | + 'name' => isset($_POST['publisherName']) ? sanitize_text_field(wp_unslash((string) $_POST['publisherName'])) : '', |
|
| 138 | + 'user_type' => isset($_POST['publisher']) ? sanitize_text_field(wp_unslash((string) $_POST['publisher'])) : '', |
|
| 139 | 139 | 'logo' => $attachment_id |
| 140 | 140 | ); |
| 141 | 141 | |
| 142 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | - if ( $diagnostic ) { |
|
| 142 | + $diagnostic = isset($_POST['diagnostic']) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | + if ($diagnostic) { |
|
| 144 | 144 | $params['share-diagnostic'] = 'on'; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -153,35 +153,35 @@ discard block |
||
| 153 | 153 | private function may_be_get_attachment_id() { |
| 154 | 154 | |
| 155 | 155 | // if image or image extension not posted then return false. |
| 156 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 156 | + if ( ! isset($_POST['image']) || ! isset($_POST['imageExtension'])) { |
|
| 157 | 157 | return false; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 161 | - $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); |
|
| 162 | - $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); |
|
| 160 | + $allowed_extensions = array('png', 'jpeg', 'jpg'); |
|
| 161 | + $image_string = sanitize_text_field(wp_unslash((string) $_POST['image'])); |
|
| 162 | + $image_ext = sanitize_text_field(wp_unslash((string) $_POST['imageExtension'])); |
|
| 163 | 163 | |
| 164 | - if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 164 | + if ( ! in_array($image_ext, $allowed_extensions)) { |
|
| 165 | 165 | return false; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 168 | + $image_decoded_string = base64_decode($image_string); |
|
| 169 | 169 | |
| 170 | 170 | $upload_dir = wp_upload_dir(); |
| 171 | 171 | |
| 172 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 172 | + $file_path = $upload_dir['path'].DIRECTORY_SEPARATOR.md5($image_string).".".$image_ext; |
|
| 173 | 173 | |
| 174 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 174 | + file_put_contents($file_path, $image_decoded_string); |
|
| 175 | 175 | |
| 176 | - $attachment_id = wp_insert_attachment( array( |
|
| 176 | + $attachment_id = wp_insert_attachment(array( |
|
| 177 | 177 | 'post_status' => 'inherit', |
| 178 | 178 | 'post_mime_type' => "image/$image_ext" |
| 179 | - ), $file_path ); |
|
| 179 | + ), $file_path); |
|
| 180 | 180 | |
| 181 | 181 | // Generate the metadata for the attachment, and update the database record. |
| 182 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 182 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path); |
|
| 183 | 183 | // Update the attachment metadata. |
| 184 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 184 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 185 | 185 | |
| 186 | 186 | return $attachment_id; |
| 187 | 187 | } |
@@ -21,191 +21,191 @@ |
||
| 21 | 21 | |
| 22 | 22 | class Ttl_Cache_Cleaner { |
| 23 | 23 | |
| 24 | - const PATH = 0; |
|
| 25 | - const MTIME = 1; |
|
| 26 | - const SIZE = 2; |
|
| 24 | + const PATH = 0; |
|
| 25 | + const MTIME = 1; |
|
| 26 | + const SIZE = 2; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The max TTL in seconds. |
|
| 30 | - * |
|
| 31 | - * @access private |
|
| 32 | - * @var int $ttl The max TTL in seconds. |
|
| 33 | - */ |
|
| 34 | - private $ttl; |
|
| 28 | + /** |
|
| 29 | + * The max TTL in seconds. |
|
| 30 | + * |
|
| 31 | + * @access private |
|
| 32 | + * @var int $ttl The max TTL in seconds. |
|
| 33 | + */ |
|
| 34 | + private $ttl; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * The max size in bytes. |
|
| 38 | - * |
|
| 39 | - * @access private |
|
| 40 | - * @var int $ttl The max size in bytes. |
|
| 41 | - */ |
|
| 42 | - private $max_size; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Ttl_Cache_Cleaner constructor. |
|
| 46 | - * |
|
| 47 | - * @param int $ttl The max TTL in seconds. |
|
| 48 | - * @param int $max_size The max size in bytes. |
|
| 49 | - */ |
|
| 50 | - public function __construct( $ttl = WORDLIFT_CACHE_DEFAULT_TTL, $max_size = WORDLIFT_CACHE_DEFAULT_MAX_SIZE ) { |
|
| 51 | - |
|
| 52 | - $this->ttl = $ttl; |
|
| 53 | - $this->max_size = $max_size; |
|
| 54 | - |
|
| 55 | - add_action( 'wp_ajax_wl_ttl_cache_cleaner__flush', array( $this, 'flush' ) ); |
|
| 56 | - add_action( 'wl_ttl_cache_cleaner__flush', array( $this, 'flush' ) ); |
|
| 57 | - |
|
| 58 | - add_action( 'wp_ajax_wl_ttl_cache_cleaner__cleanup', array( $this, 'cleanup' ) ); |
|
| 59 | - add_action( 'wl_ttl_cache_cleaner__cleanup', array( $this, 'cleanup' ) ); |
|
| 60 | - |
|
| 61 | - // Do not bother to configure scheduled tasks while running on the front-end. |
|
| 62 | - if ( is_admin() && ! wp_next_scheduled( 'wl_ttl_cache_cleaner__cleanup' ) ) { |
|
| 63 | - wp_schedule_event( time(), 'hourly', 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public static function deactivate() { |
|
| 69 | - |
|
| 70 | - $timestamp = wp_next_scheduled( 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 71 | - wp_unschedule_event( $timestamp, 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 72 | - |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function flush() { |
|
| 76 | - |
|
| 77 | - // Get all the files, recursive. |
|
| 78 | - $files = $this->reduce( array(), Ttl_Cache::get_cache_folder() ); |
|
| 79 | - |
|
| 80 | - foreach ( $files as $file ) { |
|
| 81 | - @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; |
|
| 85 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'wl_ttl_cache_cleaner__flush' === $action ) { |
|
| 86 | - wp_send_json_success( count( $files ) ); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function cleanup() { |
|
| 91 | - |
|
| 92 | - // Get all the files, recursive. |
|
| 93 | - $files = $this->reduce( array(), Ttl_Cache::get_cache_folder() ); |
|
| 94 | - |
|
| 95 | - // Get the max mtime. |
|
| 96 | - $max_mtime = time() - WORDLIFT_CACHE_DEFAULT_TTL; |
|
| 97 | - |
|
| 98 | - // Keep the original count for statistics that we're going to send the client. |
|
| 99 | - $original_count = count( $files ); |
|
| 100 | - |
|
| 101 | - // Sort by size ascending. |
|
| 102 | - usort( $files, function ( $f1, $f2 ) { |
|
| 103 | - if ( $f1[ Ttl_Cache_Cleaner::MTIME ] === $f2[ Ttl_Cache_Cleaner::MTIME ] ) { |
|
| 104 | - return 0; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return ( $f1[ Ttl_Cache_Cleaner::MTIME ] < $f2[ Ttl_Cache_Cleaner::MTIME ] ) ? - 1 : 1; |
|
| 108 | - } ); |
|
| 109 | - |
|
| 110 | - // Start removing stale files. |
|
| 111 | - for ( $i = 0; $i < count( $files ); $i ++ ) { |
|
| 112 | - $file = $files[ $i ]; |
|
| 113 | - // Break if the mtime is within the range. |
|
| 114 | - if ( $file[ Ttl_Cache_Cleaner::MTIME ] > $max_mtime ) { |
|
| 115 | - break; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - unset( $files[ $i ] ); |
|
| 119 | - @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Calculate the size. |
|
| 123 | - $total_size = array_reduce( $files, function ( $carry, $item ) { |
|
| 124 | - |
|
| 125 | - return $carry + $item[ Ttl_Cache_Cleaner::SIZE ]; |
|
| 126 | - }, 0 ); |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - // Remove files until we're within the max size. |
|
| 130 | - while ( $total_size > $this->max_size ) { |
|
| 131 | - $file = array_shift( $files ); |
|
| 132 | - $total_size -= $file[ Ttl_Cache_Cleaner::SIZE ]; |
|
| 133 | - @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Send back some stats. |
|
| 137 | - wp_send_json_success( array( |
|
| 138 | - 'initial_count' => $original_count, |
|
| 139 | - 'current_count' => count( $files ), |
|
| 140 | - 'current_size' => $total_size, |
|
| 141 | - ) ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - private function reduce( $accumulator, $path ) { |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Bail out if the path doesn't exist. |
|
| 148 | - * |
|
| 149 | - * Avoid warnings when trying to open a path which doesn't exist. |
|
| 150 | - * |
|
| 151 | - * @since 3.23.0 |
|
| 152 | - * @see https://github.com/insideout10/wordlift-plugin/issues/966 |
|
| 153 | - */ |
|
| 154 | - if ( ! file_exists( $path ) ) { |
|
| 155 | - return $accumulator; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // Open the dir handle. |
|
| 159 | - $handle = opendir( $path ); |
|
| 160 | - |
|
| 161 | - // Catch exceptions to be sure to close the dir handle. |
|
| 162 | - try { |
|
| 163 | - $accumulator = @$this->_reduce( $accumulator, $path, $handle ); |
|
| 164 | - } catch ( Exception $e ) { |
|
| 165 | - // Do nothing. |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // Finally close the directory handle. |
|
| 169 | - closedir( $handle ); |
|
| 170 | - |
|
| 171 | - return $accumulator; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param $accumulator |
|
| 176 | - * @param $path |
|
| 177 | - * @param $handle |
|
| 178 | - * |
|
| 179 | - * @return array |
|
| 180 | - */ |
|
| 181 | - private function _reduce( $accumulator, $path, $handle ) { |
|
| 182 | - |
|
| 183 | - while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 184 | - |
|
| 185 | - // Skip to the next one. |
|
| 186 | - if ( 0 === strpos( $entry, '.' ) ) { |
|
| 187 | - continue; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - // Set the full path to the entry. |
|
| 191 | - $entry_path = $path . DIRECTORY_SEPARATOR . $entry; |
|
| 192 | - |
|
| 193 | - // Handle directories. |
|
| 194 | - if ( is_dir( $entry_path ) ) { |
|
| 195 | - $accumulator = $this->reduce( $accumulator, $entry_path ); |
|
| 196 | - |
|
| 197 | - continue; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // Store the file data. |
|
| 201 | - $accumulator[] = array( |
|
| 202 | - $entry_path, |
|
| 203 | - filemtime( $entry_path ), |
|
| 204 | - filesize( $entry_path ), |
|
| 205 | - ); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return $accumulator; |
|
| 209 | - } |
|
| 36 | + /** |
|
| 37 | + * The max size in bytes. |
|
| 38 | + * |
|
| 39 | + * @access private |
|
| 40 | + * @var int $ttl The max size in bytes. |
|
| 41 | + */ |
|
| 42 | + private $max_size; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Ttl_Cache_Cleaner constructor. |
|
| 46 | + * |
|
| 47 | + * @param int $ttl The max TTL in seconds. |
|
| 48 | + * @param int $max_size The max size in bytes. |
|
| 49 | + */ |
|
| 50 | + public function __construct( $ttl = WORDLIFT_CACHE_DEFAULT_TTL, $max_size = WORDLIFT_CACHE_DEFAULT_MAX_SIZE ) { |
|
| 51 | + |
|
| 52 | + $this->ttl = $ttl; |
|
| 53 | + $this->max_size = $max_size; |
|
| 54 | + |
|
| 55 | + add_action( 'wp_ajax_wl_ttl_cache_cleaner__flush', array( $this, 'flush' ) ); |
|
| 56 | + add_action( 'wl_ttl_cache_cleaner__flush', array( $this, 'flush' ) ); |
|
| 57 | + |
|
| 58 | + add_action( 'wp_ajax_wl_ttl_cache_cleaner__cleanup', array( $this, 'cleanup' ) ); |
|
| 59 | + add_action( 'wl_ttl_cache_cleaner__cleanup', array( $this, 'cleanup' ) ); |
|
| 60 | + |
|
| 61 | + // Do not bother to configure scheduled tasks while running on the front-end. |
|
| 62 | + if ( is_admin() && ! wp_next_scheduled( 'wl_ttl_cache_cleaner__cleanup' ) ) { |
|
| 63 | + wp_schedule_event( time(), 'hourly', 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public static function deactivate() { |
|
| 69 | + |
|
| 70 | + $timestamp = wp_next_scheduled( 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 71 | + wp_unschedule_event( $timestamp, 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 72 | + |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function flush() { |
|
| 76 | + |
|
| 77 | + // Get all the files, recursive. |
|
| 78 | + $files = $this->reduce( array(), Ttl_Cache::get_cache_folder() ); |
|
| 79 | + |
|
| 80 | + foreach ( $files as $file ) { |
|
| 81 | + @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; |
|
| 85 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'wl_ttl_cache_cleaner__flush' === $action ) { |
|
| 86 | + wp_send_json_success( count( $files ) ); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function cleanup() { |
|
| 91 | + |
|
| 92 | + // Get all the files, recursive. |
|
| 93 | + $files = $this->reduce( array(), Ttl_Cache::get_cache_folder() ); |
|
| 94 | + |
|
| 95 | + // Get the max mtime. |
|
| 96 | + $max_mtime = time() - WORDLIFT_CACHE_DEFAULT_TTL; |
|
| 97 | + |
|
| 98 | + // Keep the original count for statistics that we're going to send the client. |
|
| 99 | + $original_count = count( $files ); |
|
| 100 | + |
|
| 101 | + // Sort by size ascending. |
|
| 102 | + usort( $files, function ( $f1, $f2 ) { |
|
| 103 | + if ( $f1[ Ttl_Cache_Cleaner::MTIME ] === $f2[ Ttl_Cache_Cleaner::MTIME ] ) { |
|
| 104 | + return 0; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return ( $f1[ Ttl_Cache_Cleaner::MTIME ] < $f2[ Ttl_Cache_Cleaner::MTIME ] ) ? - 1 : 1; |
|
| 108 | + } ); |
|
| 109 | + |
|
| 110 | + // Start removing stale files. |
|
| 111 | + for ( $i = 0; $i < count( $files ); $i ++ ) { |
|
| 112 | + $file = $files[ $i ]; |
|
| 113 | + // Break if the mtime is within the range. |
|
| 114 | + if ( $file[ Ttl_Cache_Cleaner::MTIME ] > $max_mtime ) { |
|
| 115 | + break; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + unset( $files[ $i ] ); |
|
| 119 | + @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Calculate the size. |
|
| 123 | + $total_size = array_reduce( $files, function ( $carry, $item ) { |
|
| 124 | + |
|
| 125 | + return $carry + $item[ Ttl_Cache_Cleaner::SIZE ]; |
|
| 126 | + }, 0 ); |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + // Remove files until we're within the max size. |
|
| 130 | + while ( $total_size > $this->max_size ) { |
|
| 131 | + $file = array_shift( $files ); |
|
| 132 | + $total_size -= $file[ Ttl_Cache_Cleaner::SIZE ]; |
|
| 133 | + @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Send back some stats. |
|
| 137 | + wp_send_json_success( array( |
|
| 138 | + 'initial_count' => $original_count, |
|
| 139 | + 'current_count' => count( $files ), |
|
| 140 | + 'current_size' => $total_size, |
|
| 141 | + ) ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + private function reduce( $accumulator, $path ) { |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Bail out if the path doesn't exist. |
|
| 148 | + * |
|
| 149 | + * Avoid warnings when trying to open a path which doesn't exist. |
|
| 150 | + * |
|
| 151 | + * @since 3.23.0 |
|
| 152 | + * @see https://github.com/insideout10/wordlift-plugin/issues/966 |
|
| 153 | + */ |
|
| 154 | + if ( ! file_exists( $path ) ) { |
|
| 155 | + return $accumulator; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // Open the dir handle. |
|
| 159 | + $handle = opendir( $path ); |
|
| 160 | + |
|
| 161 | + // Catch exceptions to be sure to close the dir handle. |
|
| 162 | + try { |
|
| 163 | + $accumulator = @$this->_reduce( $accumulator, $path, $handle ); |
|
| 164 | + } catch ( Exception $e ) { |
|
| 165 | + // Do nothing. |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // Finally close the directory handle. |
|
| 169 | + closedir( $handle ); |
|
| 170 | + |
|
| 171 | + return $accumulator; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param $accumulator |
|
| 176 | + * @param $path |
|
| 177 | + * @param $handle |
|
| 178 | + * |
|
| 179 | + * @return array |
|
| 180 | + */ |
|
| 181 | + private function _reduce( $accumulator, $path, $handle ) { |
|
| 182 | + |
|
| 183 | + while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 184 | + |
|
| 185 | + // Skip to the next one. |
|
| 186 | + if ( 0 === strpos( $entry, '.' ) ) { |
|
| 187 | + continue; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + // Set the full path to the entry. |
|
| 191 | + $entry_path = $path . DIRECTORY_SEPARATOR . $entry; |
|
| 192 | + |
|
| 193 | + // Handle directories. |
|
| 194 | + if ( is_dir( $entry_path ) ) { |
|
| 195 | + $accumulator = $this->reduce( $accumulator, $entry_path ); |
|
| 196 | + |
|
| 197 | + continue; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // Store the file data. |
|
| 201 | + $accumulator[] = array( |
|
| 202 | + $entry_path, |
|
| 203 | + filemtime( $entry_path ), |
|
| 204 | + filesize( $entry_path ), |
|
| 205 | + ); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return $accumulator; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | } |
@@ -16,8 +16,8 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | use Exception; |
| 18 | 18 | |
| 19 | -defined( 'WORDLIFT_CACHE_DEFAULT_TTL' ) || define( 'WORDLIFT_CACHE_DEFAULT_TTL', 86400 ); // 24 hours |
|
| 20 | -defined( 'WORDLIFT_CACHE_DEFAULT_MAX_SIZE' ) || define( 'WORDLIFT_CACHE_DEFAULT_MAX_SIZE', 104857600 ); // 100 M |
|
| 19 | +defined('WORDLIFT_CACHE_DEFAULT_TTL') || define('WORDLIFT_CACHE_DEFAULT_TTL', 86400); // 24 hours |
|
| 20 | +defined('WORDLIFT_CACHE_DEFAULT_MAX_SIZE') || define('WORDLIFT_CACHE_DEFAULT_MAX_SIZE', 104857600); // 100 M |
|
| 21 | 21 | |
| 22 | 22 | class Ttl_Cache_Cleaner { |
| 23 | 23 | |
@@ -47,101 +47,101 @@ discard block |
||
| 47 | 47 | * @param int $ttl The max TTL in seconds. |
| 48 | 48 | * @param int $max_size The max size in bytes. |
| 49 | 49 | */ |
| 50 | - public function __construct( $ttl = WORDLIFT_CACHE_DEFAULT_TTL, $max_size = WORDLIFT_CACHE_DEFAULT_MAX_SIZE ) { |
|
| 50 | + public function __construct($ttl = WORDLIFT_CACHE_DEFAULT_TTL, $max_size = WORDLIFT_CACHE_DEFAULT_MAX_SIZE) { |
|
| 51 | 51 | |
| 52 | 52 | $this->ttl = $ttl; |
| 53 | 53 | $this->max_size = $max_size; |
| 54 | 54 | |
| 55 | - add_action( 'wp_ajax_wl_ttl_cache_cleaner__flush', array( $this, 'flush' ) ); |
|
| 56 | - add_action( 'wl_ttl_cache_cleaner__flush', array( $this, 'flush' ) ); |
|
| 55 | + add_action('wp_ajax_wl_ttl_cache_cleaner__flush', array($this, 'flush')); |
|
| 56 | + add_action('wl_ttl_cache_cleaner__flush', array($this, 'flush')); |
|
| 57 | 57 | |
| 58 | - add_action( 'wp_ajax_wl_ttl_cache_cleaner__cleanup', array( $this, 'cleanup' ) ); |
|
| 59 | - add_action( 'wl_ttl_cache_cleaner__cleanup', array( $this, 'cleanup' ) ); |
|
| 58 | + add_action('wp_ajax_wl_ttl_cache_cleaner__cleanup', array($this, 'cleanup')); |
|
| 59 | + add_action('wl_ttl_cache_cleaner__cleanup', array($this, 'cleanup')); |
|
| 60 | 60 | |
| 61 | 61 | // Do not bother to configure scheduled tasks while running on the front-end. |
| 62 | - if ( is_admin() && ! wp_next_scheduled( 'wl_ttl_cache_cleaner__cleanup' ) ) { |
|
| 63 | - wp_schedule_event( time(), 'hourly', 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 62 | + if (is_admin() && ! wp_next_scheduled('wl_ttl_cache_cleaner__cleanup')) { |
|
| 63 | + wp_schedule_event(time(), 'hourly', 'wl_ttl_cache_cleaner__cleanup'); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public static function deactivate() { |
| 69 | 69 | |
| 70 | - $timestamp = wp_next_scheduled( 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 71 | - wp_unschedule_event( $timestamp, 'wl_ttl_cache_cleaner__cleanup' ); |
|
| 70 | + $timestamp = wp_next_scheduled('wl_ttl_cache_cleaner__cleanup'); |
|
| 71 | + wp_unschedule_event($timestamp, 'wl_ttl_cache_cleaner__cleanup'); |
|
| 72 | 72 | |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | public function flush() { |
| 76 | 76 | |
| 77 | 77 | // Get all the files, recursive. |
| 78 | - $files = $this->reduce( array(), Ttl_Cache::get_cache_folder() ); |
|
| 78 | + $files = $this->reduce(array(), Ttl_Cache::get_cache_folder()); |
|
| 79 | 79 | |
| 80 | - foreach ( $files as $file ) { |
|
| 81 | - @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 80 | + foreach ($files as $file) { |
|
| 81 | + @unlink($file[Ttl_Cache_Cleaner::PATH]); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; |
|
| 85 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'wl_ttl_cache_cleaner__flush' === $action ) { |
|
| 86 | - wp_send_json_success( count( $files ) ); |
|
| 84 | + $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; |
|
| 85 | + if (defined('DOING_AJAX') && DOING_AJAX && 'wl_ttl_cache_cleaner__flush' === $action) { |
|
| 86 | + wp_send_json_success(count($files)); |
|
| 87 | 87 | } |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | public function cleanup() { |
| 91 | 91 | |
| 92 | 92 | // Get all the files, recursive. |
| 93 | - $files = $this->reduce( array(), Ttl_Cache::get_cache_folder() ); |
|
| 93 | + $files = $this->reduce(array(), Ttl_Cache::get_cache_folder()); |
|
| 94 | 94 | |
| 95 | 95 | // Get the max mtime. |
| 96 | 96 | $max_mtime = time() - WORDLIFT_CACHE_DEFAULT_TTL; |
| 97 | 97 | |
| 98 | 98 | // Keep the original count for statistics that we're going to send the client. |
| 99 | - $original_count = count( $files ); |
|
| 99 | + $original_count = count($files); |
|
| 100 | 100 | |
| 101 | 101 | // Sort by size ascending. |
| 102 | - usort( $files, function ( $f1, $f2 ) { |
|
| 103 | - if ( $f1[ Ttl_Cache_Cleaner::MTIME ] === $f2[ Ttl_Cache_Cleaner::MTIME ] ) { |
|
| 102 | + usort($files, function($f1, $f2) { |
|
| 103 | + if ($f1[Ttl_Cache_Cleaner::MTIME] === $f2[Ttl_Cache_Cleaner::MTIME]) { |
|
| 104 | 104 | return 0; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - return ( $f1[ Ttl_Cache_Cleaner::MTIME ] < $f2[ Ttl_Cache_Cleaner::MTIME ] ) ? - 1 : 1; |
|
| 107 | + return ($f1[Ttl_Cache_Cleaner::MTIME] < $f2[Ttl_Cache_Cleaner::MTIME]) ? -1 : 1; |
|
| 108 | 108 | } ); |
| 109 | 109 | |
| 110 | 110 | // Start removing stale files. |
| 111 | - for ( $i = 0; $i < count( $files ); $i ++ ) { |
|
| 112 | - $file = $files[ $i ]; |
|
| 111 | + for ($i = 0; $i < count($files); $i++) { |
|
| 112 | + $file = $files[$i]; |
|
| 113 | 113 | // Break if the mtime is within the range. |
| 114 | - if ( $file[ Ttl_Cache_Cleaner::MTIME ] > $max_mtime ) { |
|
| 114 | + if ($file[Ttl_Cache_Cleaner::MTIME] > $max_mtime) { |
|
| 115 | 115 | break; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - unset( $files[ $i ] ); |
|
| 119 | - @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 118 | + unset($files[$i]); |
|
| 119 | + @unlink($file[Ttl_Cache_Cleaner::PATH]); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Calculate the size. |
| 123 | - $total_size = array_reduce( $files, function ( $carry, $item ) { |
|
| 123 | + $total_size = array_reduce($files, function($carry, $item) { |
|
| 124 | 124 | |
| 125 | - return $carry + $item[ Ttl_Cache_Cleaner::SIZE ]; |
|
| 126 | - }, 0 ); |
|
| 125 | + return $carry + $item[Ttl_Cache_Cleaner::SIZE]; |
|
| 126 | + }, 0); |
|
| 127 | 127 | |
| 128 | 128 | |
| 129 | 129 | // Remove files until we're within the max size. |
| 130 | - while ( $total_size > $this->max_size ) { |
|
| 131 | - $file = array_shift( $files ); |
|
| 132 | - $total_size -= $file[ Ttl_Cache_Cleaner::SIZE ]; |
|
| 133 | - @unlink( $file[ Ttl_Cache_Cleaner::PATH ] ); |
|
| 130 | + while ($total_size > $this->max_size) { |
|
| 131 | + $file = array_shift($files); |
|
| 132 | + $total_size -= $file[Ttl_Cache_Cleaner::SIZE]; |
|
| 133 | + @unlink($file[Ttl_Cache_Cleaner::PATH]); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // Send back some stats. |
| 137 | - wp_send_json_success( array( |
|
| 137 | + wp_send_json_success(array( |
|
| 138 | 138 | 'initial_count' => $original_count, |
| 139 | - 'current_count' => count( $files ), |
|
| 139 | + 'current_count' => count($files), |
|
| 140 | 140 | 'current_size' => $total_size, |
| 141 | - ) ); |
|
| 141 | + )); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - private function reduce( $accumulator, $path ) { |
|
| 144 | + private function reduce($accumulator, $path) { |
|
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | 147 | * Bail out if the path doesn't exist. |
@@ -151,22 +151,22 @@ discard block |
||
| 151 | 151 | * @since 3.23.0 |
| 152 | 152 | * @see https://github.com/insideout10/wordlift-plugin/issues/966 |
| 153 | 153 | */ |
| 154 | - if ( ! file_exists( $path ) ) { |
|
| 154 | + if ( ! file_exists($path)) { |
|
| 155 | 155 | return $accumulator; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | // Open the dir handle. |
| 159 | - $handle = opendir( $path ); |
|
| 159 | + $handle = opendir($path); |
|
| 160 | 160 | |
| 161 | 161 | // Catch exceptions to be sure to close the dir handle. |
| 162 | 162 | try { |
| 163 | - $accumulator = @$this->_reduce( $accumulator, $path, $handle ); |
|
| 164 | - } catch ( Exception $e ) { |
|
| 163 | + $accumulator = @$this->_reduce($accumulator, $path, $handle); |
|
| 164 | + } catch (Exception $e) { |
|
| 165 | 165 | // Do nothing. |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | // Finally close the directory handle. |
| 169 | - closedir( $handle ); |
|
| 169 | + closedir($handle); |
|
| 170 | 170 | |
| 171 | 171 | return $accumulator; |
| 172 | 172 | } |
@@ -178,21 +178,21 @@ discard block |
||
| 178 | 178 | * |
| 179 | 179 | * @return array |
| 180 | 180 | */ |
| 181 | - private function _reduce( $accumulator, $path, $handle ) { |
|
| 181 | + private function _reduce($accumulator, $path, $handle) { |
|
| 182 | 182 | |
| 183 | - while ( false !== ( $entry = readdir( $handle ) ) ) { |
|
| 183 | + while (false !== ($entry = readdir($handle))) { |
|
| 184 | 184 | |
| 185 | 185 | // Skip to the next one. |
| 186 | - if ( 0 === strpos( $entry, '.' ) ) { |
|
| 186 | + if (0 === strpos($entry, '.')) { |
|
| 187 | 187 | continue; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | // Set the full path to the entry. |
| 191 | - $entry_path = $path . DIRECTORY_SEPARATOR . $entry; |
|
| 191 | + $entry_path = $path.DIRECTORY_SEPARATOR.$entry; |
|
| 192 | 192 | |
| 193 | 193 | // Handle directories. |
| 194 | - if ( is_dir( $entry_path ) ) { |
|
| 195 | - $accumulator = $this->reduce( $accumulator, $entry_path ); |
|
| 194 | + if (is_dir($entry_path)) { |
|
| 195 | + $accumulator = $this->reduce($accumulator, $entry_path); |
|
| 196 | 196 | |
| 197 | 197 | continue; |
| 198 | 198 | } |
@@ -200,8 +200,8 @@ discard block |
||
| 200 | 200 | // Store the file data. |
| 201 | 201 | $accumulator[] = array( |
| 202 | 202 | $entry_path, |
| 203 | - filemtime( $entry_path ), |
|
| 204 | - filesize( $entry_path ), |
|
| 203 | + filemtime($entry_path), |
|
| 204 | + filesize($entry_path), |
|
| 205 | 205 | ); |
| 206 | 206 | } |
| 207 | 207 | |