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
76 changes: 69 additions & 7 deletions just-variables.admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ function jv_get_language_strings(){
* show variables settings page
*/
function jv_admin_settings_page(){

//$post_types = jv_get_post_types( 'object' );
$jv_read_settings = jv_get_read_settings();
$jv_multisite_settings = jv_get_multisite_settings();
$jv_tabs = !isset($_GET['tab']) ? 'fields' : $_GET['tab'];
// Form submit processing
if( !empty($_POST['submitted']) && !empty($_POST['jv_settings']) ){

$post = array_map( 'stripslashes_deep', $_POST['jv_settings']);
// update database with new values
$variables = array();
Expand All @@ -55,26 +58,85 @@ function jv_admin_settings_page(){
'slug' => $post['slug'][$key],
'name' => $post['title'][$key],
'default' => $post['default'][$key],
'placeholder' => $post['placeholder'][$key],
);

}
//pa($variables,1);

// update DB
update_option('jv_variables', $variables);
jv_update_options('jv_variables', $variables);

// check if we have variables - if no - delete all values
if( empty($variables) ){
update_option('jv_values', array());
}
}
}

$variables = get_option('jv_variables', array());
if( !empty($_POST['jv_update_settings']) ) {
if( MULTISITE ){
$jv_multisite_settings = jv_save_multisite_settings( $_POST['jv_multisite_setting'] );
}
$jv_read_settings = jv_update_read_settings();
}
$variables = jv_get_options('jv_variables', array());

// load template
include( JV_ROOT . '/templates/settings_page.tpl.php' );
}

/**
* Save miltisite settings from the form
* @param string $settings Multisite settings in present time
* @return string New multisite settings
*/
function jv_save_multisite_settings( $new_value ){
$current_value = jv_get_multisite_settings();
$new_value = trim($new_value);
if( $current_value ){
$saved = update_site_option( 'jv_multisite_setting', $new_value );
}
else{
$saved = add_site_option( 'jv_multisite_setting', $new_value );
}

if( $saved ){
jv_add_admin_notice('notice', __('<strong>MultiSite settings</strong> has been updated.', JV_TEXTDOMAIN));
}

return $new_value;
}

/**
* Get multisite settings
* @return string Return multisite settings
*/
function jv_get_multisite_settings(){
if( MULTISITE && $multisite_setting = get_site_option('jv_multisite_setting') )
{
return $multisite_setting;
}
return JV_CONF_MS_SITE;
}

/**
* Get options with wp-options
* @param string $key Option name
* @return array Options with $key
*/
function jv_get_options($key){
$jv_multisite_settings = jv_get_multisite_settings();
return $jv_multisite_settings == 'network' ? get_site_option($key, array()) : get_option($key, array());
}

/**
* Update options with wp-options
* @param string $key Option name
* @param array $value Values with option name
* @return bollean
*/
function jv_update_options($key, $value){
$jv_multisite_settings = jv_get_multisite_settings();
$jv_multisite_settings == 'network' ? update_site_option($key, $value) : update_option($key, $value);
return true;
}
?>
36 changes: 35 additions & 1 deletion just-variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
Tags: theme, variables, template, text data
Author: Alexander Prokopenko
Author URI: http://justcoded.com/
Version: 1.2.1
Version: 1.1
Donate link: http://justcoded.com/just-labs/just-wordpress-theme-variables-plugin/#donate
*/

define('JV_ROOT', dirname(__FILE__));
define('JV_TEXTDOMAIN', 'just-wp-variables');
define('JV_CONF_MS_SITE', 'site');
define('JV_CONF_MS_NETWORK', 'network');

if(!function_exists('pa')){
function pa($mixed, $stop = false) {
Expand Down Expand Up @@ -90,6 +92,38 @@ function just_variable_shortcode( $atts ){
}
add_shortcode('justvar', 'just_variable_shortcode');

/**
* add message to be printed with admin notice
* @param string $type notice|error
* @param string $message message to be printed
*/
function jv_add_admin_notice( $type, $message ){
global $jv_notices;
if( !$jv_notices )
$jv_notices = array();

$jv_notices[] = array($type, $message);
}

/**
* Function for update saving method
* @return string Return read method from file or database
*/
function jv_update_read_settings(){
$current_value = jv_get_read_settings();
$new_value = $_POST['jv_read_settings'];

if( MULTISITE && ($_POST['jv_multisite_setting'] != JV_CONF_MS_NETWORK && $new_value == JV_CONF_SOURCE_FS_GLOBAL) ){
jv_add_admin_notice('error', __('<strong>Settings storage update FAILED!</strong>. Your MultiSite Settings do not allow to set global storage in FileSystem', JCF_TEXTDOMAIN));
return $current_value;
}
}

/**
* Get read sttings
* @return string Return read method from file or database
*/
function jv_get_read_settings(){
return get_site_option('jv_read_settings');
}
?>
8 changes: 4 additions & 4 deletions just-variables.theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ function jv_theme_vars_admin_page(){
settings_fields( 'jv_theme_vars' );
do_settings_sections( 'jv_theme_vars' );

$theme_variables = get_option('jv_variables');
$theme_variables = get_site_option('jv_variables');
$values = get_option('jv_values');
?>
<p><?php _e('You can use the text variables below in your template files or post content with shortcode.', JV_TEXTDOMAIN ); ?></p>
<p><?php _e('Template files usage example: <code>&lt;?php just_variable( "code for variable" ); ?&gt;</code><br/>Get variable value (no print): <code>&lt;?php just_variable( "code for variable", FALSE ); ?&gt;</code><br/>Shortcode example: <code>[justvar code="code for variable"]</code>', JV_TEXTDOMAIN ); ?></p>
<p><?php _e('Template files usage example: <code>&lt;?php just_variable( "code for variable" ); ?&gt;</code><br/>Shortcode example: <code>[justvar code="code for variable"]</code>', JV_TEXTDOMAIN ); ?></p>
<table class="form-table">
<tbody>
<?php
Expand All @@ -64,9 +64,9 @@ function jv_theme_vars_admin_page(){
<th scope="row"><?php echo $var['name']; ?><br/><small><?php _e('code:', JV_TEXTDOMAIN); echo ' '. $slug; ?></small></th>
<td>
<?php if( $var['type'] == 'text' ) : ?>
<input class="regular-text" type="text" placeholder="<?php echo esc_attr($var['placeholder']); ?>" value="<?php echo esc_attr($value); ?>" name="jv_values[<?php echo $slug; ?>]">
<input class="regular-text" type="text" value="<?php echo esc_attr($value); ?>" name="jv_values[<?php echo $slug; ?>]">
<?php elseif( $var['type'] == 'textarea' ) : ?>
<textarea placeholder="<?php echo esc_attr($var['placeholder']); ?>" name="jv_values[<?php echo $slug; ?>]" cols="80" rows="3"><?php echo esc_html($value); ?></textarea>
<textarea name="jv_values[<?php echo $slug; ?>]" cols="80" rows="3"><?php echo esc_html($value); ?></textarea>
<?php endif; ?>
</td>
</tr>
Expand Down
41 changes: 37 additions & 4 deletions templates/settings_page.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<h2><?php _e('Just Variables', JV_TEXTDOMAIN); ?></h2>
<p><?php _e('Create at least one variable to unblock "Theme Variables" page.', JV_TEXTDOMAIN); ?></p>

<h2 class="nav-tab-wrapper">
<a class="nav-tab <?php echo ($jv_tabs == 'fields' ? 'nav-tab-active' : '');?>" href="?page=just_variables&amp;tab=fields"><?php _e('Fields', JV_TEXTDOMAIN); ?></a>
<a class="nav-tab <?php echo ($jv_tabs == 'settings' ? 'nav-tab-active' : '');?>" href="?page=just_variables&amp;tab=settings"><?php _e('Settings', JV_TEXTDOMAIN); ?></a>
</h2>

<?php // Fields list ?>
<?php if( $jv_tabs == 'fields' ): ?>
<form action="?page=just_variables" method="post">
<fieldset>
<table id="jv_settings" class="wp-list-table widefat fixed">
Expand All @@ -16,7 +23,6 @@
<th class="td_input"><?php _e('Variable', JV_TEXTDOMAIN); ?></th>
<th class="td_input"><?php _e('Title', JV_TEXTDOMAIN); ?></th>
<th class="td_input"><?php _e('Default Value', JV_TEXTDOMAIN); ?></th>
<th class="td_input"><?php _e('Inline hint', JV_TEXTDOMAIN); ?></th>
<th class="minwidth">&nbsp;</th>
</tr>
</thead>
Expand All @@ -32,10 +38,9 @@
<td class="td_input"><input type="text" name="jv_settings[slug][]" value="" class="regular-text" /></td>
<td class="td_input"><input type="text" name="jv_settings[title][]" value="" class="regular-text" /></td>
<td class="td_input"><input type="text" name="jv_settings[default][]" value="" placeholder="<?php _e('Default Value', JV_TEXTDOMAIN); ?>" class="regular-text" /></td>
<td class="td_input"><input type="text" name="jv_settings[placeholder][]" value="" placeholder="<?php _e('Inline hint (disappear on edit)', JV_TEXTDOMAIN); ?>" class="regular-text" /></td>
<td class="minwidth"><a href="#" class="delete_variable" title="<?php _e('Delete', JV_TEXTDOMAIN); ?>"><img src="<?php echo $assets_path; ?>/icon-delete.png" title="Delete" alt="Delete" /></a></td>
</tr>
<?php foreach($variables as $slug => $var) : ?>
<?php foreach($variables as $slug => $var) :?>
<tr>
<td class="minwidth"><span class="drag-handle">move</span></td>
<td class="td_type">
Expand All @@ -47,7 +52,6 @@
<td class="td_input"><input type="text" name="jv_settings[slug][]" value="<?php echo esc_attr($slug); ?>" class="regular-text" /></td>
<td class="td_input"><input type="text" name="jv_settings[title][]" value="<?php echo esc_attr($var['name']); ?>" class="regular-text" /></td>
<td class="td_input"><input type="text" name="jv_settings[default][]" value="<?php echo esc_attr($var['default']); ?>" placeholder="<?php _e('Default Value', JV_TEXTDOMAIN); ?>" class="regular-text" /></td>
<td class="td_input"><input type="text" name="jv_settings[placeholder][]" value="<?php echo esc_attr($var['placeholder']); ?>" placeholder="<?php _e('Inline hint (disappear on edit)', JV_TEXTDOMAIN); ?>" class="regular-text" /></td>
<td class="minwidth"><a href="#" class="delete_variable" title="<?php _e('Delete', JV_TEXTDOMAIN); ?>"><img src="<?php echo $assets_path; ?>/icon-delete.png" title="Delete" alt="Delete" /></a></td>
</tr>
<?php endforeach; ?>
Expand All @@ -66,4 +70,33 @@
<input type="hidden" name="submitted" value="1" />
</fieldset>
</form>

<?php // Settings boxes ?>
<?php elseif( $jv_tabs == 'settings' ): ?>
<div class="jv_tab-content">
<div class="jv_inner-tab-content" >
<form action="<?php get_permalink(); ?>" id="jvform_settings" method="post" class="jv_form_horiz" onsubmit="return initSettings();">

<?php if( MULTISITE ): ?>
<div class="card pressthis">
<h3 class="header"><?php _e('MultiSite settings:', JV_TEXTDOMAIN); ?></h3>
<fieldset>
<input type="radio" name="jv_multisite_setting" id="jv_setting_global"
value="<?php echo JV_CONF_MS_NETWORK; ?>" <?php checked($jv_multisite_settings, JV_CONF_MS_NETWORK); ?> />
<label for="jv_setting_global"><?php _e('Make fields settings global for all network', JV_TEXTDOMAIN); ?> </label><br />

<input type="radio" name="jv_multisite_setting" id="jv_setting_each"
value="<?php echo JV_CONF_MS_SITE; ?>" <?php checked($jv_multisite_settings, JV_CONF_MS_SITE); ?> />
<label for="jv_setting_each"><?php _e('Fields settings are unique for each site', JV_TEXTDOMAIN); ?> </label><br /><br />
</fieldset>
</div>
<?php endif; ?>

<br /><br />
<?php wp_nonce_field("just-nonce"); ?>
<input type="submit" class="button-primary jv_update_settings" name="jv_update_settings" value="<?php _e('Save all settings', JV_TEXTDOMAIN); ?>" />
</form>
</div>
</div>
<?php endif; ?>
</div>