'true',
'page' => 'true',
'embed' => 'block',
'allow' => array(),
);
/**
* Constructor
*
* @param array $args Constructor arguments.
*/
public function __construct( $args = array() ) {
wp_enqueue_script( 'jquery-ui-widget' );
if ( isset( $args['name'] ) ) {
$this->name = $args['name'];
}
if ( isset( $args['column'] ) ) {
$this->column = $args['column'];
}
if ( isset( $args['title'] ) ) {
$this->title = $args['title'];
}
if ( isset( $args['content'] ) ) {
$this->content = $args['content'];
}
if ( isset( $args['position'] ) && 'prepend' === $args['position'] ) {
$this->position = 'prepend';
}
if ( isset( $args['widget_id'] ) ) {
$this->widget_id = $args['widget_id'];
// Used to add widgets via ajax.
} else {
$this->widget_id = ++self::$widget_sequence_nr;
// Used to add widgets on page load.
}
if ( isset( $args['is_locked'] ) ) {
$this->is_locked = true === $args['is_locked'] || 'true' === $args['is_locked'];
}
if ( isset( $args['share'] ) && isset(
$args['share']['roles'],
$args['share']['users'],
$args['share']['post'],
$args['share']['page'],
$args['share']['embed'],
$args['share']['allow']
) ) {
$this->share = array(
'roles' => $args['share']['roles'],
'users' => $args['share']['users'],
'post' => $args['share']['post'],
'page' => $args['share']['page'],
'embed' => $args['share']['embed'],
'allow' => $args['share']['allow'],
);
}
$this->state = ( isset( $args['state'] ) ? $args['state'] : 'new' );
$this->wp_nonce = wp_create_nonce( static::WIDGET_REFRESH . WPDA::get_current_user_login() );
}
/**
* Construct widget container
*
* @return false|string
*/
protected function container() {
ob_start();
?>
js();
return ob_get_clean();
}
/**
* Construct widget html
*
* @return string
*/
protected function html() {
$share = '';
$layout = '';
$setting = '';
$refresh = ( $this->can_refresh ? " " : '' );
$close = ( !$this->is_locked ? '' : '' );
$widget = <<
EOF;
return $widget;
}
/**
* Cross origin check
*
* @param WPDA_Widget $widget Widget.
* @return bool
*/
protected static function check_cors( $widget ) {
if ( isset( $_POST['wpda_caller'] ) && 'embedded' === $_POST['wpda_caller'] ) {
// phpcs:ignore WordPress.Security.NonceVerification
$share = ( isset( $widget['widgetShare'] ) ? $widget['widgetShare'] : null );
if ( 'block' === $share['embed'] ) {
WPDA::sent_header( 'application/json', '*' );
echo static::msg( 'ERROR', 'No access' );
// phpcs:ignore WordPress.Security.EscapeOutput
wp_die();
} else {
if ( '*' === $share['embed'] ) {
WPDA::sent_header( 'application/json', '*' );
return true;
} else {
// Access is already checked with sonce token.
WPDA::sent_header( 'application/json', '*' );
return true;
}
}
}
return false;
}
/**
* Abstract method forcing each subclass to add its own specific javascript code
*
* @return mixed
*/
protected abstract function js();
// Method to add custom JavaScript code.
/**
* Add widget to dashboard
*
* @return void
*/
public function add() {
echo $this->container();
// phpcs:ignore WordPress.Security.EscapeOutput
?>
$status,
'msg' => $msg,
);
return wp_json_encode( $error );
}
}