Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions classes/Visualizer/Module/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Visualizer_Module_Admin extends Visualizer_Module {

const NAME = __CLASS__;

const OPTION_GLOBAL_SETTINGS = 'visualizer_global_settings';

/**
* Library page suffix.
*
Expand All @@ -48,6 +50,14 @@ class Visualizer_Module_Admin extends Visualizer_Module {
*/
private $_supportPage;

/**
* Settings page suffix.
*
* @access private
* @var string
*/
private $_settingsPage;

/**
* Constructor.
*
Expand All @@ -65,6 +75,8 @@ public function __construct( Visualizer_Plugin $plugin ) {
$this->_addAction( 'admin_footer', 'renderTemplates' );
$this->_addAction( 'admin_enqueue_scripts', 'enqueueLibraryScripts', null, 0 );
$this->_addAction( 'admin_enqueue_scripts', 'enqueue_support_page' );
$this->_addAction( 'admin_enqueue_scripts', 'enqueueSettingsScripts' );
$this->_addAction( 'admin_post_visualizer_save_global_settings', 'saveGlobalSettings' );
$this->_addAction( 'admin_menu', 'registerAdminMenu' );
$this->_addFilter( 'media_view_strings', 'setupMediaViewStrings' );
$this->_addFilter( 'plugin_action_links', 'getPluginActionLinks', 10, 2 );
Expand Down Expand Up @@ -835,6 +847,15 @@ public function registerAdminMenu() {
'admin.php?page=' . Visualizer_Plugin::NAME . '&vaction=addnew'
);

$this->_settingsPage = add_submenu_page(
Visualizer_Plugin::NAME,
__( 'Settings', 'visualizer' ),
__( 'Settings', 'visualizer' ),
'manage_options',
'viz-settings',
array( $this, 'renderSettingsPage' )
);

$this->_supportPage = add_submenu_page(
Visualizer_Plugin::NAME,
__( 'Support', 'visualizer' ),
Expand Down Expand Up @@ -1058,6 +1079,89 @@ public function renderSupportPage() {
include_once VISUALIZER_ABSPATH . '/templates/support.php';
}

/**
* Enqueues scripts/styles for the Settings page.
*
* @access public
*/
public function enqueueSettingsScripts( string $hook_suffix ): void {
if ( $this->_settingsPage !== $hook_suffix ) {
return;
}
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
}

/**
* Renders the global style settings page.
*
* @access public
*/
public function renderSettingsPage(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'visualizer' ) );
}
$settings = self::getGlobalSettings();
include_once VISUALIZER_ABSPATH . '/templates/global-settings.php';
}

/**
* Returns the global style settings option.
*
* @return array<string, string>
* @access public
* @static
*/
public static function getGlobalSettings(): array {
$defaults = array(
'color_primary' => '',
'color_secondary' => '',
'apply_existing' => '0',
);
$saved = get_option( self::OPTION_GLOBAL_SETTINGS, array() );
return wp_parse_args( $saved, $defaults );
}

/**
* Handles saving of the global style settings.
*
* @access public
*/
public function saveGlobalSettings(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'visualizer' ) );
}

check_admin_referer( 'visualizer_save_global_settings' );

$color_primary = sanitize_hex_color( wp_unslash( $_POST['visualizer_color_primary'] ?? '' ) );
$color_secondary = sanitize_hex_color( wp_unslash( $_POST['visualizer_color_secondary'] ?? '' ) );
$apply_existing = ! empty( $_POST['visualizer_apply_existing'] ) ? '1' : '0';
$clear = ! empty( $_POST['visualizer_clear_settings'] );

if ( $clear ) {
delete_option( self::OPTION_GLOBAL_SETTINGS );
} else {
$settings = array(
'color_primary' => $color_primary ? $color_primary : '',
'color_secondary' => $color_secondary ? $color_secondary : '',
'apply_existing' => $apply_existing,
);
update_option( self::OPTION_GLOBAL_SETTINGS, $settings );
}

wp_safe_redirect(
add_query_arg(
array(
'page' => 'viz-settings',
'updated' => $clear ? 'cleared' : 'true',
),
admin_url( 'admin.php' )
)
);
exit;
}

/**
* Renders visualizer library page.
*
Expand Down
7 changes: 6 additions & 1 deletion classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,12 @@ private function _handleDataAndSettingsPage() {
}
// save meta data only when it is NOT being canceled.
if ( ! $is_canceled ) {
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
$post_settings = $_POST;
$existing = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
if ( isset( $existing['colors'] ) && is_array( $existing['colors'] ) && ! isset( $post_settings['colors'] ) ) {
$post_settings['colors'] = $existing['colors'];
}
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $post_settings );

// we will keep a parameter called 'internal_title' that will be set to the given title or, if empty, the chart ID
// this will help in searching with the chart id.
Expand Down
Loading
Loading