Skip to content

Commit

Permalink
Merge pull request #219 from ConnectThink/sb-use-wp-compiling-folder
Browse files Browse the repository at this point in the history
Use WP variables for path to base compiling folder
  • Loading branch information
shadoath authored Oct 19, 2021
2 parents 4b1101e + 919e140 commit 7b95397
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
33 changes: 26 additions & 7 deletions options.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,44 @@ public function page_init() {
'wpscss_options' // Page
);

$base_folder_options = array(
'Uploads Directory' => 'Uploads Directory',
'WP-SCSS Plugin' => 'WP-SCSS Plugin'
);
if(get_stylesheet_directory() === get_template_directory()){
$base_folder_options['Current Theme'] = 'Current Theme';
}else{
$base_folder_options['Parent Theme'] = 'Parent Theme';
$base_folder_options['Child Theme'] = 'Child Theme';
}

add_settings_field(
'wpscss_base_folder', // ID
'Base Location', // Title
array( $this, 'input_select_callback' ), // Callback
'wpscss_options', // Page
'wpscss_paths_section', // Section
'wpscss_paths_section', // Section
array( // args
'name' => 'base_compiling_folder',
'type' => apply_filters( 'wp_scss_base_compiling_modes',
array(
get_template_directory() => 'Parent theme', // Won't display if no parent theme as it would have duplicate keys in array
get_stylesheet_directory() => (get_stylesheet_directory() === get_template_directory() ? 'Current theme' : 'Child theme'),
wp_get_upload_dir()['basedir'] => 'Uploads directory',
WPSCSS_PLUGIN_DIR => 'WP-SCSS Plugin',
)
$base_folder_options
)
)
);

// #TODO see if this is ever warrented
// add_settings_field(
// 'Use Absolute Path', // ID
// 'Use Absolute Path', // Title
// array( $this, 'input_checkbox_callback' ), // Callback
// 'wpscss_options', // Page
// 'wpscss_paths_section', // Section
// array( // args
// 'name' => 'use_absolute_paths'
// )
// );


add_settings_field(
'wpscss_scss_dir', // ID
'SCSS Location', // Title
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ This plugin will only work with .scss format.

## Changelog

- 2.4.0
- Changes the base_compiling_folder to store key not path to directory [shadoath](https://github.com/ConnectThink/WP-SCSS/issues/219)
- This allows deploying from local or staging to production by not saving absolute paths in DB.
- 2.3.5
- Add 'selected' to wp_kses on select() [shadoath](https://github.com/ConnectThink/WP-SCSS/issues/217)
- 2.3.4
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Plugin URI: https://github.com/ConnectThink/WP-SCSS
Requires at least: 3.0.1
Tested up to: 5.8
Requires PHP: 5.6
Stable tag: 2.3.5
Stable tag: 2.4.0
License: GPLv3 or later
License URI: http://www.gnu.org/copyleft/gpl.html

Expand Down
42 changes: 36 additions & 6 deletions wp-scss.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP-SCSS
* Plugin URI: https://github.com/ConnectThink/WP-SCSS
* Description: Compiles scss files live on WordPress.
* Version: 2.3.5
* Version: 2.4.0
* Author: Connect Think
* Author URI: http://connectthink.com
* License: GPLv3
Expand Down Expand Up @@ -44,7 +44,7 @@
define('WPSCSS_VERSION_KEY', 'wpscss_version');

if (!defined('WPSCSS_VERSION_NUM'))
define('WPSCSS_VERSION_NUM', '2.3.5');
define('WPSCSS_VERSION_NUM', '2.4.0');

// Add version to options table
if ( get_option( WPSCSS_VERSION_KEY ) !== false ) {
Expand Down Expand Up @@ -130,15 +130,45 @@ function wpscss_plugin_db_cleanup($option_values){
* Assign settings via settings array to pass to object
*/

// Use current WP functions to get directory values, only store key
function get_base_dir_from_name($name_or_old_path){
$possible_directories = array(
'Uploads Directory' => wp_get_upload_dir()['basedir'],
'WP-SCSS Plugin' => WPSCSS_PLUGIN_DIR,
);
// Won't display if no parent theme as it would have duplicate keys in array
if(get_stylesheet_directory() === get_template_directory()){
$possible_directories['Current Theme'] = get_stylesheet_directory();
}else{
$possible_directories['Parent Theme'] = get_template_directory();
$possible_directories['Child Theme'] = get_stylesheet_directory();
}
if(array_key_exists($name_or_old_path, $possible_directories)){
return $possible_directories[$name_or_old_path];
}else{
$key = array_search($name_or_old_path, $possible_directories);
$notice = '<p><strong>WP-SCSS:</strong> <a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">Please save your settings</a>';
if($key){
$notice .= ' with the Base Location of <b>'. $key .'</b> specified.</p>';
}else{
$notice .= ' with the <b>correct</b> Base Location specified.</p>';
}
add_action('admin_notices', function() use ($notice){
echo '<div class="notice notice-info">' . $notice . '</div>';
});
return $name_or_old_path;
}
}

$wpscss_options = get_option( 'wpscss_options' );
$base_compiling_folder = isset($wpscss_options['base_compiling_folder']) ? $wpscss_options['base_compiling_folder'] : get_stylesheet_directory();
$base_compiling_folder = isset($wpscss_options['base_compiling_folder']) ? get_base_dir_from_name($wpscss_options['base_compiling_folder']) : get_stylesheet_directory();
$scss_dir_setting = isset($wpscss_options['scss_dir']) ? $wpscss_options['scss_dir'] : '';
$css_dir_setting = isset($wpscss_options['css_dir']) ? $wpscss_options['css_dir'] : '';

// Checks if directories are not yet defined
if( $scss_dir_setting == false || $css_dir_setting == false ) {
function wpscss_settings_error() {
echo '<div class="error">
echo '<div class="notice notice-error">
<p><strong>WP-SCSS</strong> requires both directories be specified. <a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">Please update your settings.</a></p>
</div>';
}
Expand All @@ -148,7 +178,7 @@ function wpscss_settings_error() {
// Checks if SCSS directory exists
} elseif ( !is_dir($base_compiling_folder . $scss_dir_setting) ) {
add_action('admin_notices', function() use ($base_compiling_folder, $scss_dir_setting){
echo '<div class="error">
echo '<div class="notice notice-error">
<p><strong>WP-SCSS:</strong> The SCSS directory does not exist (' . $base_compiling_folder . $scss_dir_setting . '). Please create the directory or <a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">update your settings.</a></p>
</div>';
});
Expand All @@ -157,7 +187,7 @@ function wpscss_settings_error() {
// Checks if CSS directory exists
} elseif ( !is_dir($base_compiling_folder . $css_dir_setting) ) {
add_action('admin_notices', function() use ($base_compiling_folder, $css_dir_setting){
echo '<div class="error">
echo '<div class="notice notice-error">
<p><strong>WP-SCSS:</strong> The CSS directory does not exist (' . $base_compiling_folder . $css_dir_setting . '). Please create the directory or <a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">update your settings.</a></p>
</div>';
});
Expand Down

0 comments on commit 7b95397

Please sign in to comment.