mirror of
https://github.com/KevinMidboe/rohnenedre.git
synced 2025-10-29 17:50:37 +00:00
Initial commit. State 04.2021.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
class AjaxSettings {
|
||||
const VERSION = '0.0.1';
|
||||
static $DEFAULTS = array(
|
||||
"network_wide" => false
|
||||
);
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
private function __construct( $opts ) {
|
||||
$this->options = array_merge(self::$DEFAULTS, $opts);
|
||||
|
||||
|
||||
add_action('admin_enqueue_scripts', array($this, "enqueue_scripts"));
|
||||
add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
|
||||
|
||||
add_action(
|
||||
'wp_ajax_ajax_settings_save_' . $this->name(),
|
||||
array($this, 'handle_settings_save')
|
||||
);
|
||||
}
|
||||
|
||||
function enqueue_scripts() {
|
||||
$ident = $this->identifier();
|
||||
wp_register_script(
|
||||
$ident,
|
||||
$this->options['base_url'] . "js/ajax-settings.min.js",
|
||||
array('backbone'),
|
||||
self::VERSION,
|
||||
TRUE
|
||||
);
|
||||
wp_localize_script($ident, 'ajaxSetOpt', $this->options);
|
||||
wp_enqueue_script($ident);
|
||||
}
|
||||
|
||||
function enqueue_styles() {
|
||||
$ident = $this->identifier();
|
||||
wp_register_style(
|
||||
$ident,
|
||||
$this->options['base_url'] . 'css/ajax-settings.min.css',
|
||||
false,
|
||||
self::VERSION
|
||||
);
|
||||
wp_enqueue_style($ident);
|
||||
}
|
||||
|
||||
function handle_settings_save() {
|
||||
global $HTTP_RAW_POST_DATA;
|
||||
if (!isset($HTTP_RAW_POST_DATA)) {
|
||||
$HTTP_RAW_POST_DATA = file_get_contents( "php://input" );
|
||||
}
|
||||
|
||||
// strip off any leading characters before json starts and then parse
|
||||
$stripped = preg_replace('/^.*?{/', '{', $HTTP_RAW_POST_DATA);
|
||||
$settings = json_decode($stripped, true);
|
||||
|
||||
if (!$settings) wp_die(__('Settings could not be parsed — this may be caused by a plugin conflict.'));
|
||||
|
||||
$option_page = $settings['option_page'];
|
||||
$is_network_wide = isset($_REQUEST['network_wide']) && $_REQUEST['network_wide'];
|
||||
|
||||
if (!wp_verify_nonce($settings['_wpnonce'], $option_page . "-options")) {
|
||||
wp_die(__('Cheatin’ uh?'));
|
||||
}
|
||||
|
||||
// if it's network request, we want to check that the current user is
|
||||
// a network admin
|
||||
if ($is_network_wide && !is_super_admin()) wp_die(__('Cheatin’ uh?'));
|
||||
|
||||
// verify that the user has the permissions to edit the clef page
|
||||
$capability = 'manage_options';
|
||||
$capability = apply_filters( "option_page_capability_{$option_page}", $capability );
|
||||
if ( !current_user_can( $capability ) )
|
||||
wp_die(__('Cheatin’ uh?'));
|
||||
|
||||
$whitelist_options = apply_filters( 'whitelist_options', array() );
|
||||
$options = $whitelist_options[$settings['option_page']];
|
||||
if (empty($options[0]) || $options[0] != $this->name()) {
|
||||
wp_die("You can't do that!");
|
||||
}
|
||||
|
||||
$to_be_saved = array();
|
||||
foreach ($settings as $key => &$value) {
|
||||
$match = preg_match('/(.+)\[(.+)\]$/', $key, $output);
|
||||
if ($match) {
|
||||
$nester_key = $output[1];
|
||||
if ($nester_key == $this->name()) {
|
||||
$nested_key = $output[2];
|
||||
$to_be_saved[$nested_key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$to_be_saved = apply_filters('ajax_settings_pre_save', $to_be_saved);
|
||||
|
||||
if ($is_network_wide) {
|
||||
update_site_option($this->name(), $to_be_saved);
|
||||
} else {
|
||||
update_option($this->name(), $to_be_saved);
|
||||
}
|
||||
|
||||
$errors = get_settings_errors();
|
||||
$response = array();
|
||||
|
||||
if (!empty($errors)) {
|
||||
$error_messages = array();
|
||||
foreach ($errors as &$error) {
|
||||
$error_messages[$error['code']] = $error['message'];
|
||||
}
|
||||
$response['errors'] = $error_messages;
|
||||
header('HTTP/1.0 400');
|
||||
wp_send_json_error($response);
|
||||
} else {
|
||||
$response['success'] = true;
|
||||
wp_send_json($response);
|
||||
}
|
||||
}
|
||||
|
||||
function identifier() {
|
||||
return $this->name() . '-ajax-settings';
|
||||
}
|
||||
|
||||
function name() {
|
||||
return $this->options['options_name'];
|
||||
}
|
||||
|
||||
function update_options($options) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
public static function start($options = array()) {
|
||||
if (!isset(self::$instance) || self::$instance === null) {
|
||||
self::$instance = new self($options);
|
||||
} else {
|
||||
self::$instance->update_options($options);
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
|
||||
1
wp-content/plugins/wpclef/includes/lib/ajax-settings/css/ajax-settings.min.css
vendored
Normal file
1
wp-content/plugins/wpclef/includes/lib/ajax-settings/css/ajax-settings.min.css
vendored
Normal file
@@ -0,0 +1 @@
|
||||
div.ajax-settings-updated{width:20px;height:20px;background:#fff;position:absolute;border-radius:20px;right:0;top:0;margin-left:10px;opacity:1;-webkit-animation:blink 2s infinite;animation:blink 2s infinite}div.ajax-settings-updated.success{background-color:#2ecc71;-webkit-transition:500ms;transition:500ms}@-webkit-keyframes blink{0%,100%{opacity:1}50%{opacity:.4}}@keyframes blink{0%,100%{opacity:1}50%{opacity:.4}}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
2
wp-content/plugins/wpclef/includes/lib/ajax-settings/js/ajax-settings.min.js
vendored
Normal file
2
wp-content/plugins/wpclef/includes/lib/ajax-settings/js/ajax-settings.min.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
(function(e,t){var i;return i=t.Model.extend({url:ajaxurl+"?action=ajax_settings_save",sync:function(e,i,s){return s=s||{},s.emulateHTTP=!0,t.Model.prototype.sync.call(this,e,i,s)},parse:function(e,t){return t.form?(this.optionsName=t.options_name,this.url=ajaxurl+("?action=ajax_settings_save_"+this.optionsName),t.url&&(this.url=t.url),t.network_wide&&(this.url+="&network_wide=true"),this.$form=t.form,_.extend(e,this.$form.serializeObject())):void 0},isNew:function(){return!1},update:function(t){var i;return i=e(t),i.is(":checkbox")&&(t.value=i.is(":checked")?1:0),this.saving=!0,this.save(t.name,t.value,{success:this.saveSuccess.bind(this),error:this.saveError.bind(this)})},saveSuccess:function(e,t){return this.saving=!1},saveError:function(e,t){return this.saving=!1},findInput:function(e,t){return null==t&&(t={}),this._is||(this._is={}),this._is[e]||(this._is[e]=this.$form.find("input, select, textarea").filter("[name='"+e+"']")),this._is[e]}}),e.fn.serializeObject=function(t){var i,s,n,r,a;for(s={},a=e(this).serializeArray(),n=0,r=a.length;r>n;n++)i=a[n],s[i.name]=i.value;return s},i.extend=t.Model.extend,this.AjaxSettingsModel=i}).call(this,jQuery,Backbone);
|
||||
(function(e,t){var s;return s=t.View.extend({messageTemplate:_.template("<div class='<%=type%> ajax-settings-msg'><p><%= message %></p></div>"),events:{"change input:not(.ajax-ignore)":"persistChanges","change select:not(.ajax-ignore)":"persistChanges","change textarea:not(.ajax-ignore)":"persistChanges"},modelClass:AjaxSettingsModel,el:'form[action="options.php"]',genericErrorMessage:"Something went wrong: ",successMessageDisplayTime:3e3,initialize:function(t){return this.opts=t,this.opts.formSelector&&this.setElement(e(t.formSelector)),this.hide(),this.model=new this.modelClass({},_.extend({form:this.$el,parse:!0},this.opts)),this.listenTo(this.model,"change",this.render),this.listenTo(this.model,"change",this.startUpdating),this.listenTo(this.model,"sync",this.updated),this.listenTo(this.model,"error",this.error),this.listenTo(this.model,"change",this.clearErrors)},hide:function(){return this.$el.hide()},render:function(){var e,t,s,r,i;r=this.model.attributes,i=[];for(t in r)s=r[t],e=this.model.findInput(t),e?e.is(":checkbox")?i.push(e.prop("checked",parseInt(s))):i.push(e.val(s)):i.push(void 0);return i},show:function(){return this.$el.fadeIn()},persistChanges:function(e){return e.preventDefault(),this.model.update(e.currentTarget)},startUpdating:function(e,t){var s,r,i,n;i=e.changed,n=[];for(s in i)r=i[s],n.push(this.settingUpdateSent(this.model.findInput(s)));return n},updated:function(e,t){var s,r,i,n;this.render(),i=e.changed,n=[];for(s in i)r=i[s],n.push(this.settingUpdateSuccess(this.model.findInput(s)));return n},error:function(e,t){var s,r,i,n;if(!t.responseJSON||!t.responseJSON.data)return void this.showMessage({message:""+this.genericErrorMessage+" "+t.responseText,type:"error"});if(!t.responseJSON.data.errors&&t.responseJSON.data.error)return void this.showMessage({message:t.responseJSON.data.error,type:"error"});i=t.responseJSON.data.errors,n=[];for(s in i)r=i[s],n.push(this.settingsUpdateError(this.model.findInput(""+this.opts.options_name+"["+s+"]"),r));return n},clearErrors:function(e,t){var s,r,i,n,a;this.globalError&&(this.globalError.remove(),this.globalError=null),n=e.changed,a=[];for(r in n)i=n[r],s=this.model.findInput(r),s.data("errorEl")?(s.data("errorEl").remove(),a.push(s.data("errorEl",null))):a.push(void 0);return a},settingUpdateSent:function(e){},settingUpdateSuccess:function(t){var s;if(t.length&&!t.data("successEl"))return s=e(this.messageTemplate({message:"Setting saved.",type:"updated"})).hide(),t.data("successEl",s.insertAfter(t).slideDown()),setTimeout(function(){return s.slideUp(),t.data("successEl",null)},this.successMessageDisplayTime)},settingsUpdateError:function(t,s){var r;return t.data("errorEl")?t.data("errorEl").find("p").html(s):(r=e(this.messageTemplate({message:s,type:"error"})).hide(),t.data("errorEl",r.insertAfter(t).slideDown()))},showMessage:function(t){var s;return s=e(this.messageTemplate(t)).hide(),this.globalError=s.prependTo(this.$el).slideDown(),e("html, body").animate({scrollTop:this.$el.scrollTop()},"slow")}},{extend:t.View.extend}),this.AjaxSettingsView=s,e(document).ready(function(){var e;return ajaxSetOpt.initialize?(e=""+ajaxSetOpt.options_name+"AjaxSettingsView",window[e]=new s(ajaxSetOpt)):void 0}),e.fn.leftPositionWithPadding=function(){var e;return e=this.position().left,e+=this.width(),this.css("padding-left")&&(e+=parseInt(this.css("padding-left"))),this.css("padding-right")&&(e+=parseInt(this.css("padding-right"))),this.css("border-left")&&(e+=parseInt(this.css("border-left"))),this.css("border-right")&&(e+=parseInt(this.css("border-right"))),e}}).call(this,jQuery,Backbone);
|
||||
Reference in New Issue
Block a user