@@ -17,59 +17,59 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - const SHORTCODE = 'wl_navigator'; |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + const SHORTCODE = 'wl_navigator'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - public function render( $atts ) { |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + public function render( $atts ) { |
|
| 29 | 29 | |
| 30 | - // Extract attributes and set default values. |
|
| 31 | - $shortcode_atts = shortcode_atts( array( |
|
| 32 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 33 | - 'with_carousel' => true, |
|
| 34 | - 'squared_thumbs' => false, |
|
| 35 | - ), $atts ); |
|
| 30 | + // Extract attributes and set default values. |
|
| 31 | + $shortcode_atts = shortcode_atts( array( |
|
| 32 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 33 | + 'with_carousel' => true, |
|
| 34 | + 'squared_thumbs' => false, |
|
| 35 | + ), $atts ); |
|
| 36 | 36 | |
| 37 | - foreach ( |
|
| 38 | - array( |
|
| 39 | - 'with_carousel', |
|
| 40 | - 'squared_thumbs', |
|
| 41 | - ) as $att |
|
| 42 | - ) { |
|
| 37 | + foreach ( |
|
| 38 | + array( |
|
| 39 | + 'with_carousel', |
|
| 40 | + 'squared_thumbs', |
|
| 41 | + ) as $att |
|
| 42 | + ) { |
|
| 43 | 43 | |
| 44 | - // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
|
| 45 | - $shortcode_atts[ $att ] = filter_var( |
|
| 46 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 47 | - ); |
|
| 48 | - } |
|
| 44 | + // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
|
| 45 | + $shortcode_atts[ $att ] = filter_var( |
|
| 46 | + $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - // avoid building the widget when there is a list of posts. |
|
| 51 | - if ( ! is_single() ) { |
|
| 52 | - return ''; |
|
| 53 | - } |
|
| 50 | + // avoid building the widget when there is a list of posts. |
|
| 51 | + if ( ! is_single() ) { |
|
| 52 | + return ''; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - $current_post = get_post(); |
|
| 55 | + $current_post = get_post(); |
|
| 56 | 56 | |
| 57 | - // Enqueue common shortcode scripts. |
|
| 58 | - $this->enqueue_scripts(); |
|
| 57 | + // Enqueue common shortcode scripts. |
|
| 58 | + $this->enqueue_scripts(); |
|
| 59 | 59 | |
| 60 | - wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
|
| 60 | + wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
|
| 61 | 61 | |
| 62 | - $navigator_id = uniqid( 'wl-navigator-widget-' ); |
|
| 62 | + $navigator_id = uniqid( 'wl-navigator-widget-' ); |
|
| 63 | 63 | |
| 64 | - wp_localize_script( 'wordlift-ui', 'wl_navigator_params', array( |
|
| 65 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 66 | - 'action' => 'wl_navigator', |
|
| 67 | - 'post_id' => $current_post->ID, |
|
| 68 | - 'attrs' => $shortcode_atts, |
|
| 69 | - ) |
|
| 70 | - ); |
|
| 64 | + wp_localize_script( 'wordlift-ui', 'wl_navigator_params', array( |
|
| 65 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 66 | + 'action' => 'wl_navigator', |
|
| 67 | + 'post_id' => $current_post->ID, |
|
| 68 | + 'attrs' => $shortcode_atts, |
|
| 69 | + ) |
|
| 70 | + ); |
|
| 71 | 71 | |
| 72 | - return "<div id='$navigator_id' class='wl-navigator-widget'></div>"; |
|
| 73 | - } |
|
| 72 | + return "<div id='$navigator_id' class='wl-navigator-widget'></div>"; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | } |
@@ -25,14 +25,14 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * {@inheritdoc} |
| 27 | 27 | */ |
| 28 | - public function render( $atts ) { |
|
| 28 | + public function render($atts) { |
|
| 29 | 29 | |
| 30 | 30 | // Extract attributes and set default values. |
| 31 | - $shortcode_atts = shortcode_atts( array( |
|
| 32 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 31 | + $shortcode_atts = shortcode_atts(array( |
|
| 32 | + 'title' => __('Related articles', 'wordlift'), |
|
| 33 | 33 | 'with_carousel' => true, |
| 34 | 34 | 'squared_thumbs' => false, |
| 35 | - ), $atts ); |
|
| 35 | + ), $atts); |
|
| 36 | 36 | |
| 37 | 37 | foreach ( |
| 38 | 38 | array( |
@@ -42,13 +42,13 @@ discard block |
||
| 42 | 42 | ) { |
| 43 | 43 | |
| 44 | 44 | // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
| 45 | - $shortcode_atts[ $att ] = filter_var( |
|
| 46 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 45 | + $shortcode_atts[$att] = filter_var( |
|
| 46 | + $shortcode_atts[$att], FILTER_VALIDATE_BOOLEAN |
|
| 47 | 47 | ); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // avoid building the widget when there is a list of posts. |
| 51 | - if ( ! is_single() ) { |
|
| 51 | + if ( ! is_single()) { |
|
| 52 | 52 | return ''; |
| 53 | 53 | } |
| 54 | 54 | |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | // Enqueue common shortcode scripts. |
| 58 | 58 | $this->enqueue_scripts(); |
| 59 | 59 | |
| 60 | - wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
|
| 60 | + wp_enqueue_style('wordlift-ui', dirname(plugin_dir_url(__FILE__)).'/css/wordlift-ui.min.css'); |
|
| 61 | 61 | |
| 62 | - $navigator_id = uniqid( 'wl-navigator-widget-' ); |
|
| 62 | + $navigator_id = uniqid('wl-navigator-widget-'); |
|
| 63 | 63 | |
| 64 | - wp_localize_script( 'wordlift-ui', 'wl_navigator_params', array( |
|
| 65 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 64 | + wp_localize_script('wordlift-ui', 'wl_navigator_params', array( |
|
| 65 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 66 | 66 | 'action' => 'wl_navigator', |
| 67 | 67 | 'post_id' => $current_post->ID, |
| 68 | 68 | 'attrs' => $shortcode_atts, |
@@ -18,38 +18,38 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Select2_Element implements Wordlift_Admin_Element { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @inheritdoc |
|
| 23 | - */ |
|
| 24 | - public function render( $args ) { |
|
| 21 | + /** |
|
| 22 | + * @inheritdoc |
|
| 23 | + */ |
|
| 24 | + public function render( $args ) { |
|
| 25 | 25 | |
| 26 | - // Enqueue select2 library js and css. |
|
| 27 | - // Underscore is needed for Select2's `templateResult` and `templateSelection` templates. |
|
| 28 | - wp_enqueue_script( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/js/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', array( |
|
| 29 | - 'jquery', |
|
| 30 | - 'underscore', |
|
| 31 | - ), '4.0.3' ); |
|
| 32 | - wp_enqueue_style( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/css/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), '4.0.3' ); |
|
| 26 | + // Enqueue select2 library js and css. |
|
| 27 | + // Underscore is needed for Select2's `templateResult` and `templateSelection` templates. |
|
| 28 | + wp_enqueue_script( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/js/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', array( |
|
| 29 | + 'jquery', |
|
| 30 | + 'underscore', |
|
| 31 | + ), '4.0.3' ); |
|
| 32 | + wp_enqueue_style( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/css/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), '4.0.3' ); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - // Parse the arguments and merge with default values. |
|
| 36 | - $params = wp_parse_args( $args, array( |
|
| 37 | - 'id' => uniqid( 'wl-input-' ), |
|
| 38 | - 'name' => uniqid( 'wl-input-' ), |
|
| 39 | - 'value' => null, |
|
| 40 | - 'options' => array(), |
|
| 41 | - 'description' => false, |
|
| 42 | - 'data' => array(), |
|
| 43 | - 'template-result' => '<%= text %>', |
|
| 44 | - 'template-selection' => '<%= text %>', |
|
| 45 | - ) ); |
|
| 35 | + // Parse the arguments and merge with default values. |
|
| 36 | + $params = wp_parse_args( $args, array( |
|
| 37 | + 'id' => uniqid( 'wl-input-' ), |
|
| 38 | + 'name' => uniqid( 'wl-input-' ), |
|
| 39 | + 'value' => null, |
|
| 40 | + 'options' => array(), |
|
| 41 | + 'description' => false, |
|
| 42 | + 'data' => array(), |
|
| 43 | + 'template-result' => '<%= text %>', |
|
| 44 | + 'template-selection' => '<%= text %>', |
|
| 45 | + ) ); |
|
| 46 | 46 | |
| 47 | - $description = $params['description'] ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
| 47 | + $description = $params['description'] ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
| 48 | 48 | |
| 49 | - // For data attributes we define our own "namespace" `wl-select2-` to avoid |
|
| 50 | - // Select2 preloading configuration on its own. In fact we need to support |
|
| 51 | - // underscore templates. |
|
| 52 | - ?> |
|
| 49 | + // For data attributes we define our own "namespace" `wl-select2-` to avoid |
|
| 50 | + // Select2 preloading configuration on its own. In fact we need to support |
|
| 51 | + // underscore templates. |
|
| 52 | + ?> |
|
| 53 | 53 | <select id="<?php echo esc_attr( $params['id'] ); ?>" |
| 54 | 54 | name="<?php echo esc_attr( $params['name'] ); ?>" |
| 55 | 55 | class="wl-select2-element" |
@@ -59,14 +59,14 @@ discard block |
||
| 59 | 59 | <?php foreach ( $params['options'] as $value => $label ) { ?> |
| 60 | 60 | <option value="<?php echo esc_attr( $value ); ?>" |
| 61 | 61 | <?php selected( $params['value'], $value ) ?>><?php |
| 62 | - echo esc_html( $label ); ?></option> |
|
| 62 | + echo esc_html( $label ); ?></option> |
|
| 63 | 63 | <?php } ?> |
| 64 | 64 | </select> |
| 65 | 65 | <?php echo $description; ?> |
| 66 | 66 | |
| 67 | 67 | <?php |
| 68 | 68 | |
| 69 | - return $this; |
|
| 70 | - } |
|
| 69 | + return $this; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | } |
@@ -21,45 +21,45 @@ |
||
| 21 | 21 | /** |
| 22 | 22 | * @inheritdoc |
| 23 | 23 | */ |
| 24 | - public function render( $args ) { |
|
| 24 | + public function render($args) { |
|
| 25 | 25 | |
| 26 | 26 | // Enqueue select2 library js and css. |
| 27 | 27 | // Underscore is needed for Select2's `templateResult` and `templateSelection` templates. |
| 28 | - wp_enqueue_script( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/js/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', array( |
|
| 28 | + wp_enqueue_script('wordlift-select2', plugin_dir_url(dirname(__FILE__)).'admin/js/select2/js/select2'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '.min' : '').'.js', array( |
|
| 29 | 29 | 'jquery', |
| 30 | 30 | 'underscore', |
| 31 | - ), '4.0.3' ); |
|
| 32 | - wp_enqueue_style( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/css/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), '4.0.3' ); |
|
| 31 | + ), '4.0.3'); |
|
| 32 | + wp_enqueue_style('wordlift-select2', plugin_dir_url(dirname(__FILE__)).'admin/js/select2/css/select2'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '.min' : '').'.css', array(), '4.0.3'); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | 35 | // Parse the arguments and merge with default values. |
| 36 | - $params = wp_parse_args( $args, array( |
|
| 37 | - 'id' => uniqid( 'wl-input-' ), |
|
| 38 | - 'name' => uniqid( 'wl-input-' ), |
|
| 36 | + $params = wp_parse_args($args, array( |
|
| 37 | + 'id' => uniqid('wl-input-'), |
|
| 38 | + 'name' => uniqid('wl-input-'), |
|
| 39 | 39 | 'value' => null, |
| 40 | 40 | 'options' => array(), |
| 41 | 41 | 'description' => false, |
| 42 | 42 | 'data' => array(), |
| 43 | 43 | 'template-result' => '<%= text %>', |
| 44 | 44 | 'template-selection' => '<%= text %>', |
| 45 | - ) ); |
|
| 45 | + )); |
|
| 46 | 46 | |
| 47 | - $description = $params['description'] ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
| 47 | + $description = $params['description'] ? '<p>'.wp_kses($params['description'], array('a' => array('href' => array()))).'</p>' : ''; |
|
| 48 | 48 | |
| 49 | 49 | // For data attributes we define our own "namespace" `wl-select2-` to avoid |
| 50 | 50 | // Select2 preloading configuration on its own. In fact we need to support |
| 51 | 51 | // underscore templates. |
| 52 | 52 | ?> |
| 53 | - <select id="<?php echo esc_attr( $params['id'] ); ?>" |
|
| 54 | - name="<?php echo esc_attr( $params['name'] ); ?>" |
|
| 53 | + <select id="<?php echo esc_attr($params['id']); ?>" |
|
| 54 | + name="<?php echo esc_attr($params['name']); ?>" |
|
| 55 | 55 | class="wl-select2-element" |
| 56 | - data-wl-select2-data="<?php echo esc_attr( json_encode( $params['data'] ) ); ?>" |
|
| 57 | - data-wl-select2-template-result="<?php echo esc_attr( $params['template-result'] ); ?>" |
|
| 58 | - data-wl-select2-template-selection="<?php echo esc_attr( $params['template-selection'] ); ?>"> |
|
| 59 | - <?php foreach ( $params['options'] as $value => $label ) { ?> |
|
| 60 | - <option value="<?php echo esc_attr( $value ); ?>" |
|
| 61 | - <?php selected( $params['value'], $value ) ?>><?php |
|
| 62 | - echo esc_html( $label ); ?></option> |
|
| 56 | + data-wl-select2-data="<?php echo esc_attr(json_encode($params['data'])); ?>" |
|
| 57 | + data-wl-select2-template-result="<?php echo esc_attr($params['template-result']); ?>" |
|
| 58 | + data-wl-select2-template-selection="<?php echo esc_attr($params['template-selection']); ?>"> |
|
| 59 | + <?php foreach ($params['options'] as $value => $label) { ?> |
|
| 60 | + <option value="<?php echo esc_attr($value); ?>" |
|
| 61 | + <?php selected($params['value'], $value) ?>><?php |
|
| 62 | + echo esc_html($label); ?></option> |
|
| 63 | 63 | <?php } ?> |
| 64 | 64 | </select> |
| 65 | 65 | <?php echo $description; ?> |
@@ -18,41 +18,41 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Metabox_Field_Duration extends WL_Metabox_Field_date { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * {@inheritdoc} |
|
| 23 | - */ |
|
| 24 | - public function __construct( $args ) { |
|
| 25 | - parent::__construct( $args ); |
|
| 26 | - |
|
| 27 | - $this->date_format = 'H:i'; |
|
| 28 | - $this->timepicker = true; |
|
| 29 | - $this->no_calendar = true; |
|
| 30 | - |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Sanitize a single value. Called from $this->sanitize_data. Default sanitization excludes empty values. |
|
| 35 | - * make sure the value is either empty, an integer representing valid number of minutes |
|
| 36 | - * or an HH:MM time format. |
|
| 37 | - * |
|
| 38 | - * @param mixed $value The value being sanitized. |
|
| 39 | - * |
|
| 40 | - * @return mixed Returns sanitized value, or null. |
|
| 41 | - */ |
|
| 42 | - public function sanitize_data_filter( $value ) { |
|
| 43 | - |
|
| 44 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 45 | - preg_match( '#((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}#', |
|
| 46 | - trim( $value ), |
|
| 47 | - $matches |
|
| 48 | - ); |
|
| 49 | - |
|
| 50 | - if ( count( $matches ) > 0 ) { |
|
| 51 | - return $matches[0]; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - return null; |
|
| 56 | - } |
|
| 21 | + /** |
|
| 22 | + * {@inheritdoc} |
|
| 23 | + */ |
|
| 24 | + public function __construct( $args ) { |
|
| 25 | + parent::__construct( $args ); |
|
| 26 | + |
|
| 27 | + $this->date_format = 'H:i'; |
|
| 28 | + $this->timepicker = true; |
|
| 29 | + $this->no_calendar = true; |
|
| 30 | + |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Sanitize a single value. Called from $this->sanitize_data. Default sanitization excludes empty values. |
|
| 35 | + * make sure the value is either empty, an integer representing valid number of minutes |
|
| 36 | + * or an HH:MM time format. |
|
| 37 | + * |
|
| 38 | + * @param mixed $value The value being sanitized. |
|
| 39 | + * |
|
| 40 | + * @return mixed Returns sanitized value, or null. |
|
| 41 | + */ |
|
| 42 | + public function sanitize_data_filter( $value ) { |
|
| 43 | + |
|
| 44 | + if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 45 | + preg_match( '#((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}#', |
|
| 46 | + trim( $value ), |
|
| 47 | + $matches |
|
| 48 | + ); |
|
| 49 | + |
|
| 50 | + if ( count( $matches ) > 0 ) { |
|
| 51 | + return $matches[0]; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + return null; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | /** |
| 22 | 22 | * {@inheritdoc} |
| 23 | 23 | */ |
| 24 | - public function __construct( $args ) { |
|
| 25 | - parent::__construct( $args ); |
|
| 24 | + public function __construct($args) { |
|
| 25 | + parent::__construct($args); |
|
| 26 | 26 | |
| 27 | 27 | $this->date_format = 'H:i'; |
| 28 | 28 | $this->timepicker = true; |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @return mixed Returns sanitized value, or null. |
| 41 | 41 | */ |
| 42 | - public function sanitize_data_filter( $value ) { |
|
| 42 | + public function sanitize_data_filter($value) { |
|
| 43 | 43 | |
| 44 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 45 | - preg_match( '#((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}#', |
|
| 46 | - trim( $value ), |
|
| 44 | + if ( ! is_null($value) && '' !== $value) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 45 | + preg_match('#((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}#', |
|
| 46 | + trim($value), |
|
| 47 | 47 | $matches |
| 48 | 48 | ); |
| 49 | 49 | |
| 50 | - if ( count( $matches ) > 0 ) { |
|
| 50 | + if (count($matches) > 0) { |
|
| 51 | 51 | return $matches[0]; |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -7,258 +7,258 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class WL_Metabox_Field { |
| 9 | 9 | |
| 10 | - public $meta_name; |
|
| 11 | - public $raw_custom_field; |
|
| 12 | - public $predicate; |
|
| 13 | - public $label; |
|
| 14 | - public $expected_wl_type; |
|
| 15 | - public $expected_uri_type; |
|
| 16 | - public $cardinality; |
|
| 17 | - public $data; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @param array $args |
|
| 21 | - */ |
|
| 22 | - public function __construct( $args ) { |
|
| 23 | - |
|
| 24 | - if ( empty( $args ) ) { |
|
| 25 | - return; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - // Save a copy of the custom field's params. |
|
| 29 | - $this->raw_custom_field = reset( $args ); |
|
| 30 | - |
|
| 31 | - // Extract meta name (post_meta key for the DB). |
|
| 32 | - $this->meta_name = key( $args ); |
|
| 33 | - |
|
| 34 | - // Extract linked data predicate. |
|
| 35 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 36 | - $this->predicate = $this->raw_custom_field['predicate']; |
|
| 37 | - } else { |
|
| 38 | - return; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - // Extract human readable label. |
|
| 42 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 43 | - |
|
| 44 | - // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 45 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 46 | - |
|
| 47 | - // Extract field constraints (numerosity, expected type). |
|
| 48 | - // Default constaints: accept one string.. |
|
| 49 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 50 | - $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 51 | - } else { |
|
| 52 | - $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $this->cardinality = 1; |
|
| 56 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 57 | - |
|
| 58 | - $constraints = $this->raw_custom_field['constraints']; |
|
| 59 | - |
|
| 60 | - // Extract cardinality. |
|
| 61 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 62 | - $this->cardinality = $constraints['cardinality']; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 66 | - if ( $this->expected_wl_type === Wordlift_Schema_Service::DATA_TYPE_URI && isset( $constraints['uri_type'] ) ) { |
|
| 67 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) ? $constraints['uri_type'] : array( $constraints['uri_type'] ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Return nonce HTML. |
|
| 75 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 76 | - */ |
|
| 77 | - public function html_nonce() { |
|
| 78 | - |
|
| 79 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', TRUE, FALSE ); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Verify nonce. |
|
| 84 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 85 | - * |
|
| 86 | - * @return boolean Nonce verification |
|
| 87 | - */ |
|
| 88 | - public function verify_nonce() { |
|
| 89 | - |
|
| 90 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 91 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 92 | - |
|
| 93 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 94 | - return FALSE; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Verify that the nonce is valid. |
|
| 98 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Load data from DB and store the resulting array in $this->data. |
|
| 103 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 104 | - */ |
|
| 105 | - public function get_data() { |
|
| 106 | - |
|
| 107 | - $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 108 | - |
|
| 109 | - // Values are always contained in an array (makes it easier to manage cardinality). |
|
| 110 | - if ( ! is_array( $data ) ) { |
|
| 111 | - $data = array( $data ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $this->data = $data; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Sanitizes data before saving to DB. Default sanitization trashes empty values. |
|
| 119 | - * Stores the sanitized values into $this->data so they can be later processed. |
|
| 120 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 121 | - * |
|
| 122 | - * @param array $values Array of values to be sanitized and then stored into $this->data |
|
| 123 | - * |
|
| 124 | - */ |
|
| 125 | - public function sanitize_data( $values ) { |
|
| 126 | - |
|
| 127 | - $sanitized_data = array(); |
|
| 128 | - |
|
| 129 | - if ( ! is_array( $values ) ) { |
|
| 130 | - $values = array( $values ); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - foreach ( $values as $value ) { |
|
| 134 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 135 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 136 | - $sanitized_data[] = $sanitized_value; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $this->data = $sanitized_data; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Sanitize a single value. Called from $this->sanitize_data. Default sanitization excludes empty values. |
|
| 145 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 146 | - * |
|
| 147 | - * @return mixed Returns sanitized value, or null. |
|
| 148 | - */ |
|
| 149 | - public function sanitize_data_filter( $value ) { |
|
| 150 | - |
|
| 151 | - // TODO: all fields should provide their own sanitize which shouldn't be part of a UI class. |
|
| 152 | - // If the field provides its own validation, use it. |
|
| 153 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 154 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ( ! is_null( $value ) && $value !== '' ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 158 | - return $value; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return NULL; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Save data to DB. |
|
| 166 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 167 | - */ |
|
| 168 | - public function save_data( $values ) { |
|
| 169 | - |
|
| 170 | - // Will sanitize data and store them in $field->data. |
|
| 171 | - $this->sanitize_data( $values ); |
|
| 172 | - |
|
| 173 | - $entity_id = get_the_ID(); |
|
| 174 | - |
|
| 175 | - // Take away old values. |
|
| 176 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 177 | - |
|
| 178 | - // insert new values, respecting cardinality. |
|
| 179 | - $single = ( $this->cardinality == 1 ); |
|
| 180 | - foreach ( $this->data as $value ) { |
|
| 181 | - add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns the HTML tag that will contain the Field. By default the we return a <div> with data- attributes on cardinality and expected types. |
|
| 187 | - * It is useful to provide data- attributes for the JS scripts. |
|
| 188 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 189 | - */ |
|
| 190 | - public function html_wrapper_open() { |
|
| 191 | - |
|
| 192 | - return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Returns Field HTML (nonce included). |
|
| 197 | - * Overwrite this method (or methods called from this method) in a child class to obtain custom behaviour. |
|
| 198 | - */ |
|
| 199 | - public function html() { |
|
| 200 | - |
|
| 201 | - // Open main <div> for the Field. |
|
| 202 | - $html = $this->html_wrapper_open(); |
|
| 203 | - |
|
| 204 | - // Label. |
|
| 205 | - $html .= "<h3>$this->label</h3>"; |
|
| 206 | - |
|
| 207 | - // print nonce. |
|
| 208 | - $html .= $this->html_nonce(); |
|
| 209 | - |
|
| 210 | - // print data loaded from DB. |
|
| 211 | - $count = 0; |
|
| 212 | - if ( $this->data ) { |
|
| 213 | - foreach ( $this->data as $value ) { |
|
| 214 | - if ( $count < $this->cardinality ) { |
|
| 215 | - $html .= $this->html_input( $value ); |
|
| 216 | - } |
|
| 217 | - $count ++; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // Print the empty <input> to add new values. |
|
| 222 | - if ( $count === 0 ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 223 | - $html .= $this->html_input( '' ); // Will print an empty <input> |
|
| 224 | - $count ++; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // If cardiality allows it, print button to add new values. |
|
| 228 | - if ( $count < $this->cardinality ) { |
|
| 229 | - $html .= '<button class="button wl-add-input wl-button" type="button">Add</button>'; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - // Close the HTML wrapper. |
|
| 233 | - $html .= $this->html_wrapper_close(); |
|
| 234 | - |
|
| 235 | - return $html; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Return a single <input> tag for the Field. |
|
| 240 | - * |
|
| 241 | - * @param mixed $value Input value |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - */ |
|
| 245 | - public function html_input( $value ) { |
|
| 246 | - $html = <<<EOF |
|
| 10 | + public $meta_name; |
|
| 11 | + public $raw_custom_field; |
|
| 12 | + public $predicate; |
|
| 13 | + public $label; |
|
| 14 | + public $expected_wl_type; |
|
| 15 | + public $expected_uri_type; |
|
| 16 | + public $cardinality; |
|
| 17 | + public $data; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @param array $args |
|
| 21 | + */ |
|
| 22 | + public function __construct( $args ) { |
|
| 23 | + |
|
| 24 | + if ( empty( $args ) ) { |
|
| 25 | + return; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + // Save a copy of the custom field's params. |
|
| 29 | + $this->raw_custom_field = reset( $args ); |
|
| 30 | + |
|
| 31 | + // Extract meta name (post_meta key for the DB). |
|
| 32 | + $this->meta_name = key( $args ); |
|
| 33 | + |
|
| 34 | + // Extract linked data predicate. |
|
| 35 | + if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 36 | + $this->predicate = $this->raw_custom_field['predicate']; |
|
| 37 | + } else { |
|
| 38 | + return; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + // Extract human readable label. |
|
| 42 | + $exploded_predicate = explode( '/', $this->predicate ); |
|
| 43 | + |
|
| 44 | + // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 45 | + $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 46 | + |
|
| 47 | + // Extract field constraints (numerosity, expected type). |
|
| 48 | + // Default constaints: accept one string.. |
|
| 49 | + if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 50 | + $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 51 | + } else { |
|
| 52 | + $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $this->cardinality = 1; |
|
| 56 | + if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 57 | + |
|
| 58 | + $constraints = $this->raw_custom_field['constraints']; |
|
| 59 | + |
|
| 60 | + // Extract cardinality. |
|
| 61 | + if ( isset( $constraints['cardinality'] ) ) { |
|
| 62 | + $this->cardinality = $constraints['cardinality']; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 66 | + if ( $this->expected_wl_type === Wordlift_Schema_Service::DATA_TYPE_URI && isset( $constraints['uri_type'] ) ) { |
|
| 67 | + $this->expected_uri_type = is_array( $constraints['uri_type'] ) ? $constraints['uri_type'] : array( $constraints['uri_type'] ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Return nonce HTML. |
|
| 75 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 76 | + */ |
|
| 77 | + public function html_nonce() { |
|
| 78 | + |
|
| 79 | + return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', TRUE, FALSE ); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Verify nonce. |
|
| 84 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 85 | + * |
|
| 86 | + * @return boolean Nonce verification |
|
| 87 | + */ |
|
| 88 | + public function verify_nonce() { |
|
| 89 | + |
|
| 90 | + $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 91 | + $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 92 | + |
|
| 93 | + if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 94 | + return FALSE; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Verify that the nonce is valid. |
|
| 98 | + return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Load data from DB and store the resulting array in $this->data. |
|
| 103 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 104 | + */ |
|
| 105 | + public function get_data() { |
|
| 106 | + |
|
| 107 | + $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 108 | + |
|
| 109 | + // Values are always contained in an array (makes it easier to manage cardinality). |
|
| 110 | + if ( ! is_array( $data ) ) { |
|
| 111 | + $data = array( $data ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $this->data = $data; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Sanitizes data before saving to DB. Default sanitization trashes empty values. |
|
| 119 | + * Stores the sanitized values into $this->data so they can be later processed. |
|
| 120 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 121 | + * |
|
| 122 | + * @param array $values Array of values to be sanitized and then stored into $this->data |
|
| 123 | + * |
|
| 124 | + */ |
|
| 125 | + public function sanitize_data( $values ) { |
|
| 126 | + |
|
| 127 | + $sanitized_data = array(); |
|
| 128 | + |
|
| 129 | + if ( ! is_array( $values ) ) { |
|
| 130 | + $values = array( $values ); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + foreach ( $values as $value ) { |
|
| 134 | + $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 135 | + if ( ! is_null( $sanitized_value ) ) { |
|
| 136 | + $sanitized_data[] = $sanitized_value; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $this->data = $sanitized_data; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Sanitize a single value. Called from $this->sanitize_data. Default sanitization excludes empty values. |
|
| 145 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 146 | + * |
|
| 147 | + * @return mixed Returns sanitized value, or null. |
|
| 148 | + */ |
|
| 149 | + public function sanitize_data_filter( $value ) { |
|
| 150 | + |
|
| 151 | + // TODO: all fields should provide their own sanitize which shouldn't be part of a UI class. |
|
| 152 | + // If the field provides its own validation, use it. |
|
| 153 | + if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 154 | + return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ( ! is_null( $value ) && $value !== '' ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 158 | + return $value; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return NULL; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Save data to DB. |
|
| 166 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 167 | + */ |
|
| 168 | + public function save_data( $values ) { |
|
| 169 | + |
|
| 170 | + // Will sanitize data and store them in $field->data. |
|
| 171 | + $this->sanitize_data( $values ); |
|
| 172 | + |
|
| 173 | + $entity_id = get_the_ID(); |
|
| 174 | + |
|
| 175 | + // Take away old values. |
|
| 176 | + delete_post_meta( $entity_id, $this->meta_name ); |
|
| 177 | + |
|
| 178 | + // insert new values, respecting cardinality. |
|
| 179 | + $single = ( $this->cardinality == 1 ); |
|
| 180 | + foreach ( $this->data as $value ) { |
|
| 181 | + add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns the HTML tag that will contain the Field. By default the we return a <div> with data- attributes on cardinality and expected types. |
|
| 187 | + * It is useful to provide data- attributes for the JS scripts. |
|
| 188 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 189 | + */ |
|
| 190 | + public function html_wrapper_open() { |
|
| 191 | + |
|
| 192 | + return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Returns Field HTML (nonce included). |
|
| 197 | + * Overwrite this method (or methods called from this method) in a child class to obtain custom behaviour. |
|
| 198 | + */ |
|
| 199 | + public function html() { |
|
| 200 | + |
|
| 201 | + // Open main <div> for the Field. |
|
| 202 | + $html = $this->html_wrapper_open(); |
|
| 203 | + |
|
| 204 | + // Label. |
|
| 205 | + $html .= "<h3>$this->label</h3>"; |
|
| 206 | + |
|
| 207 | + // print nonce. |
|
| 208 | + $html .= $this->html_nonce(); |
|
| 209 | + |
|
| 210 | + // print data loaded from DB. |
|
| 211 | + $count = 0; |
|
| 212 | + if ( $this->data ) { |
|
| 213 | + foreach ( $this->data as $value ) { |
|
| 214 | + if ( $count < $this->cardinality ) { |
|
| 215 | + $html .= $this->html_input( $value ); |
|
| 216 | + } |
|
| 217 | + $count ++; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // Print the empty <input> to add new values. |
|
| 222 | + if ( $count === 0 ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 223 | + $html .= $this->html_input( '' ); // Will print an empty <input> |
|
| 224 | + $count ++; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // If cardiality allows it, print button to add new values. |
|
| 228 | + if ( $count < $this->cardinality ) { |
|
| 229 | + $html .= '<button class="button wl-add-input wl-button" type="button">Add</button>'; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + // Close the HTML wrapper. |
|
| 233 | + $html .= $this->html_wrapper_close(); |
|
| 234 | + |
|
| 235 | + return $html; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Return a single <input> tag for the Field. |
|
| 240 | + * |
|
| 241 | + * @param mixed $value Input value |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + */ |
|
| 245 | + public function html_input( $value ) { |
|
| 246 | + $html = <<<EOF |
|
| 247 | 247 | <div class="wl-input-wrapper"> |
| 248 | 248 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
| 249 | 249 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 250 | 250 | </div> |
| 251 | 251 | EOF; |
| 252 | 252 | |
| 253 | - return $html; |
|
| 254 | - } |
|
| 253 | + return $html; |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * Returns closing for the wrapper HTML tag. |
|
| 258 | - */ |
|
| 259 | - public function html_wrapper_close() { |
|
| 256 | + /** |
|
| 257 | + * Returns closing for the wrapper HTML tag. |
|
| 258 | + */ |
|
| 259 | + public function html_wrapper_close() { |
|
| 260 | 260 | |
| 261 | - return '</div><hr>'; |
|
| 262 | - } |
|
| 261 | + return '</div><hr>'; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | 264 | } |
@@ -18,81 +18,81 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class WL_Metabox_Field_date extends WL_Metabox_Field { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Attribute to distinguish between date formats, inferred from the schema property export type |
|
| 23 | - * |
|
| 24 | - * @since 3.2.0 |
|
| 25 | - * @access protected |
|
| 26 | - * @var string $date_format The date format. |
|
| 27 | - */ |
|
| 28 | - protected $date_format; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Boolean flag to decide if the calendar should include time or not |
|
| 32 | - * |
|
| 33 | - * @since 3.2.0 |
|
| 34 | - * @access protected |
|
| 35 | - * @var boolean $timepicker A boolean flag. |
|
| 36 | - */ |
|
| 37 | - protected $timepicker; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Whether the calendar should be displayed or not. |
|
| 41 | - * |
|
| 42 | - * @since 3.14.0 |
|
| 43 | - * @access protected |
|
| 44 | - * @var boolean $no_calendar Whether the calendar should be displayed or not. |
|
| 45 | - */ |
|
| 46 | - protected $no_calendar; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function __construct( $args ) { |
|
| 52 | - parent::__construct( $args ); |
|
| 53 | - |
|
| 54 | - $this->no_calendar = false; |
|
| 55 | - |
|
| 56 | - // Distinguish between date and datetime |
|
| 57 | - if ( isset( $this->raw_custom_field['export_type'] ) && 'xsd:datetime' === $this->raw_custom_field['export_type'] ) { |
|
| 58 | - $this->date_format = 'Y/m/d H:i'; |
|
| 59 | - $this->timepicker = true; |
|
| 60 | - } else { |
|
| 61 | - $this->date_format = 'Y/m/d'; |
|
| 62 | - $this->timepicker = false; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param mixed $date |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function html_input( $date ) { |
|
| 73 | - |
|
| 74 | - // $picker_date = ( empty( $date ) ? '' : esc_attr( date( $this->date_format, strtotime( $date ) ) ) ); |
|
| 75 | - |
|
| 76 | - return <<<EOF |
|
| 21 | + /** |
|
| 22 | + * Attribute to distinguish between date formats, inferred from the schema property export type |
|
| 23 | + * |
|
| 24 | + * @since 3.2.0 |
|
| 25 | + * @access protected |
|
| 26 | + * @var string $date_format The date format. |
|
| 27 | + */ |
|
| 28 | + protected $date_format; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Boolean flag to decide if the calendar should include time or not |
|
| 32 | + * |
|
| 33 | + * @since 3.2.0 |
|
| 34 | + * @access protected |
|
| 35 | + * @var boolean $timepicker A boolean flag. |
|
| 36 | + */ |
|
| 37 | + protected $timepicker; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Whether the calendar should be displayed or not. |
|
| 41 | + * |
|
| 42 | + * @since 3.14.0 |
|
| 43 | + * @access protected |
|
| 44 | + * @var boolean $no_calendar Whether the calendar should be displayed or not. |
|
| 45 | + */ |
|
| 46 | + protected $no_calendar; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function __construct( $args ) { |
|
| 52 | + parent::__construct( $args ); |
|
| 53 | + |
|
| 54 | + $this->no_calendar = false; |
|
| 55 | + |
|
| 56 | + // Distinguish between date and datetime |
|
| 57 | + if ( isset( $this->raw_custom_field['export_type'] ) && 'xsd:datetime' === $this->raw_custom_field['export_type'] ) { |
|
| 58 | + $this->date_format = 'Y/m/d H:i'; |
|
| 59 | + $this->timepicker = true; |
|
| 60 | + } else { |
|
| 61 | + $this->date_format = 'Y/m/d'; |
|
| 62 | + $this->timepicker = false; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param mixed $date |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function html_input( $date ) { |
|
| 73 | + |
|
| 74 | + // $picker_date = ( empty( $date ) ? '' : esc_attr( date( $this->date_format, strtotime( $date ) ) ) ); |
|
| 75 | + |
|
| 76 | + return <<<EOF |
|
| 77 | 77 | <div class="wl-input-wrapper"> |
| 78 | 78 | <input type="text" class="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$date" style="width:88%" /> |
| 79 | 79 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 80 | 80 | </div> |
| 81 | 81 | EOF; |
| 82 | - } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - public function html_wrapper_close() { |
|
| 84 | + public function html_wrapper_close() { |
|
| 85 | 85 | |
| 86 | - // Should the widget include time picker? |
|
| 87 | - $timepicker = json_encode( $this->timepicker ); |
|
| 88 | - $date_format = json_encode( $this->date_format ); |
|
| 89 | - $no_calendar = json_encode( $this->no_calendar ); |
|
| 86 | + // Should the widget include time picker? |
|
| 87 | + $timepicker = json_encode( $this->timepicker ); |
|
| 88 | + $date_format = json_encode( $this->date_format ); |
|
| 89 | + $no_calendar = json_encode( $this->no_calendar ); |
|
| 90 | 90 | |
| 91 | - // Set up the datetimepicker. |
|
| 92 | - // |
|
| 93 | - // See https://github.com/trentrichardson/jQuery-Timepicker-Addon |
|
| 94 | - // See in http://trentrichardson.com/examples/timepicker. |
|
| 95 | - $html = <<<EOF |
|
| 91 | + // Set up the datetimepicker. |
|
| 92 | + // |
|
| 93 | + // See https://github.com/trentrichardson/jQuery-Timepicker-Addon |
|
| 94 | + // See in http://trentrichardson.com/examples/timepicker. |
|
| 95 | + $html = <<<EOF |
|
| 96 | 96 | <script type='text/javascript'> |
| 97 | 97 | ( function( $ ) { |
| 98 | 98 | |
@@ -109,9 +109,9 @@ discard block |
||
| 109 | 109 | </script> |
| 110 | 110 | EOF; |
| 111 | 111 | |
| 112 | - $html .= parent::html_wrapper_close(); |
|
| 112 | + $html .= parent::html_wrapper_close(); |
|
| 113 | 113 | |
| 114 | - return $html; |
|
| 115 | - } |
|
| 114 | + return $html; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | } |
@@ -48,13 +48,13 @@ discard block |
||
| 48 | 48 | /** |
| 49 | 49 | * {@inheritdoc} |
| 50 | 50 | */ |
| 51 | - public function __construct( $args ) { |
|
| 52 | - parent::__construct( $args ); |
|
| 51 | + public function __construct($args) { |
|
| 52 | + parent::__construct($args); |
|
| 53 | 53 | |
| 54 | 54 | $this->no_calendar = false; |
| 55 | 55 | |
| 56 | 56 | // Distinguish between date and datetime |
| 57 | - if ( isset( $this->raw_custom_field['export_type'] ) && 'xsd:datetime' === $this->raw_custom_field['export_type'] ) { |
|
| 57 | + if (isset($this->raw_custom_field['export_type']) && 'xsd:datetime' === $this->raw_custom_field['export_type']) { |
|
| 58 | 58 | $this->date_format = 'Y/m/d H:i'; |
| 59 | 59 | $this->timepicker = true; |
| 60 | 60 | } else { |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return string |
| 71 | 71 | */ |
| 72 | - public function html_input( $date ) { |
|
| 72 | + public function html_input($date) { |
|
| 73 | 73 | |
| 74 | 74 | // $picker_date = ( empty( $date ) ? '' : esc_attr( date( $this->date_format, strtotime( $date ) ) ) ); |
| 75 | 75 | |
@@ -84,9 +84,9 @@ discard block |
||
| 84 | 84 | public function html_wrapper_close() { |
| 85 | 85 | |
| 86 | 86 | // Should the widget include time picker? |
| 87 | - $timepicker = json_encode( $this->timepicker ); |
|
| 88 | - $date_format = json_encode( $this->date_format ); |
|
| 89 | - $no_calendar = json_encode( $this->no_calendar ); |
|
| 87 | + $timepicker = json_encode($this->timepicker); |
|
| 88 | + $date_format = json_encode($this->date_format); |
|
| 89 | + $no_calendar = json_encode($this->no_calendar); |
|
| 90 | 90 | |
| 91 | 91 | // Set up the datetimepicker. |
| 92 | 92 | // |
@@ -18,20 +18,20 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Metabox_Field_Multiline extends WL_Metabox_Field { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @inheritdoc |
|
| 23 | - */ |
|
| 24 | - public function html_input( $text ) { |
|
| 21 | + /** |
|
| 22 | + * @inheritdoc |
|
| 23 | + */ |
|
| 24 | + public function html_input( $text ) { |
|
| 25 | 25 | |
| 26 | - $esc_text = esc_textarea( $text ); |
|
| 27 | - $esc_meta_name = esc_attr( $this->meta_name ); |
|
| 28 | - $html = <<<EOF |
|
| 26 | + $esc_text = esc_textarea( $text ); |
|
| 27 | + $esc_meta_name = esc_attr( $this->meta_name ); |
|
| 28 | + $html = <<<EOF |
|
| 29 | 29 | <div class="wl-input-wrapper"> |
| 30 | 30 | <textarea id="$esc_meta_name" class="$esc_meta_name" name="wl_metaboxes[$esc_meta_name][]" style="width:88%">$esc_text</textarea> |
| 31 | 31 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 32 | 32 | </div> |
| 33 | 33 | EOF; |
| 34 | 34 | |
| 35 | - return $html; |
|
| 36 | - } |
|
| 35 | + return $html; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -25,295 +25,295 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class WL_Metabox { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The metabox custom fields for the current {@link WP_Post}. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - * @access public |
|
| 33 | - * @var array $fields The metabox custom fields. |
|
| 34 | - */ |
|
| 35 | - public $fields; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * WL_Metabox constructor. |
|
| 39 | - * |
|
| 40 | - * @since 3.1.0 |
|
| 41 | - */ |
|
| 42 | - public function __construct() { |
|
| 43 | - |
|
| 44 | - // Add hooks to print metaboxes and save submitted data. |
|
| 45 | - add_action( 'add_meta_boxes', array( &$this, 'add_main_metabox' ) ); |
|
| 46 | - add_action( 'wl_linked_data_save_post', array( |
|
| 47 | - &$this, |
|
| 48 | - 'save_form_data', |
|
| 49 | - ) ); |
|
| 50 | - |
|
| 51 | - // Enqueue js and css. |
|
| 52 | - $this->enqueue_scripts_and_styles(); |
|
| 53 | - |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Add a callback to print the metabox in page. |
|
| 58 | - * Wordpress will fire the $this->html() callback at the right time. |
|
| 59 | - */ |
|
| 60 | - public function add_main_metabox() { |
|
| 61 | - |
|
| 62 | - // Add main metabox (will print also the inner fields). |
|
| 63 | - $id = uniqid( 'wl-metabox-' ); |
|
| 64 | - $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 65 | - add_meta_box( $id, $title, array( |
|
| 66 | - $this, |
|
| 67 | - 'html', |
|
| 68 | - ), Wordlift_Entity_Service::TYPE_NAME, 'normal', 'high' ); |
|
| 69 | - |
|
| 70 | - // Add filter to change the metabox CSS class. |
|
| 71 | - add_filter( "postbox_classes_entity_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Called from WP to print the metabox content in page. |
|
| 76 | - * |
|
| 77 | - * @since 3.1.0 |
|
| 78 | - * |
|
| 79 | - * @param WP_Post $post The post. |
|
| 80 | - */ |
|
| 81 | - public function html( $post ) { |
|
| 82 | - |
|
| 83 | - // Build the fields we need to print. |
|
| 84 | - $this->instantiate_fields( $post->ID ); |
|
| 85 | - |
|
| 86 | - // Loop over the fields. |
|
| 87 | - foreach ( $this->fields as $field ) { |
|
| 88 | - |
|
| 89 | - // load data from DB (values will be available in $field->data). |
|
| 90 | - $field->get_data(); |
|
| 91 | - |
|
| 92 | - // print field HTML (nonce included). |
|
| 93 | - echo $field->html(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Read the WL <-> Schema mapping and build the Fields for the entity being edited. |
|
| 100 | - * |
|
| 101 | - * Note: the first function that calls this method will instantiate the fields. |
|
| 102 | - * Why it isn't called from the constructor? Because we need to hook this process as late as possible. |
|
| 103 | - * |
|
| 104 | - * @since 3.1.0 |
|
| 105 | - * |
|
| 106 | - * @param int $post_id The post id. |
|
| 107 | - */ |
|
| 108 | - public function instantiate_fields( $post_id ) { |
|
| 109 | - |
|
| 110 | - // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
|
| 111 | - if ( isset( $this->fields ) ) { |
|
| 112 | - return; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $entity_type = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 116 | - |
|
| 117 | - if ( isset( $entity_type ) ) { |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods. |
|
| 121 | - * We must divide fields in two groups: |
|
| 122 | - * - simple: accept values for one property |
|
| 123 | - * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
|
| 124 | - */ |
|
| 125 | - $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 126 | - $simple_metaboxes = $metaboxes[0]; |
|
| 127 | - $grouped_metaboxes = $metaboxes[1]; |
|
| 128 | - |
|
| 129 | - // Loop over simple entity properties. |
|
| 130 | - foreach ( $simple_metaboxes as $key => $property ) { |
|
| 131 | - |
|
| 132 | - // Info passed to the metabox. |
|
| 133 | - $info = array(); |
|
| 134 | - $info[ $key ] = $property; |
|
| 135 | - |
|
| 136 | - // Build the requested field as WL_Metabox_Field_ object. |
|
| 137 | - $this->add_field( $info ); |
|
| 138 | - |
|
| 139 | - } |
|
| 28 | + /** |
|
| 29 | + * The metabox custom fields for the current {@link WP_Post}. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + * @access public |
|
| 33 | + * @var array $fields The metabox custom fields. |
|
| 34 | + */ |
|
| 35 | + public $fields; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * WL_Metabox constructor. |
|
| 39 | + * |
|
| 40 | + * @since 3.1.0 |
|
| 41 | + */ |
|
| 42 | + public function __construct() { |
|
| 43 | + |
|
| 44 | + // Add hooks to print metaboxes and save submitted data. |
|
| 45 | + add_action( 'add_meta_boxes', array( &$this, 'add_main_metabox' ) ); |
|
| 46 | + add_action( 'wl_linked_data_save_post', array( |
|
| 47 | + &$this, |
|
| 48 | + 'save_form_data', |
|
| 49 | + ) ); |
|
| 50 | + |
|
| 51 | + // Enqueue js and css. |
|
| 52 | + $this->enqueue_scripts_and_styles(); |
|
| 53 | + |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Add a callback to print the metabox in page. |
|
| 58 | + * Wordpress will fire the $this->html() callback at the right time. |
|
| 59 | + */ |
|
| 60 | + public function add_main_metabox() { |
|
| 61 | + |
|
| 62 | + // Add main metabox (will print also the inner fields). |
|
| 63 | + $id = uniqid( 'wl-metabox-' ); |
|
| 64 | + $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 65 | + add_meta_box( $id, $title, array( |
|
| 66 | + $this, |
|
| 67 | + 'html', |
|
| 68 | + ), Wordlift_Entity_Service::TYPE_NAME, 'normal', 'high' ); |
|
| 69 | + |
|
| 70 | + // Add filter to change the metabox CSS class. |
|
| 71 | + add_filter( "postbox_classes_entity_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Called from WP to print the metabox content in page. |
|
| 76 | + * |
|
| 77 | + * @since 3.1.0 |
|
| 78 | + * |
|
| 79 | + * @param WP_Post $post The post. |
|
| 80 | + */ |
|
| 81 | + public function html( $post ) { |
|
| 82 | + |
|
| 83 | + // Build the fields we need to print. |
|
| 84 | + $this->instantiate_fields( $post->ID ); |
|
| 85 | + |
|
| 86 | + // Loop over the fields. |
|
| 87 | + foreach ( $this->fields as $field ) { |
|
| 88 | + |
|
| 89 | + // load data from DB (values will be available in $field->data). |
|
| 90 | + $field->get_data(); |
|
| 91 | + |
|
| 92 | + // print field HTML (nonce included). |
|
| 93 | + echo $field->html(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Read the WL <-> Schema mapping and build the Fields for the entity being edited. |
|
| 100 | + * |
|
| 101 | + * Note: the first function that calls this method will instantiate the fields. |
|
| 102 | + * Why it isn't called from the constructor? Because we need to hook this process as late as possible. |
|
| 103 | + * |
|
| 104 | + * @since 3.1.0 |
|
| 105 | + * |
|
| 106 | + * @param int $post_id The post id. |
|
| 107 | + */ |
|
| 108 | + public function instantiate_fields( $post_id ) { |
|
| 109 | + |
|
| 110 | + // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
|
| 111 | + if ( isset( $this->fields ) ) { |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $entity_type = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 116 | + |
|
| 117 | + if ( isset( $entity_type ) ) { |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods. |
|
| 121 | + * We must divide fields in two groups: |
|
| 122 | + * - simple: accept values for one property |
|
| 123 | + * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
|
| 124 | + */ |
|
| 125 | + $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 126 | + $simple_metaboxes = $metaboxes[0]; |
|
| 127 | + $grouped_metaboxes = $metaboxes[1]; |
|
| 128 | + |
|
| 129 | + // Loop over simple entity properties. |
|
| 130 | + foreach ( $simple_metaboxes as $key => $property ) { |
|
| 131 | + |
|
| 132 | + // Info passed to the metabox. |
|
| 133 | + $info = array(); |
|
| 134 | + $info[ $key ] = $property; |
|
| 135 | + |
|
| 136 | + // Build the requested field as WL_Metabox_Field_ object. |
|
| 137 | + $this->add_field( $info ); |
|
| 138 | + |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - // Loop over grouped properties. |
|
| 142 | - foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 141 | + // Loop over grouped properties. |
|
| 142 | + foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 143 | 143 | |
| 144 | - // Info passed to the metabox. |
|
| 145 | - $info = array(); |
|
| 146 | - $info[ $key ] = $property; |
|
| 144 | + // Info passed to the metabox. |
|
| 145 | + $info = array(); |
|
| 146 | + $info[ $key ] = $property; |
|
| 147 | 147 | |
| 148 | - // Build the requested field group as WL_Metabox_Field_ object. |
|
| 149 | - $this->add_field( $info, true ); |
|
| 148 | + // Build the requested field group as WL_Metabox_Field_ object. |
|
| 149 | + $this->add_field( $info, true ); |
|
| 150 | 150 | |
| 151 | - } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - } |
|
| 154 | - |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Separates metaboxes in simple and grouped. |
|
| 159 | - * |
|
| 160 | - * @param array $custom_fields Information on the entity type. |
|
| 161 | - * |
|
| 162 | - * @return array |
|
| 163 | - */ |
|
| 164 | - public function group_properties_by_input_field( $custom_fields ) { |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Separates metaboxes in simple and grouped. |
|
| 159 | + * |
|
| 160 | + * @param array $custom_fields Information on the entity type. |
|
| 161 | + * |
|
| 162 | + * @return array |
|
| 163 | + */ |
|
| 164 | + public function group_properties_by_input_field( $custom_fields ) { |
|
| 165 | 165 | |
| 166 | - $simple_properties = array(); |
|
| 167 | - $grouped_properties = array(); |
|
| 166 | + $simple_properties = array(); |
|
| 167 | + $grouped_properties = array(); |
|
| 168 | 168 | |
| 169 | - // Loop over possible entity properties. |
|
| 170 | - foreach ( $custom_fields as $key => $property ) { |
|
| 169 | + // Loop over possible entity properties. |
|
| 170 | + foreach ( $custom_fields as $key => $property ) { |
|
| 171 | 171 | |
| 172 | - // Check presence of predicate and type. |
|
| 173 | - if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 172 | + // Check presence of predicate and type. |
|
| 173 | + if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 174 | 174 | |
| 175 | - // Check if input_field is defined. |
|
| 176 | - if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 175 | + // Check if input_field is defined. |
|
| 176 | + if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 177 | 177 | |
| 178 | - $grouped_key = $property['input_field']; |
|
| 178 | + $grouped_key = $property['input_field']; |
|
| 179 | 179 | |
| 180 | - // Update list of grouped properties. |
|
| 181 | - $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 180 | + // Update list of grouped properties. |
|
| 181 | + $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 182 | 182 | |
| 183 | - } else { |
|
| 183 | + } else { |
|
| 184 | 184 | |
| 185 | - // input_field not defined, add simple metabox. |
|
| 186 | - $simple_properties[ $key ] = $property; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - } |
|
| 185 | + // input_field not defined, add simple metabox. |
|
| 186 | + $simple_properties[ $key ] = $property; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - return array( $simple_properties, $grouped_properties ); |
|
| 192 | - } |
|
| 191 | + return array( $simple_properties, $grouped_properties ); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Add a Field to the current Metabox, based on the description of the Field. |
|
| 196 | - * This method is a rude factory for Field objects. |
|
| 197 | - * |
|
| 198 | - * @param array $args The field's information. |
|
| 199 | - * @param bool $grouped Flag to distinguish between simple and grouped fields. |
|
| 200 | - */ |
|
| 201 | - public function add_field( $args, $grouped = false ) { |
|
| 194 | + /** |
|
| 195 | + * Add a Field to the current Metabox, based on the description of the Field. |
|
| 196 | + * This method is a rude factory for Field objects. |
|
| 197 | + * |
|
| 198 | + * @param array $args The field's information. |
|
| 199 | + * @param bool $grouped Flag to distinguish between simple and grouped fields. |
|
| 200 | + */ |
|
| 201 | + public function add_field( $args, $grouped = false ) { |
|
| 202 | 202 | |
| 203 | - if ( $grouped ) { |
|
| 203 | + if ( $grouped ) { |
|
| 204 | 204 | |
| 205 | - // Special fields (sameas, coordinates, etc.). |
|
| 206 | - // |
|
| 207 | - // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
|
| 208 | - $field_class = 'WL_Metabox_Field_' . key( $args ); |
|
| 205 | + // Special fields (sameas, coordinates, etc.). |
|
| 206 | + // |
|
| 207 | + // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
|
| 208 | + $field_class = 'WL_Metabox_Field_' . key( $args ); |
|
| 209 | 209 | |
| 210 | - } else { |
|
| 210 | + } else { |
|
| 211 | 211 | |
| 212 | - // Simple fields (string, uri, boolean, etc.). |
|
| 213 | - // |
|
| 214 | - // Which field? We want to use the class that is specific for the field. |
|
| 215 | - $meta = key( $args ); |
|
| 216 | - $this_meta = $args[ $meta ]; |
|
| 212 | + // Simple fields (string, uri, boolean, etc.). |
|
| 213 | + // |
|
| 214 | + // Which field? We want to use the class that is specific for the field. |
|
| 215 | + $meta = key( $args ); |
|
| 216 | + $this_meta = $args[ $meta ]; |
|
| 217 | 217 | |
| 218 | - // If the field declares what metabox it wants, use that one. |
|
| 219 | - if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 218 | + // If the field declares what metabox it wants, use that one. |
|
| 219 | + if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 220 | 220 | |
| 221 | - $field_class = $this_meta['metabox']['class']; |
|
| 221 | + $field_class = $this_meta['metabox']['class']; |
|
| 222 | 222 | |
| 223 | - } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 223 | + } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 224 | 224 | |
| 225 | - // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 226 | - // When they will remove this. |
|
| 227 | - // |
|
| 228 | - // Use default WL_Metabox_Field (manages strings). |
|
| 229 | - $field_class = 'WL_Metabox_Field'; |
|
| 225 | + // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 226 | + // When they will remove this. |
|
| 227 | + // |
|
| 228 | + // Use default WL_Metabox_Field (manages strings). |
|
| 229 | + $field_class = 'WL_Metabox_Field'; |
|
| 230 | 230 | |
| 231 | - } else { |
|
| 231 | + } else { |
|
| 232 | 232 | |
| 233 | - // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 234 | - // When they will remove this. |
|
| 235 | - // |
|
| 236 | - // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
|
| 237 | - $field_class = 'WL_Metabox_Field_' . $this_meta['type']; |
|
| 233 | + // TODO: all fields should explicitly declare the required WL_Metabox. |
|
| 234 | + // When they will remove this. |
|
| 235 | + // |
|
| 236 | + // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
|
| 237 | + $field_class = 'WL_Metabox_Field_' . $this_meta['type']; |
|
| 238 | 238 | |
| 239 | - } |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - } |
|
| 242 | - // End if(). |
|
| 241 | + } |
|
| 242 | + // End if(). |
|
| 243 | 243 | |
| 244 | - // Call apropriate constructor (e.g. WL_Metabox_Field_... ). |
|
| 245 | - $this->fields[] = new $field_class( $args ); |
|
| 246 | - } |
|
| 244 | + // Call apropriate constructor (e.g. WL_Metabox_Field_... ). |
|
| 245 | + $this->fields[] = new $field_class( $args ); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * Save the form data for the specified entity {@link WP_Post}'s id. |
|
| 250 | - * |
|
| 251 | - * @since 3.5.4 |
|
| 252 | - * |
|
| 253 | - * @param int $entity_id The entity's {@link WP_Post}'s id. |
|
| 254 | - */ |
|
| 255 | - public function save_form_data( $entity_id ) { |
|
| 248 | + /** |
|
| 249 | + * Save the form data for the specified entity {@link WP_Post}'s id. |
|
| 250 | + * |
|
| 251 | + * @since 3.5.4 |
|
| 252 | + * |
|
| 253 | + * @param int $entity_id The entity's {@link WP_Post}'s id. |
|
| 254 | + */ |
|
| 255 | + public function save_form_data( $entity_id ) { |
|
| 256 | 256 | |
| 257 | - // Build Field objects. |
|
| 258 | - $this->instantiate_fields( $entity_id ); |
|
| 257 | + // Build Field objects. |
|
| 258 | + $this->instantiate_fields( $entity_id ); |
|
| 259 | 259 | |
| 260 | - // Check if WL metabox form was posted. |
|
| 261 | - if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 262 | - return; |
|
| 263 | - } |
|
| 260 | + // Check if WL metabox form was posted. |
|
| 261 | + if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 262 | + return; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - foreach ( $this->fields as $field ) { |
|
| 265 | + foreach ( $this->fields as $field ) { |
|
| 266 | 266 | |
| 267 | - // Verify nonce. |
|
| 268 | - $valid_nonce = $field->verify_nonce(); |
|
| 269 | - if ( $valid_nonce ) { |
|
| 267 | + // Verify nonce. |
|
| 268 | + $valid_nonce = $field->verify_nonce(); |
|
| 269 | + if ( $valid_nonce ) { |
|
| 270 | 270 | |
| 271 | - $posted_data = $_POST['wl_metaboxes']; |
|
| 272 | - $field_name = $field->meta_name; |
|
| 271 | + $posted_data = $_POST['wl_metaboxes']; |
|
| 272 | + $field_name = $field->meta_name; |
|
| 273 | 273 | |
| 274 | - // Each Filed only deals with its values. |
|
| 275 | - if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 274 | + // Each Filed only deals with its values. |
|
| 275 | + if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 276 | 276 | |
| 277 | - $values = $posted_data[ $field_name ]; |
|
| 278 | - if ( ! is_array( $values ) ) { |
|
| 279 | - $values = array( $values ); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // Save data permanently |
|
| 283 | - $field->save_data( $values ); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - wl_linked_data_push_to_redlink( $entity_id ); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Enqueue scripts and styles. |
|
| 293 | - * |
|
| 294 | - * @since 3.0.0 |
|
| 295 | - */ |
|
| 296 | - public function enqueue_scripts_and_styles() { |
|
| 297 | - |
|
| 298 | - // Use the minified version if PW_DEBUG isn't set. |
|
| 299 | - $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 277 | + $values = $posted_data[ $field_name ]; |
|
| 278 | + if ( ! is_array( $values ) ) { |
|
| 279 | + $values = array( $values ); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // Save data permanently |
|
| 283 | + $field->save_data( $values ); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + wl_linked_data_push_to_redlink( $entity_id ); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Enqueue scripts and styles. |
|
| 293 | + * |
|
| 294 | + * @since 3.0.0 |
|
| 295 | + */ |
|
| 296 | + public function enqueue_scripts_and_styles() { |
|
| 297 | + |
|
| 298 | + // Use the minified version if PW_DEBUG isn't set. |
|
| 299 | + $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 300 | 300 | |
| 301 | - // Load the jquery-ui-timepicker-addon library. |
|
| 302 | - wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 303 | - wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 301 | + // Load the jquery-ui-timepicker-addon library. |
|
| 302 | + wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 303 | + wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 304 | 304 | |
| 305 | - // Leaflet. |
|
| 306 | - wp_enqueue_style( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.css' ); |
|
| 307 | - wp_enqueue_script( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.js', __FILE__ ); |
|
| 305 | + // Leaflet. |
|
| 306 | + wp_enqueue_style( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.css' ); |
|
| 307 | + wp_enqueue_script( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.js', __FILE__ ); |
|
| 308 | 308 | |
| 309 | - // Add AJAX autocomplete to facilitate metabox editing. |
|
| 310 | - wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' ); |
|
| 311 | - wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 312 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 313 | - 'action' => 'entity_by_title', |
|
| 314 | - ) |
|
| 315 | - ); |
|
| 309 | + // Add AJAX autocomplete to facilitate metabox editing. |
|
| 310 | + wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' ); |
|
| 311 | + wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 312 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 313 | + 'action' => 'entity_by_title', |
|
| 314 | + ) |
|
| 315 | + ); |
|
| 316 | 316 | |
| 317 | - } |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | 319 | } |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | * @subpackage Wordlift/admin/WL_Metabox |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -require_once( 'WL_Metabox_Field.php' ); |
|
| 11 | -require_once( 'WL_Metabox_Field_date.php' ); |
|
| 12 | -require_once( 'WL_Metabox_Field_uri.php' ); |
|
| 13 | -require_once( 'WL_Metabox_Field_coordinates.php' ); |
|
| 14 | -require_once( 'WL_Metabox_Field_sameas.php' ); |
|
| 15 | -require_once( 'WL_Metabox_Field_address.php' ); |
|
| 16 | -require_once( 'class-wordlift-metabox-field-duration.php' ); |
|
| 17 | -require_once( 'class-wordlift-metabox-field-multiline.php' ); |
|
| 10 | +require_once('WL_Metabox_Field.php'); |
|
| 11 | +require_once('WL_Metabox_Field_date.php'); |
|
| 12 | +require_once('WL_Metabox_Field_uri.php'); |
|
| 13 | +require_once('WL_Metabox_Field_coordinates.php'); |
|
| 14 | +require_once('WL_Metabox_Field_sameas.php'); |
|
| 15 | +require_once('WL_Metabox_Field_address.php'); |
|
| 16 | +require_once('class-wordlift-metabox-field-duration.php'); |
|
| 17 | +require_once('class-wordlift-metabox-field-multiline.php'); |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Define the {@link WL_Metabox} class. |
@@ -42,11 +42,11 @@ discard block |
||
| 42 | 42 | public function __construct() { |
| 43 | 43 | |
| 44 | 44 | // Add hooks to print metaboxes and save submitted data. |
| 45 | - add_action( 'add_meta_boxes', array( &$this, 'add_main_metabox' ) ); |
|
| 46 | - add_action( 'wl_linked_data_save_post', array( |
|
| 45 | + add_action('add_meta_boxes', array(&$this, 'add_main_metabox')); |
|
| 46 | + add_action('wl_linked_data_save_post', array( |
|
| 47 | 47 | &$this, |
| 48 | 48 | 'save_form_data', |
| 49 | - ) ); |
|
| 49 | + )); |
|
| 50 | 50 | |
| 51 | 51 | // Enqueue js and css. |
| 52 | 52 | $this->enqueue_scripts_and_styles(); |
@@ -60,15 +60,15 @@ discard block |
||
| 60 | 60 | public function add_main_metabox() { |
| 61 | 61 | |
| 62 | 62 | // Add main metabox (will print also the inner fields). |
| 63 | - $id = uniqid( 'wl-metabox-' ); |
|
| 64 | - $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 65 | - add_meta_box( $id, $title, array( |
|
| 63 | + $id = uniqid('wl-metabox-'); |
|
| 64 | + $title = get_the_title().' '.__('properties', 'wordlift'); |
|
| 65 | + add_meta_box($id, $title, array( |
|
| 66 | 66 | $this, |
| 67 | 67 | 'html', |
| 68 | - ), Wordlift_Entity_Service::TYPE_NAME, 'normal', 'high' ); |
|
| 68 | + ), Wordlift_Entity_Service::TYPE_NAME, 'normal', 'high'); |
|
| 69 | 69 | |
| 70 | 70 | // Add filter to change the metabox CSS class. |
| 71 | - add_filter( "postbox_classes_entity_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 71 | + add_filter("postbox_classes_entity_$id", 'wl_admin_metaboxes_add_css_class'); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -78,13 +78,13 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @param WP_Post $post The post. |
| 80 | 80 | */ |
| 81 | - public function html( $post ) { |
|
| 81 | + public function html($post) { |
|
| 82 | 82 | |
| 83 | 83 | // Build the fields we need to print. |
| 84 | - $this->instantiate_fields( $post->ID ); |
|
| 84 | + $this->instantiate_fields($post->ID); |
|
| 85 | 85 | |
| 86 | 86 | // Loop over the fields. |
| 87 | - foreach ( $this->fields as $field ) { |
|
| 87 | + foreach ($this->fields as $field) { |
|
| 88 | 88 | |
| 89 | 89 | // load data from DB (values will be available in $field->data). |
| 90 | 90 | $field->get_data(); |
@@ -105,16 +105,16 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @param int $post_id The post id. |
| 107 | 107 | */ |
| 108 | - public function instantiate_fields( $post_id ) { |
|
| 108 | + public function instantiate_fields($post_id) { |
|
| 109 | 109 | |
| 110 | 110 | // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
| 111 | - if ( isset( $this->fields ) ) { |
|
| 111 | + if (isset($this->fields)) { |
|
| 112 | 112 | return; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - $entity_type = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 115 | + $entity_type = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 116 | 116 | |
| 117 | - if ( isset( $entity_type ) ) { |
|
| 117 | + if (isset($entity_type)) { |
|
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods. |
@@ -122,31 +122,31 @@ discard block |
||
| 122 | 122 | * - simple: accept values for one property |
| 123 | 123 | * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
| 124 | 124 | */ |
| 125 | - $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 125 | + $metaboxes = $this->group_properties_by_input_field($entity_type); |
|
| 126 | 126 | $simple_metaboxes = $metaboxes[0]; |
| 127 | 127 | $grouped_metaboxes = $metaboxes[1]; |
| 128 | 128 | |
| 129 | 129 | // Loop over simple entity properties. |
| 130 | - foreach ( $simple_metaboxes as $key => $property ) { |
|
| 130 | + foreach ($simple_metaboxes as $key => $property) { |
|
| 131 | 131 | |
| 132 | 132 | // Info passed to the metabox. |
| 133 | 133 | $info = array(); |
| 134 | - $info[ $key ] = $property; |
|
| 134 | + $info[$key] = $property; |
|
| 135 | 135 | |
| 136 | 136 | // Build the requested field as WL_Metabox_Field_ object. |
| 137 | - $this->add_field( $info ); |
|
| 137 | + $this->add_field($info); |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | // Loop over grouped properties. |
| 142 | - foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 142 | + foreach ($grouped_metaboxes as $key => $property) { |
|
| 143 | 143 | |
| 144 | 144 | // Info passed to the metabox. |
| 145 | 145 | $info = array(); |
| 146 | - $info[ $key ] = $property; |
|
| 146 | + $info[$key] = $property; |
|
| 147 | 147 | |
| 148 | 148 | // Build the requested field group as WL_Metabox_Field_ object. |
| 149 | - $this->add_field( $info, true ); |
|
| 149 | + $this->add_field($info, true); |
|
| 150 | 150 | |
| 151 | 151 | } |
| 152 | 152 | |
@@ -161,34 +161,34 @@ discard block |
||
| 161 | 161 | * |
| 162 | 162 | * @return array |
| 163 | 163 | */ |
| 164 | - public function group_properties_by_input_field( $custom_fields ) { |
|
| 164 | + public function group_properties_by_input_field($custom_fields) { |
|
| 165 | 165 | |
| 166 | 166 | $simple_properties = array(); |
| 167 | 167 | $grouped_properties = array(); |
| 168 | 168 | |
| 169 | 169 | // Loop over possible entity properties. |
| 170 | - foreach ( $custom_fields as $key => $property ) { |
|
| 170 | + foreach ($custom_fields as $key => $property) { |
|
| 171 | 171 | |
| 172 | 172 | // Check presence of predicate and type. |
| 173 | - if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 173 | + if (isset($property['predicate']) && isset($property['type'])) { |
|
| 174 | 174 | |
| 175 | 175 | // Check if input_field is defined. |
| 176 | - if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 176 | + if (isset($property['input_field']) && '' !== $property['input_field']) { |
|
| 177 | 177 | |
| 178 | 178 | $grouped_key = $property['input_field']; |
| 179 | 179 | |
| 180 | 180 | // Update list of grouped properties. |
| 181 | - $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 181 | + $grouped_properties[$grouped_key][$key] = $property; |
|
| 182 | 182 | |
| 183 | 183 | } else { |
| 184 | 184 | |
| 185 | 185 | // input_field not defined, add simple metabox. |
| 186 | - $simple_properties[ $key ] = $property; |
|
| 186 | + $simple_properties[$key] = $property; |
|
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - return array( $simple_properties, $grouped_properties ); |
|
| 191 | + return array($simple_properties, $grouped_properties); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -198,29 +198,29 @@ discard block |
||
| 198 | 198 | * @param array $args The field's information. |
| 199 | 199 | * @param bool $grouped Flag to distinguish between simple and grouped fields. |
| 200 | 200 | */ |
| 201 | - public function add_field( $args, $grouped = false ) { |
|
| 201 | + public function add_field($args, $grouped = false) { |
|
| 202 | 202 | |
| 203 | - if ( $grouped ) { |
|
| 203 | + if ($grouped) { |
|
| 204 | 204 | |
| 205 | 205 | // Special fields (sameas, coordinates, etc.). |
| 206 | 206 | // |
| 207 | 207 | // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
| 208 | - $field_class = 'WL_Metabox_Field_' . key( $args ); |
|
| 208 | + $field_class = 'WL_Metabox_Field_'.key($args); |
|
| 209 | 209 | |
| 210 | 210 | } else { |
| 211 | 211 | |
| 212 | 212 | // Simple fields (string, uri, boolean, etc.). |
| 213 | 213 | // |
| 214 | 214 | // Which field? We want to use the class that is specific for the field. |
| 215 | - $meta = key( $args ); |
|
| 216 | - $this_meta = $args[ $meta ]; |
|
| 215 | + $meta = key($args); |
|
| 216 | + $this_meta = $args[$meta]; |
|
| 217 | 217 | |
| 218 | 218 | // If the field declares what metabox it wants, use that one. |
| 219 | - if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 219 | + if (isset($this_meta['metabox']['class'])) { |
|
| 220 | 220 | |
| 221 | 221 | $field_class = $this_meta['metabox']['class']; |
| 222 | 222 | |
| 223 | - } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 223 | + } elseif ( ! isset($this_meta['type']) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type']) { |
|
| 224 | 224 | |
| 225 | 225 | // TODO: all fields should explicitly declare the required WL_Metabox. |
| 226 | 226 | // When they will remove this. |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | // When they will remove this. |
| 235 | 235 | // |
| 236 | 236 | // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
| 237 | - $field_class = 'WL_Metabox_Field_' . $this_meta['type']; |
|
| 237 | + $field_class = 'WL_Metabox_Field_'.$this_meta['type']; |
|
| 238 | 238 | |
| 239 | 239 | } |
| 240 | 240 | |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | // End if(). |
| 243 | 243 | |
| 244 | 244 | // Call apropriate constructor (e.g. WL_Metabox_Field_... ). |
| 245 | - $this->fields[] = new $field_class( $args ); |
|
| 245 | + $this->fields[] = new $field_class($args); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** |
@@ -252,40 +252,40 @@ discard block |
||
| 252 | 252 | * |
| 253 | 253 | * @param int $entity_id The entity's {@link WP_Post}'s id. |
| 254 | 254 | */ |
| 255 | - public function save_form_data( $entity_id ) { |
|
| 255 | + public function save_form_data($entity_id) { |
|
| 256 | 256 | |
| 257 | 257 | // Build Field objects. |
| 258 | - $this->instantiate_fields( $entity_id ); |
|
| 258 | + $this->instantiate_fields($entity_id); |
|
| 259 | 259 | |
| 260 | 260 | // Check if WL metabox form was posted. |
| 261 | - if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 261 | + if ( ! isset($_POST['wl_metaboxes'])) { |
|
| 262 | 262 | return; |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - foreach ( $this->fields as $field ) { |
|
| 265 | + foreach ($this->fields as $field) { |
|
| 266 | 266 | |
| 267 | 267 | // Verify nonce. |
| 268 | 268 | $valid_nonce = $field->verify_nonce(); |
| 269 | - if ( $valid_nonce ) { |
|
| 269 | + if ($valid_nonce) { |
|
| 270 | 270 | |
| 271 | 271 | $posted_data = $_POST['wl_metaboxes']; |
| 272 | 272 | $field_name = $field->meta_name; |
| 273 | 273 | |
| 274 | 274 | // Each Filed only deals with its values. |
| 275 | - if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 275 | + if (isset($posted_data[$field_name])) { |
|
| 276 | 276 | |
| 277 | - $values = $posted_data[ $field_name ]; |
|
| 278 | - if ( ! is_array( $values ) ) { |
|
| 279 | - $values = array( $values ); |
|
| 277 | + $values = $posted_data[$field_name]; |
|
| 278 | + if ( ! is_array($values)) { |
|
| 279 | + $values = array($values); |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | // Save data permanently |
| 283 | - $field->save_data( $values ); |
|
| 283 | + $field->save_data($values); |
|
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - wl_linked_data_push_to_redlink( $entity_id ); |
|
| 288 | + wl_linked_data_push_to_redlink($entity_id); |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | /** |
@@ -296,20 +296,20 @@ discard block |
||
| 296 | 296 | public function enqueue_scripts_and_styles() { |
| 297 | 297 | |
| 298 | 298 | // Use the minified version if PW_DEBUG isn't set. |
| 299 | - $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 299 | + $min = ! defined('WP_DEBUG') || ! WP_DEBUG ? '.min' : ''; |
|
| 300 | 300 | |
| 301 | 301 | // Load the jquery-ui-timepicker-addon library. |
| 302 | - wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 303 | - wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 302 | + wp_enqueue_style('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.css", array(), '3.0.6'); |
|
| 303 | + wp_enqueue_script('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.js", array('jquery'), '3.0.6', true); |
|
| 304 | 304 | |
| 305 | 305 | // Leaflet. |
| 306 | - wp_enqueue_style( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.css' ); |
|
| 307 | - wp_enqueue_script( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.js', __FILE__ ); |
|
| 306 | + wp_enqueue_style('leaflet', dirname(dirname(plugin_dir_url(__FILE__))).'/bower_components/leaflet/dist/leaflet.css'); |
|
| 307 | + wp_enqueue_script('leaflet', dirname(dirname(plugin_dir_url(__FILE__))).'/bower_components/leaflet/dist/leaflet.js', __FILE__); |
|
| 308 | 308 | |
| 309 | 309 | // Add AJAX autocomplete to facilitate metabox editing. |
| 310 | - wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' ); |
|
| 311 | - wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 312 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 310 | + wp_enqueue_script('wl-entity-metabox-utility', dirname(plugin_dir_url(__FILE__)).'/js/wl_entity_metabox_utilities.js'); |
|
| 311 | + wp_localize_script('wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 312 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 313 | 313 | 'action' => 'entity_by_title', |
| 314 | 314 | ) |
| 315 | 315 | ); |
@@ -10,22 +10,22 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | function wl_ajax_analyze_action() { |
| 12 | 12 | |
| 13 | - try { |
|
| 14 | - if ( $analysis = wl_analyze_content( file_get_contents( "php://input" ) ) ) { |
|
| 15 | - header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
|
| 16 | - echo( $analysis ); |
|
| 17 | - wp_die(); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - status_header( 500 ); |
|
| 21 | - wp_send_json( __( 'An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift' ) ); |
|
| 22 | - |
|
| 23 | - } catch ( Exception $e ) { |
|
| 24 | - wp_send_json_error( array( |
|
| 25 | - 'code' => $e->getCode(), |
|
| 26 | - 'message' => $e->getMessage(), |
|
| 27 | - ) ); |
|
| 28 | - } |
|
| 13 | + try { |
|
| 14 | + if ( $analysis = wl_analyze_content( file_get_contents( "php://input" ) ) ) { |
|
| 15 | + header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
|
| 16 | + echo( $analysis ); |
|
| 17 | + wp_die(); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + status_header( 500 ); |
|
| 21 | + wp_send_json( __( 'An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift' ) ); |
|
| 22 | + |
|
| 23 | + } catch ( Exception $e ) { |
|
| 24 | + wp_send_json_error( array( |
|
| 25 | + 'code' => $e->getCode(), |
|
| 26 | + 'message' => $e->getMessage(), |
|
| 27 | + ) ); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | } |
| 31 | 31 | |
@@ -47,51 +47,51 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | function wl_analyze_content( $content ) { |
| 49 | 49 | |
| 50 | - // Get the analyzer URL. |
|
| 51 | - $url = wl_configuration_get_analyzer_url(); |
|
| 50 | + // Get the analyzer URL. |
|
| 51 | + $url = wl_configuration_get_analyzer_url(); |
|
| 52 | 52 | |
| 53 | - // Set the content type to the request content type or to text/plain by default. |
|
| 54 | - $content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain'; |
|
| 53 | + // Set the content type to the request content type or to text/plain by default. |
|
| 54 | + $content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain'; |
|
| 55 | 55 | |
| 56 | - // Prepare the request. |
|
| 57 | - $args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 58 | - 'method' => 'POST', |
|
| 59 | - 'headers' => array( |
|
| 60 | - 'Accept' => 'application/json', |
|
| 61 | - 'Content-type' => $content_type, |
|
| 62 | - ), |
|
| 63 | - // we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response. |
|
| 64 | - 'httpversion' => '1.0', |
|
| 65 | - 'body' => $content, |
|
| 66 | - ) ); |
|
| 56 | + // Prepare the request. |
|
| 57 | + $args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 58 | + 'method' => 'POST', |
|
| 59 | + 'headers' => array( |
|
| 60 | + 'Accept' => 'application/json', |
|
| 61 | + 'Content-type' => $content_type, |
|
| 62 | + ), |
|
| 63 | + // we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response. |
|
| 64 | + 'httpversion' => '1.0', |
|
| 65 | + 'body' => $content, |
|
| 66 | + ) ); |
|
| 67 | 67 | |
| 68 | - $response = wp_remote_post( $url, $args ); |
|
| 68 | + $response = wp_remote_post( $url, $args ); |
|
| 69 | 69 | |
| 70 | - // If it's an error log it. |
|
| 71 | - if ( is_wp_error( $response ) ) { |
|
| 70 | + // If it's an error log it. |
|
| 71 | + if ( is_wp_error( $response ) ) { |
|
| 72 | 72 | |
| 73 | - $message = "An error occurred while requesting an analysis to $url: {$response->get_error_message()}"; |
|
| 73 | + $message = "An error occurred while requesting an analysis to $url: {$response->get_error_message()}"; |
|
| 74 | 74 | |
| 75 | - Wordlift_Log_Service::get_logger( 'wl_analyze_content' )->error( $message ); |
|
| 75 | + Wordlift_Log_Service::get_logger( 'wl_analyze_content' )->error( $message ); |
|
| 76 | 76 | |
| 77 | - throw new Exception( $response->get_error_message(), is_numeric( $response->get_error_code() ) ? $response->get_error_code() : - 1 ); |
|
| 78 | - } |
|
| 77 | + throw new Exception( $response->get_error_message(), is_numeric( $response->get_error_code() ) ? $response->get_error_code() : - 1 ); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - // Get the status code. |
|
| 81 | - $status_code = (int) $response['response']['code']; |
|
| 80 | + // Get the status code. |
|
| 81 | + $status_code = (int) $response['response']['code']; |
|
| 82 | 82 | |
| 83 | - // If status code is OK, return the body. |
|
| 84 | - if ( 200 === $status_code ) { |
|
| 85 | - return $response['body']; |
|
| 86 | - } |
|
| 83 | + // If status code is OK, return the body. |
|
| 84 | + if ( 200 === $status_code ) { |
|
| 85 | + return $response['body']; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // Invalid request, e.g. invalid key. |
|
| 89 | - if ( 400 === $status_code ) { |
|
| 90 | - $error = json_decode( $response['body'] ); |
|
| 88 | + // Invalid request, e.g. invalid key. |
|
| 89 | + if ( 400 === $status_code ) { |
|
| 90 | + $error = json_decode( $response['body'] ); |
|
| 91 | 91 | |
| 92 | - throw new Exception( $error->message, $error->code ); |
|
| 93 | - } |
|
| 92 | + throw new Exception( $error->message, $error->code ); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - // Another generic error. |
|
| 96 | - throw new Exception( "An error occurred.", $status_code );; |
|
| 95 | + // Another generic error. |
|
| 96 | + throw new Exception( "An error occurred.", $status_code );; |
|
| 97 | 97 | } |
@@ -11,25 +11,25 @@ discard block |
||
| 11 | 11 | function wl_ajax_analyze_action() { |
| 12 | 12 | |
| 13 | 13 | try { |
| 14 | - if ( $analysis = wl_analyze_content( file_get_contents( "php://input" ) ) ) { |
|
| 15 | - header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
|
| 16 | - echo( $analysis ); |
|
| 14 | + if ($analysis = wl_analyze_content(file_get_contents("php://input"))) { |
|
| 15 | + header('Content-Type: application/json; charset='.get_option('blog_charset')); |
|
| 16 | + echo($analysis); |
|
| 17 | 17 | wp_die(); |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - status_header( 500 ); |
|
| 21 | - wp_send_json( __( 'An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift' ) ); |
|
| 20 | + status_header(500); |
|
| 21 | + wp_send_json(__('An error occurred while request an analysis to the remote service. Please try again later.', 'wordlift')); |
|
| 22 | 22 | |
| 23 | - } catch ( Exception $e ) { |
|
| 24 | - wp_send_json_error( array( |
|
| 23 | + } catch (Exception $e) { |
|
| 24 | + wp_send_json_error(array( |
|
| 25 | 25 | 'code' => $e->getCode(), |
| 26 | 26 | 'message' => $e->getMessage(), |
| 27 | - ) ); |
|
| 27 | + )); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | -add_action( 'wp_ajax_wordlift_analyze', 'wl_ajax_analyze_action' ); |
|
| 32 | +add_action('wp_ajax_wordlift_analyze', 'wl_ajax_analyze_action'); |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Analyze the provided content. The analysis will make use of the method *wl_ajax_analyze_action* |
@@ -45,16 +45,16 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @throws Exception |
| 47 | 47 | */ |
| 48 | -function wl_analyze_content( $content ) { |
|
| 48 | +function wl_analyze_content($content) { |
|
| 49 | 49 | |
| 50 | 50 | // Get the analyzer URL. |
| 51 | 51 | $url = wl_configuration_get_analyzer_url(); |
| 52 | 52 | |
| 53 | 53 | // Set the content type to the request content type or to text/plain by default. |
| 54 | - $content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain'; |
|
| 54 | + $content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : 'text/plain'; |
|
| 55 | 55 | |
| 56 | 56 | // Prepare the request. |
| 57 | - $args = array_merge_recursive( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 57 | + $args = array_merge_recursive(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array( |
|
| 58 | 58 | 'method' => 'POST', |
| 59 | 59 | 'headers' => array( |
| 60 | 60 | 'Accept' => 'application/json', |
@@ -63,35 +63,35 @@ discard block |
||
| 63 | 63 | // we need to downgrade the HTTP version in this case since chunked encoding is dumping numbers in the response. |
| 64 | 64 | 'httpversion' => '1.0', |
| 65 | 65 | 'body' => $content, |
| 66 | - ) ); |
|
| 66 | + )); |
|
| 67 | 67 | |
| 68 | - $response = wp_remote_post( $url, $args ); |
|
| 68 | + $response = wp_remote_post($url, $args); |
|
| 69 | 69 | |
| 70 | 70 | // If it's an error log it. |
| 71 | - if ( is_wp_error( $response ) ) { |
|
| 71 | + if (is_wp_error($response)) { |
|
| 72 | 72 | |
| 73 | 73 | $message = "An error occurred while requesting an analysis to $url: {$response->get_error_message()}"; |
| 74 | 74 | |
| 75 | - Wordlift_Log_Service::get_logger( 'wl_analyze_content' )->error( $message ); |
|
| 75 | + Wordlift_Log_Service::get_logger('wl_analyze_content')->error($message); |
|
| 76 | 76 | |
| 77 | - throw new Exception( $response->get_error_message(), is_numeric( $response->get_error_code() ) ? $response->get_error_code() : - 1 ); |
|
| 77 | + throw new Exception($response->get_error_message(), is_numeric($response->get_error_code()) ? $response->get_error_code() : - 1); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // Get the status code. |
| 81 | 81 | $status_code = (int) $response['response']['code']; |
| 82 | 82 | |
| 83 | 83 | // If status code is OK, return the body. |
| 84 | - if ( 200 === $status_code ) { |
|
| 84 | + if (200 === $status_code) { |
|
| 85 | 85 | return $response['body']; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // Invalid request, e.g. invalid key. |
| 89 | - if ( 400 === $status_code ) { |
|
| 90 | - $error = json_decode( $response['body'] ); |
|
| 89 | + if (400 === $status_code) { |
|
| 90 | + $error = json_decode($response['body']); |
|
| 91 | 91 | |
| 92 | - throw new Exception( $error->message, $error->code ); |
|
| 92 | + throw new Exception($error->message, $error->code); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Another generic error. |
| 96 | - throw new Exception( "An error occurred.", $status_code );; |
|
| 96 | + throw new Exception("An error occurred.", $status_code); ; |
|
| 97 | 97 | } |
@@ -10,436 +10,436 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Wordlift_User_Service { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * The meta key where the user's URI is stored. |
|
| 15 | - * |
|
| 16 | - * @since 3.1.7 |
|
| 17 | - */ |
|
| 18 | - const URI_META_KEY = '_wl_uri'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The user meta key where the deny entity edit flag is stored. |
|
| 22 | - * |
|
| 23 | - * @since 3.14.0 |
|
| 24 | - */ |
|
| 25 | - const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The meta key holding the entity id representing a {@link WP_User}. |
|
| 29 | - * |
|
| 30 | - * @since 3.14.0 |
|
| 31 | - */ |
|
| 32 | - const ENTITY_META_KEY = '_wl_entity'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The Log service. |
|
| 36 | - * |
|
| 37 | - * @since 3.1.7 |
|
| 38 | - * @access private |
|
| 39 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 40 | - */ |
|
| 41 | - private $log_service; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The singleton instance of the User service. |
|
| 45 | - * |
|
| 46 | - * @since 3.1.7 |
|
| 47 | - * @access private |
|
| 48 | - * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
| 49 | - */ |
|
| 50 | - private static $instance; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Create an instance of the User service. |
|
| 54 | - * |
|
| 55 | - * @since 3.1.7 |
|
| 56 | - */ |
|
| 57 | - public function __construct() { |
|
| 58 | - |
|
| 59 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 60 | - |
|
| 61 | - self::$instance = $this; |
|
| 62 | - |
|
| 63 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the singleton instance of the User service. |
|
| 68 | - * |
|
| 69 | - * @since 3.1.7 |
|
| 70 | - * @return \Wordlift_User_Service The singleton instance of the User service. |
|
| 71 | - */ |
|
| 72 | - public static function get_instance() { |
|
| 73 | - |
|
| 74 | - return self::$instance; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the URI for a user. |
|
| 79 | - * |
|
| 80 | - * @since 3.1.7 |
|
| 81 | - * |
|
| 82 | - * @param int $user_id The user id |
|
| 83 | - * |
|
| 84 | - * @return false|string The user's URI or false in case of failure. |
|
| 85 | - */ |
|
| 86 | - public function get_uri( $user_id ) { |
|
| 87 | - |
|
| 88 | - // Try to get the URI stored in the user's meta and return it if available. |
|
| 89 | - if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 90 | - return $user_uri; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // Try to build an URI, return false in case of failure. |
|
| 94 | - if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // Store the URI for future requests (we need a "permanent" URI). |
|
| 99 | - $this->_set_uri( $user_id, $user_uri ); |
|
| 100 | - |
|
| 101 | - return $user_uri; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Receives wp_insert_post events. |
|
| 106 | - * |
|
| 107 | - * @since 3.1.7 |
|
| 108 | - * |
|
| 109 | - * @param int $post_id Post ID. |
|
| 110 | - * @param WP_Post $post Post object. |
|
| 111 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 112 | - */ |
|
| 113 | - public function wp_insert_post( $post_id, $post, $update ) { |
|
| 114 | - |
|
| 115 | - // If the post is not published, return. |
|
| 116 | - if ( 'publish' !== get_post_status( $post_id ) ) { |
|
| 117 | - return; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // We expect a numeric author id. |
|
| 121 | - if ( ! is_numeric( $post->post_author ) ) { |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Get the delete query,or return in case of failure. |
|
| 126 | - if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
| 127 | - return; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // Get the insert query,or return in case of failure. |
|
| 131 | - if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
| 132 | - return; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Send the query to the triple store. |
|
| 136 | - rl_execute_sparql_update_query( $delete . $insert ); |
|
| 137 | - |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Set the `id` of the entity representing a {@link WP_User}. |
|
| 142 | - * |
|
| 143 | - * If the `id` is set to 0 (or less) then the meta is deleted. |
|
| 144 | - * |
|
| 145 | - * @since 3.14.0 |
|
| 146 | - * |
|
| 147 | - * @param int $user_id The {@link WP_User}. |
|
| 148 | - * @param int $value The entity {@link WP_Post} `id`. |
|
| 149 | - * |
|
| 150 | - * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 151 | - */ |
|
| 152 | - public function set_entity( $user_id, $value ) { |
|
| 153 | - |
|
| 154 | - return 0 < $value |
|
| 155 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 156 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
| 161 | - * |
|
| 162 | - * @since 3.14.0 |
|
| 163 | - * |
|
| 164 | - * @param int $user_id The {@link WP_User}'s `id`. |
|
| 165 | - * |
|
| 166 | - * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
|
| 167 | - */ |
|
| 168 | - public function get_entity( $user_id ) { |
|
| 169 | - |
|
| 170 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Get the user's URI stored in the user's meta. |
|
| 175 | - * |
|
| 176 | - * @since 3.1.7 |
|
| 177 | - * |
|
| 178 | - * @param int $user_id The user id. |
|
| 179 | - * |
|
| 180 | - * @return false|string The user's URI or false if not found. |
|
| 181 | - */ |
|
| 182 | - private function _get_uri( $user_id ) { |
|
| 183 | - |
|
| 184 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 185 | - |
|
| 186 | - if ( empty( $user_uri ) ) { |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $user_uri; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Build an URI for a user. |
|
| 195 | - * |
|
| 196 | - * @since 3.1.7 |
|
| 197 | - * |
|
| 198 | - * @param int $user_id The user's id. |
|
| 199 | - * |
|
| 200 | - * @return false|string The user's URI or false in case of failure. |
|
| 201 | - */ |
|
| 202 | - private function _build_uri( $user_id ) { |
|
| 203 | - |
|
| 204 | - // Get the user, return false in case of failure. |
|
| 205 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 206 | - return false; |
|
| 207 | - }; |
|
| 208 | - |
|
| 209 | - // If the nicename is not set, return a failure. |
|
| 210 | - if ( empty( $user->user_nicename ) ) { |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Store the URI in user's meta. |
|
| 219 | - * |
|
| 220 | - * @since 3.1.7 |
|
| 221 | - * |
|
| 222 | - * @param int $user_id The user's id. |
|
| 223 | - * @param string $user_uri The user's uri. |
|
| 224 | - * |
|
| 225 | - * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 226 | - */ |
|
| 227 | - private function _set_uri( $user_id, $user_uri ) { |
|
| 228 | - |
|
| 229 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Get the delete query. |
|
| 234 | - * |
|
| 235 | - * @since 3.1.7 |
|
| 236 | - * |
|
| 237 | - * @param int $user_id The user id. |
|
| 238 | - * |
|
| 239 | - * @return false|string The delete query or false in case of failure. |
|
| 240 | - */ |
|
| 241 | - private function get_delete_query( $user_id ) { |
|
| 242 | - |
|
| 243 | - // Get the URI, return if there's none. |
|
| 244 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Build the delete query. |
|
| 249 | - $query = Wordlift_Query_Builder::new_instance()->delete() |
|
| 250 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 251 | - ->build() |
|
| 252 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 253 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
| 254 | - ->build() |
|
| 255 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 256 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
| 257 | - ->build() |
|
| 258 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 259 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
| 260 | - ->build() |
|
| 261 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 262 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 263 | - ->build(); |
|
| 264 | - |
|
| 265 | - return $query; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get the insert query. |
|
| 270 | - * |
|
| 271 | - * @since 3.1.7 |
|
| 272 | - * |
|
| 273 | - * @param int $user_id The user id. |
|
| 274 | - * |
|
| 275 | - * @return false|string The insert query or false in case of failure. |
|
| 276 | - */ |
|
| 277 | - private function get_insert_query( $user_id ) { |
|
| 278 | - |
|
| 279 | - // Get the URI, return if there's none. |
|
| 280 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - // Try to get the user data, in case of failure return false. |
|
| 285 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 286 | - return false; |
|
| 287 | - }; |
|
| 288 | - |
|
| 289 | - // Build the insert query. |
|
| 290 | - $query = Wordlift_Query_Builder::new_instance() |
|
| 291 | - ->insert() |
|
| 292 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
| 293 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
| 294 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
| 295 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
| 296 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
| 297 | - ->build(); |
|
| 298 | - |
|
| 299 | - return $query; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Mark an editor user as denied from editing entities. |
|
| 304 | - * Does nothing if the user is not an editor |
|
| 305 | - * |
|
| 306 | - * @since 3.14.0 |
|
| 307 | - * |
|
| 308 | - * @param integer $user_id The ID of the user |
|
| 309 | - */ |
|
| 310 | - public function deny_editor_entity_create( $user_id ) { |
|
| 311 | - |
|
| 312 | - // Bail out if the user is not an editor. |
|
| 313 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 314 | - return; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // The user explicitly do not have the capability. |
|
| 318 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 319 | - |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Remove the "deny entity editing" mark from an editor user. |
|
| 324 | - * Does nothing if the user is not an editor |
|
| 325 | - * |
|
| 326 | - * @since 3.14.0 |
|
| 327 | - * |
|
| 328 | - * @param integer $user_id The ID of the user |
|
| 329 | - */ |
|
| 330 | - public function allow_editor_entity_create( $user_id ) { |
|
| 331 | - |
|
| 332 | - // Bail out if the user is not an editor. |
|
| 333 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 334 | - return; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // The user explicitly do not have the capability. |
|
| 338 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 339 | - |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Get whether the 'deny editor entity editing' flag is set. |
|
| 344 | - * |
|
| 345 | - * @since 3.14.0 |
|
| 346 | - * |
|
| 347 | - * @param int $user_id The {@link WP_User} `id`. |
|
| 348 | - * |
|
| 349 | - * @return int bool True if editing is denied otherwise false. |
|
| 350 | - */ |
|
| 351 | - public function is_deny_editor_entity_create( $user_id ) { |
|
| 352 | - |
|
| 353 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
| 358 | - * i.e. has the `editor` role. |
|
| 359 | - * |
|
| 360 | - * @since 3.14.0 |
|
| 361 | - * |
|
| 362 | - * @param int $user_id The {@link WP_User} `id`. |
|
| 363 | - * |
|
| 364 | - * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
| 365 | - */ |
|
| 366 | - public function is_editor( $user_id ) { |
|
| 367 | - |
|
| 368 | - // Get the user. |
|
| 369 | - $user = get_user_by( 'id', $user_id ); |
|
| 370 | - |
|
| 371 | - // Return true, if the user is found and has the `editor` role. |
|
| 372 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Check if an editor can create entities. |
|
| 377 | - * |
|
| 378 | - * @since 3.14.0 |
|
| 379 | - * |
|
| 380 | - * @param int $user_id The user id of the user being checked. |
|
| 381 | - * |
|
| 382 | - * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
| 383 | - */ |
|
| 384 | - public function editor_can_create_entities( $user_id ) { |
|
| 385 | - |
|
| 386 | - // Return true if not an editor. |
|
| 387 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 388 | - return true; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // Check if the user explicitly denied. |
|
| 392 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Filter capabilities of user. |
|
| 397 | - * |
|
| 398 | - * Deny the capability of managing and editing entities for some users. |
|
| 399 | - * |
|
| 400 | - * @since 3.14.0 |
|
| 401 | - * |
|
| 402 | - * @param array $allcaps All the capabilities of the user |
|
| 403 | - * @param array $cap [0] Required capability |
|
| 404 | - * @param array $args [0] Requested capability |
|
| 405 | - * [1] User ID |
|
| 406 | - * [2] Associated object ID |
|
| 407 | - * |
|
| 408 | - * @return array The capabilities array. |
|
| 409 | - */ |
|
| 410 | - public function has_cap( $allcaps, $cap, $args ) { |
|
| 411 | - /* |
|
| 13 | + /** |
|
| 14 | + * The meta key where the user's URI is stored. |
|
| 15 | + * |
|
| 16 | + * @since 3.1.7 |
|
| 17 | + */ |
|
| 18 | + const URI_META_KEY = '_wl_uri'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The user meta key where the deny entity edit flag is stored. |
|
| 22 | + * |
|
| 23 | + * @since 3.14.0 |
|
| 24 | + */ |
|
| 25 | + const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The meta key holding the entity id representing a {@link WP_User}. |
|
| 29 | + * |
|
| 30 | + * @since 3.14.0 |
|
| 31 | + */ |
|
| 32 | + const ENTITY_META_KEY = '_wl_entity'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The Log service. |
|
| 36 | + * |
|
| 37 | + * @since 3.1.7 |
|
| 38 | + * @access private |
|
| 39 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 40 | + */ |
|
| 41 | + private $log_service; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The singleton instance of the User service. |
|
| 45 | + * |
|
| 46 | + * @since 3.1.7 |
|
| 47 | + * @access private |
|
| 48 | + * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
| 49 | + */ |
|
| 50 | + private static $instance; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Create an instance of the User service. |
|
| 54 | + * |
|
| 55 | + * @since 3.1.7 |
|
| 56 | + */ |
|
| 57 | + public function __construct() { |
|
| 58 | + |
|
| 59 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 60 | + |
|
| 61 | + self::$instance = $this; |
|
| 62 | + |
|
| 63 | + add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the singleton instance of the User service. |
|
| 68 | + * |
|
| 69 | + * @since 3.1.7 |
|
| 70 | + * @return \Wordlift_User_Service The singleton instance of the User service. |
|
| 71 | + */ |
|
| 72 | + public static function get_instance() { |
|
| 73 | + |
|
| 74 | + return self::$instance; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the URI for a user. |
|
| 79 | + * |
|
| 80 | + * @since 3.1.7 |
|
| 81 | + * |
|
| 82 | + * @param int $user_id The user id |
|
| 83 | + * |
|
| 84 | + * @return false|string The user's URI or false in case of failure. |
|
| 85 | + */ |
|
| 86 | + public function get_uri( $user_id ) { |
|
| 87 | + |
|
| 88 | + // Try to get the URI stored in the user's meta and return it if available. |
|
| 89 | + if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 90 | + return $user_uri; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // Try to build an URI, return false in case of failure. |
|
| 94 | + if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // Store the URI for future requests (we need a "permanent" URI). |
|
| 99 | + $this->_set_uri( $user_id, $user_uri ); |
|
| 100 | + |
|
| 101 | + return $user_uri; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Receives wp_insert_post events. |
|
| 106 | + * |
|
| 107 | + * @since 3.1.7 |
|
| 108 | + * |
|
| 109 | + * @param int $post_id Post ID. |
|
| 110 | + * @param WP_Post $post Post object. |
|
| 111 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 112 | + */ |
|
| 113 | + public function wp_insert_post( $post_id, $post, $update ) { |
|
| 114 | + |
|
| 115 | + // If the post is not published, return. |
|
| 116 | + if ( 'publish' !== get_post_status( $post_id ) ) { |
|
| 117 | + return; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // We expect a numeric author id. |
|
| 121 | + if ( ! is_numeric( $post->post_author ) ) { |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Get the delete query,or return in case of failure. |
|
| 126 | + if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // Get the insert query,or return in case of failure. |
|
| 131 | + if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Send the query to the triple store. |
|
| 136 | + rl_execute_sparql_update_query( $delete . $insert ); |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Set the `id` of the entity representing a {@link WP_User}. |
|
| 142 | + * |
|
| 143 | + * If the `id` is set to 0 (or less) then the meta is deleted. |
|
| 144 | + * |
|
| 145 | + * @since 3.14.0 |
|
| 146 | + * |
|
| 147 | + * @param int $user_id The {@link WP_User}. |
|
| 148 | + * @param int $value The entity {@link WP_Post} `id`. |
|
| 149 | + * |
|
| 150 | + * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 151 | + */ |
|
| 152 | + public function set_entity( $user_id, $value ) { |
|
| 153 | + |
|
| 154 | + return 0 < $value |
|
| 155 | + ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 156 | + : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
| 161 | + * |
|
| 162 | + * @since 3.14.0 |
|
| 163 | + * |
|
| 164 | + * @param int $user_id The {@link WP_User}'s `id`. |
|
| 165 | + * |
|
| 166 | + * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
|
| 167 | + */ |
|
| 168 | + public function get_entity( $user_id ) { |
|
| 169 | + |
|
| 170 | + return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Get the user's URI stored in the user's meta. |
|
| 175 | + * |
|
| 176 | + * @since 3.1.7 |
|
| 177 | + * |
|
| 178 | + * @param int $user_id The user id. |
|
| 179 | + * |
|
| 180 | + * @return false|string The user's URI or false if not found. |
|
| 181 | + */ |
|
| 182 | + private function _get_uri( $user_id ) { |
|
| 183 | + |
|
| 184 | + $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 185 | + |
|
| 186 | + if ( empty( $user_uri ) ) { |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $user_uri; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Build an URI for a user. |
|
| 195 | + * |
|
| 196 | + * @since 3.1.7 |
|
| 197 | + * |
|
| 198 | + * @param int $user_id The user's id. |
|
| 199 | + * |
|
| 200 | + * @return false|string The user's URI or false in case of failure. |
|
| 201 | + */ |
|
| 202 | + private function _build_uri( $user_id ) { |
|
| 203 | + |
|
| 204 | + // Get the user, return false in case of failure. |
|
| 205 | + if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 206 | + return false; |
|
| 207 | + }; |
|
| 208 | + |
|
| 209 | + // If the nicename is not set, return a failure. |
|
| 210 | + if ( empty( $user->user_nicename ) ) { |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Store the URI in user's meta. |
|
| 219 | + * |
|
| 220 | + * @since 3.1.7 |
|
| 221 | + * |
|
| 222 | + * @param int $user_id The user's id. |
|
| 223 | + * @param string $user_uri The user's uri. |
|
| 224 | + * |
|
| 225 | + * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 226 | + */ |
|
| 227 | + private function _set_uri( $user_id, $user_uri ) { |
|
| 228 | + |
|
| 229 | + return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Get the delete query. |
|
| 234 | + * |
|
| 235 | + * @since 3.1.7 |
|
| 236 | + * |
|
| 237 | + * @param int $user_id The user id. |
|
| 238 | + * |
|
| 239 | + * @return false|string The delete query or false in case of failure. |
|
| 240 | + */ |
|
| 241 | + private function get_delete_query( $user_id ) { |
|
| 242 | + |
|
| 243 | + // Get the URI, return if there's none. |
|
| 244 | + if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Build the delete query. |
|
| 249 | + $query = Wordlift_Query_Builder::new_instance()->delete() |
|
| 250 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 251 | + ->build() |
|
| 252 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 253 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
| 254 | + ->build() |
|
| 255 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 256 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
| 257 | + ->build() |
|
| 258 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 259 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
| 260 | + ->build() |
|
| 261 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 262 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 263 | + ->build(); |
|
| 264 | + |
|
| 265 | + return $query; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get the insert query. |
|
| 270 | + * |
|
| 271 | + * @since 3.1.7 |
|
| 272 | + * |
|
| 273 | + * @param int $user_id The user id. |
|
| 274 | + * |
|
| 275 | + * @return false|string The insert query or false in case of failure. |
|
| 276 | + */ |
|
| 277 | + private function get_insert_query( $user_id ) { |
|
| 278 | + |
|
| 279 | + // Get the URI, return if there's none. |
|
| 280 | + if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + // Try to get the user data, in case of failure return false. |
|
| 285 | + if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 286 | + return false; |
|
| 287 | + }; |
|
| 288 | + |
|
| 289 | + // Build the insert query. |
|
| 290 | + $query = Wordlift_Query_Builder::new_instance() |
|
| 291 | + ->insert() |
|
| 292 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
| 293 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
| 294 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
| 295 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
| 296 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
| 297 | + ->build(); |
|
| 298 | + |
|
| 299 | + return $query; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Mark an editor user as denied from editing entities. |
|
| 304 | + * Does nothing if the user is not an editor |
|
| 305 | + * |
|
| 306 | + * @since 3.14.0 |
|
| 307 | + * |
|
| 308 | + * @param integer $user_id The ID of the user |
|
| 309 | + */ |
|
| 310 | + public function deny_editor_entity_create( $user_id ) { |
|
| 311 | + |
|
| 312 | + // Bail out if the user is not an editor. |
|
| 313 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 314 | + return; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // The user explicitly do not have the capability. |
|
| 318 | + update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 319 | + |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Remove the "deny entity editing" mark from an editor user. |
|
| 324 | + * Does nothing if the user is not an editor |
|
| 325 | + * |
|
| 326 | + * @since 3.14.0 |
|
| 327 | + * |
|
| 328 | + * @param integer $user_id The ID of the user |
|
| 329 | + */ |
|
| 330 | + public function allow_editor_entity_create( $user_id ) { |
|
| 331 | + |
|
| 332 | + // Bail out if the user is not an editor. |
|
| 333 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 334 | + return; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // The user explicitly do not have the capability. |
|
| 338 | + delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 339 | + |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Get whether the 'deny editor entity editing' flag is set. |
|
| 344 | + * |
|
| 345 | + * @since 3.14.0 |
|
| 346 | + * |
|
| 347 | + * @param int $user_id The {@link WP_User} `id`. |
|
| 348 | + * |
|
| 349 | + * @return int bool True if editing is denied otherwise false. |
|
| 350 | + */ |
|
| 351 | + public function is_deny_editor_entity_create( $user_id ) { |
|
| 352 | + |
|
| 353 | + return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
| 358 | + * i.e. has the `editor` role. |
|
| 359 | + * |
|
| 360 | + * @since 3.14.0 |
|
| 361 | + * |
|
| 362 | + * @param int $user_id The {@link WP_User} `id`. |
|
| 363 | + * |
|
| 364 | + * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
| 365 | + */ |
|
| 366 | + public function is_editor( $user_id ) { |
|
| 367 | + |
|
| 368 | + // Get the user. |
|
| 369 | + $user = get_user_by( 'id', $user_id ); |
|
| 370 | + |
|
| 371 | + // Return true, if the user is found and has the `editor` role. |
|
| 372 | + return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Check if an editor can create entities. |
|
| 377 | + * |
|
| 378 | + * @since 3.14.0 |
|
| 379 | + * |
|
| 380 | + * @param int $user_id The user id of the user being checked. |
|
| 381 | + * |
|
| 382 | + * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
| 383 | + */ |
|
| 384 | + public function editor_can_create_entities( $user_id ) { |
|
| 385 | + |
|
| 386 | + // Return true if not an editor. |
|
| 387 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 388 | + return true; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // Check if the user explicitly denied. |
|
| 392 | + return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Filter capabilities of user. |
|
| 397 | + * |
|
| 398 | + * Deny the capability of managing and editing entities for some users. |
|
| 399 | + * |
|
| 400 | + * @since 3.14.0 |
|
| 401 | + * |
|
| 402 | + * @param array $allcaps All the capabilities of the user |
|
| 403 | + * @param array $cap [0] Required capability |
|
| 404 | + * @param array $args [0] Requested capability |
|
| 405 | + * [1] User ID |
|
| 406 | + * [2] Associated object ID |
|
| 407 | + * |
|
| 408 | + * @return array The capabilities array. |
|
| 409 | + */ |
|
| 410 | + public function has_cap( $allcaps, $cap, $args ) { |
|
| 411 | + /* |
|
| 412 | 412 | * For entity management/editing related capabilities |
| 413 | 413 | * check that an editor was not explicitly denied (in user profile) |
| 414 | 414 | * the capability. |
| 415 | 415 | */ |
| 416 | 416 | |
| 417 | - /* |
|
| 417 | + /* |
|
| 418 | 418 | * Need protection against the case of edit_user and likes which do not |
| 419 | 419 | * require a capability, just request one. |
| 420 | 420 | */ |
| 421 | - if ( empty( $cap ) ) { |
|
| 422 | - return $allcaps; |
|
| 423 | - } |
|
| 424 | - if ( |
|
| 425 | - ( 'edit_wordlift_entity' == $cap[0] ) || |
|
| 426 | - ( 'edit_wordlift_entities' == $cap[0] ) || |
|
| 427 | - ( 'edit_others_wordlift_entities' == $cap[0] ) || |
|
| 428 | - ( 'publish_wordlift_entities' == $cap[0] ) || |
|
| 429 | - ( 'read_private_wordlift_entities' == $cap[0] ) || |
|
| 430 | - ( 'delete_wordlift_entity' == $cap[0] ) || |
|
| 431 | - ( 'delete_wordlift_entities' == $cap[0] ) || |
|
| 432 | - ( 'delete_others_wordlift_entities' == $cap[0] ) || |
|
| 433 | - ( 'delete_published_wordlift_entities' == $cap[0] ) || |
|
| 434 | - ( 'delete_private_wordlift_entities' == $cap[0] ) |
|
| 435 | - ) { |
|
| 436 | - $user_id = $args[1]; |
|
| 437 | - |
|
| 438 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 439 | - $allcaps[ $cap[0] ] = false; |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - return $allcaps; |
|
| 444 | - } |
|
| 421 | + if ( empty( $cap ) ) { |
|
| 422 | + return $allcaps; |
|
| 423 | + } |
|
| 424 | + if ( |
|
| 425 | + ( 'edit_wordlift_entity' == $cap[0] ) || |
|
| 426 | + ( 'edit_wordlift_entities' == $cap[0] ) || |
|
| 427 | + ( 'edit_others_wordlift_entities' == $cap[0] ) || |
|
| 428 | + ( 'publish_wordlift_entities' == $cap[0] ) || |
|
| 429 | + ( 'read_private_wordlift_entities' == $cap[0] ) || |
|
| 430 | + ( 'delete_wordlift_entity' == $cap[0] ) || |
|
| 431 | + ( 'delete_wordlift_entities' == $cap[0] ) || |
|
| 432 | + ( 'delete_others_wordlift_entities' == $cap[0] ) || |
|
| 433 | + ( 'delete_published_wordlift_entities' == $cap[0] ) || |
|
| 434 | + ( 'delete_private_wordlift_entities' == $cap[0] ) |
|
| 435 | + ) { |
|
| 436 | + $user_id = $args[1]; |
|
| 437 | + |
|
| 438 | + if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 439 | + $allcaps[ $cap[0] ] = false; |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + return $allcaps; |
|
| 444 | + } |
|
| 445 | 445 | } |
@@ -56,11 +56,11 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function __construct() { |
| 58 | 58 | |
| 59 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 59 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_User_Service'); |
|
| 60 | 60 | |
| 61 | 61 | self::$instance = $this; |
| 62 | 62 | |
| 63 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 63 | + add_filter('user_has_cap', array($this, 'has_cap'), 10, 3); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -83,20 +83,20 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return false|string The user's URI or false in case of failure. |
| 85 | 85 | */ |
| 86 | - public function get_uri( $user_id ) { |
|
| 86 | + public function get_uri($user_id) { |
|
| 87 | 87 | |
| 88 | 88 | // Try to get the URI stored in the user's meta and return it if available. |
| 89 | - if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 89 | + if (false !== ($user_uri = $this->_get_uri($user_id))) { |
|
| 90 | 90 | return $user_uri; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | // Try to build an URI, return false in case of failure. |
| 94 | - if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 94 | + if (false === ($user_uri = $this->_build_uri($user_id))) { |
|
| 95 | 95 | return false; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // Store the URI for future requests (we need a "permanent" URI). |
| 99 | - $this->_set_uri( $user_id, $user_uri ); |
|
| 99 | + $this->_set_uri($user_id, $user_uri); |
|
| 100 | 100 | |
| 101 | 101 | return $user_uri; |
| 102 | 102 | } |
@@ -110,30 +110,30 @@ discard block |
||
| 110 | 110 | * @param WP_Post $post Post object. |
| 111 | 111 | * @param bool $update Whether this is an existing post being updated or not. |
| 112 | 112 | */ |
| 113 | - public function wp_insert_post( $post_id, $post, $update ) { |
|
| 113 | + public function wp_insert_post($post_id, $post, $update) { |
|
| 114 | 114 | |
| 115 | 115 | // If the post is not published, return. |
| 116 | - if ( 'publish' !== get_post_status( $post_id ) ) { |
|
| 116 | + if ('publish' !== get_post_status($post_id)) { |
|
| 117 | 117 | return; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // We expect a numeric author id. |
| 121 | - if ( ! is_numeric( $post->post_author ) ) { |
|
| 121 | + if ( ! is_numeric($post->post_author)) { |
|
| 122 | 122 | return; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Get the delete query,or return in case of failure. |
| 126 | - if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
| 126 | + if (false === ($delete = $this->get_delete_query($post->post_author))) { |
|
| 127 | 127 | return; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // Get the insert query,or return in case of failure. |
| 131 | - if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
| 131 | + if (false === ($insert = $this->get_insert_query($post->post_author))) { |
|
| 132 | 132 | return; |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // Send the query to the triple store. |
| 136 | - rl_execute_sparql_update_query( $delete . $insert ); |
|
| 136 | + rl_execute_sparql_update_query($delete.$insert); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
@@ -149,11 +149,11 @@ discard block |
||
| 149 | 149 | * |
| 150 | 150 | * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
| 151 | 151 | */ |
| 152 | - public function set_entity( $user_id, $value ) { |
|
| 152 | + public function set_entity($user_id, $value) { |
|
| 153 | 153 | |
| 154 | 154 | return 0 < $value |
| 155 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 156 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 155 | + ? update_user_meta($user_id, self::ENTITY_META_KEY, $value) |
|
| 156 | + : delete_user_meta($user_id, self::ENTITY_META_KEY); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -165,9 +165,9 @@ discard block |
||
| 165 | 165 | * |
| 166 | 166 | * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
| 167 | 167 | */ |
| 168 | - public function get_entity( $user_id ) { |
|
| 168 | + public function get_entity($user_id) { |
|
| 169 | 169 | |
| 170 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 170 | + return get_user_meta($user_id, self::ENTITY_META_KEY, true); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | * |
| 180 | 180 | * @return false|string The user's URI or false if not found. |
| 181 | 181 | */ |
| 182 | - private function _get_uri( $user_id ) { |
|
| 182 | + private function _get_uri($user_id) { |
|
| 183 | 183 | |
| 184 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 184 | + $user_uri = get_user_meta($user_id, self::URI_META_KEY, true); |
|
| 185 | 185 | |
| 186 | - if ( empty( $user_uri ) ) { |
|
| 186 | + if (empty($user_uri)) { |
|
| 187 | 187 | return false; |
| 188 | 188 | } |
| 189 | 189 | |
@@ -199,19 +199,19 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return false|string The user's URI or false in case of failure. |
| 201 | 201 | */ |
| 202 | - private function _build_uri( $user_id ) { |
|
| 202 | + private function _build_uri($user_id) { |
|
| 203 | 203 | |
| 204 | 204 | // Get the user, return false in case of failure. |
| 205 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 205 | + if (false === ($user = get_userdata($user_id))) { |
|
| 206 | 206 | return false; |
| 207 | 207 | }; |
| 208 | 208 | |
| 209 | 209 | // If the nicename is not set, return a failure. |
| 210 | - if ( empty( $user->user_nicename ) ) { |
|
| 210 | + if (empty($user->user_nicename)) { |
|
| 211 | 211 | return false; |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 214 | + return wl_configuration_get_redlink_dataset_uri()."/user/$user->user_nicename"; |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | * |
| 225 | 225 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 226 | 226 | */ |
| 227 | - private function _set_uri( $user_id, $user_uri ) { |
|
| 227 | + private function _set_uri($user_id, $user_uri) { |
|
| 228 | 228 | |
| 229 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 229 | + return update_user_meta($user_id, self::URI_META_KEY, $user_uri); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
@@ -238,28 +238,28 @@ discard block |
||
| 238 | 238 | * |
| 239 | 239 | * @return false|string The delete query or false in case of failure. |
| 240 | 240 | */ |
| 241 | - private function get_delete_query( $user_id ) { |
|
| 241 | + private function get_delete_query($user_id) { |
|
| 242 | 242 | |
| 243 | 243 | // Get the URI, return if there's none. |
| 244 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 244 | + if (false === ($user_uri = $this->get_uri($user_id))) { |
|
| 245 | 245 | return false; |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | // Build the delete query. |
| 249 | 249 | $query = Wordlift_Query_Builder::new_instance()->delete() |
| 250 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 250 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o') |
|
| 251 | 251 | ->build() |
| 252 | 252 | . Wordlift_Query_Builder::new_instance()->delete() |
| 253 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
| 253 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o') |
|
| 254 | 254 | ->build() |
| 255 | 255 | . Wordlift_Query_Builder::new_instance()->delete() |
| 256 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
| 256 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o') |
|
| 257 | 257 | ->build() |
| 258 | 258 | . Wordlift_Query_Builder::new_instance()->delete() |
| 259 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
| 259 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o') |
|
| 260 | 260 | ->build() |
| 261 | 261 | . Wordlift_Query_Builder::new_instance()->delete() |
| 262 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 262 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o') |
|
| 263 | 263 | ->build(); |
| 264 | 264 | |
| 265 | 265 | return $query; |
@@ -274,26 +274,26 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @return false|string The insert query or false in case of failure. |
| 276 | 276 | */ |
| 277 | - private function get_insert_query( $user_id ) { |
|
| 277 | + private function get_insert_query($user_id) { |
|
| 278 | 278 | |
| 279 | 279 | // Get the URI, return if there's none. |
| 280 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 280 | + if (false === ($user_uri = $this->get_uri($user_id))) { |
|
| 281 | 281 | return false; |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // Try to get the user data, in case of failure return false. |
| 285 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 285 | + if (false === ($user = get_userdata($user_id))) { |
|
| 286 | 286 | return false; |
| 287 | 287 | }; |
| 288 | 288 | |
| 289 | 289 | // Build the insert query. |
| 290 | 290 | $query = Wordlift_Query_Builder::new_instance() |
| 291 | 291 | ->insert() |
| 292 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
| 293 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
| 294 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
| 295 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
| 296 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
| 292 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI) |
|
| 293 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name) |
|
| 294 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname) |
|
| 295 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname) |
|
| 296 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty($user->user_url) ? $user->user_url : get_author_posts_url($user_id))) |
|
| 297 | 297 | ->build(); |
| 298 | 298 | |
| 299 | 299 | return $query; |
@@ -307,15 +307,15 @@ discard block |
||
| 307 | 307 | * |
| 308 | 308 | * @param integer $user_id The ID of the user |
| 309 | 309 | */ |
| 310 | - public function deny_editor_entity_create( $user_id ) { |
|
| 310 | + public function deny_editor_entity_create($user_id) { |
|
| 311 | 311 | |
| 312 | 312 | // Bail out if the user is not an editor. |
| 313 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 313 | + if ( ! $this->is_editor($user_id)) { |
|
| 314 | 314 | return; |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | // The user explicitly do not have the capability. |
| 318 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 318 | + update_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes'); |
|
| 319 | 319 | |
| 320 | 320 | } |
| 321 | 321 | |
@@ -327,15 +327,15 @@ discard block |
||
| 327 | 327 | * |
| 328 | 328 | * @param integer $user_id The ID of the user |
| 329 | 329 | */ |
| 330 | - public function allow_editor_entity_create( $user_id ) { |
|
| 330 | + public function allow_editor_entity_create($user_id) { |
|
| 331 | 331 | |
| 332 | 332 | // Bail out if the user is not an editor. |
| 333 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 333 | + if ( ! $this->is_editor($user_id)) { |
|
| 334 | 334 | return; |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | // The user explicitly do not have the capability. |
| 338 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 338 | + delete_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY); |
|
| 339 | 339 | |
| 340 | 340 | } |
| 341 | 341 | |
@@ -348,9 +348,9 @@ discard block |
||
| 348 | 348 | * |
| 349 | 349 | * @return int bool True if editing is denied otherwise false. |
| 350 | 350 | */ |
| 351 | - public function is_deny_editor_entity_create( $user_id ) { |
|
| 351 | + public function is_deny_editor_entity_create($user_id) { |
|
| 352 | 352 | |
| 353 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 353 | + return 'yes' === get_user_option(self::DENY_ENTITY_CREATE_META_KEY, $user_id); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | /** |
@@ -363,13 +363,13 @@ discard block |
||
| 363 | 363 | * |
| 364 | 364 | * @return bool True if the {@link WP_User} is an editor otherwise false. |
| 365 | 365 | */ |
| 366 | - public function is_editor( $user_id ) { |
|
| 366 | + public function is_editor($user_id) { |
|
| 367 | 367 | |
| 368 | 368 | // Get the user. |
| 369 | - $user = get_user_by( 'id', $user_id ); |
|
| 369 | + $user = get_user_by('id', $user_id); |
|
| 370 | 370 | |
| 371 | 371 | // Return true, if the user is found and has the `editor` role. |
| 372 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 372 | + return is_a($user, 'WP_User') && in_array('editor', (array) $user->roles); |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | /** |
@@ -381,15 +381,15 @@ discard block |
||
| 381 | 381 | * |
| 382 | 382 | * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
| 383 | 383 | */ |
| 384 | - public function editor_can_create_entities( $user_id ) { |
|
| 384 | + public function editor_can_create_entities($user_id) { |
|
| 385 | 385 | |
| 386 | 386 | // Return true if not an editor. |
| 387 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 387 | + if ( ! $this->is_editor($user_id)) { |
|
| 388 | 388 | return true; |
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | // Check if the user explicitly denied. |
| 392 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 392 | + return ! $this->is_deny_editor_entity_create($user_id); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | * |
| 408 | 408 | * @return array The capabilities array. |
| 409 | 409 | */ |
| 410 | - public function has_cap( $allcaps, $cap, $args ) { |
|
| 410 | + public function has_cap($allcaps, $cap, $args) { |
|
| 411 | 411 | /* |
| 412 | 412 | * For entity management/editing related capabilities |
| 413 | 413 | * check that an editor was not explicitly denied (in user profile) |
@@ -418,25 +418,25 @@ discard block |
||
| 418 | 418 | * Need protection against the case of edit_user and likes which do not |
| 419 | 419 | * require a capability, just request one. |
| 420 | 420 | */ |
| 421 | - if ( empty( $cap ) ) { |
|
| 421 | + if (empty($cap)) { |
|
| 422 | 422 | return $allcaps; |
| 423 | 423 | } |
| 424 | 424 | if ( |
| 425 | - ( 'edit_wordlift_entity' == $cap[0] ) || |
|
| 426 | - ( 'edit_wordlift_entities' == $cap[0] ) || |
|
| 427 | - ( 'edit_others_wordlift_entities' == $cap[0] ) || |
|
| 428 | - ( 'publish_wordlift_entities' == $cap[0] ) || |
|
| 429 | - ( 'read_private_wordlift_entities' == $cap[0] ) || |
|
| 430 | - ( 'delete_wordlift_entity' == $cap[0] ) || |
|
| 431 | - ( 'delete_wordlift_entities' == $cap[0] ) || |
|
| 432 | - ( 'delete_others_wordlift_entities' == $cap[0] ) || |
|
| 433 | - ( 'delete_published_wordlift_entities' == $cap[0] ) || |
|
| 434 | - ( 'delete_private_wordlift_entities' == $cap[0] ) |
|
| 425 | + ('edit_wordlift_entity' == $cap[0]) || |
|
| 426 | + ('edit_wordlift_entities' == $cap[0]) || |
|
| 427 | + ('edit_others_wordlift_entities' == $cap[0]) || |
|
| 428 | + ('publish_wordlift_entities' == $cap[0]) || |
|
| 429 | + ('read_private_wordlift_entities' == $cap[0]) || |
|
| 430 | + ('delete_wordlift_entity' == $cap[0]) || |
|
| 431 | + ('delete_wordlift_entities' == $cap[0]) || |
|
| 432 | + ('delete_others_wordlift_entities' == $cap[0]) || |
|
| 433 | + ('delete_published_wordlift_entities' == $cap[0]) || |
|
| 434 | + ('delete_private_wordlift_entities' == $cap[0]) |
|
| 435 | 435 | ) { |
| 436 | 436 | $user_id = $args[1]; |
| 437 | 437 | |
| 438 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 439 | - $allcaps[ $cap[0] ] = false; |
|
| 438 | + if ( ! $this->editor_can_create_entities($user_id)) { |
|
| 439 | + $allcaps[$cap[0]] = false; |
|
| 440 | 440 | } |
| 441 | 441 | } |
| 442 | 442 | |