My Custom Functions - Version 2.4

Version Description

  • Added trigger for temporary disable the custom functions (PHP code).
  • Functions _prepare, _duplicates, and _exec optimized.
Download this release

Release Info

Developer Arthur Gareginyan
Plugin Icon 128x128 My Custom Functions
Version 2.4
Comparing to
See all releases

Code changes from version 2.3 to 2.4

Files changed (4) hide show
  1. inc/settings_page.php +8 -3
  2. inc/style.css +77 -1
  3. my-custom-functions.php +39 -33
  4. readme.txt +7 -4
inc/settings_page.php CHANGED
@@ -10,7 +10,7 @@ defined('ABSPATH') or die("Restricted access!");
10
  /**
11
  * Render Settings Page
12
  *
13
- * @since 2.3
14
  */
15
  function MCFunctions_render_submenu_page() {
16
 
@@ -102,11 +102,16 @@ function MCFunctions_render_submenu_page() {
102
  <?php
103
  // Declare variables
104
  $options = get_option( 'anarcho_cfunctions_settings' );
105
- $content = isset( $options['anarcho_cfunctions-content'] ) && ! empty( $options['anarcho_cfunctions-content'] ) ? $options['anarcho_cfunctions-content'] : '/* Enter Your Custom Functions Here */';
 
106
  ?>
107
  <div class="postbox">
108
  <h3 class="title">
109
  <label for="anarcho_cfunctions_settings[anarcho_cfunctions-content]" ><?php _e( 'Functions', 'my-custom-functions' ) ?></label>
 
 
 
 
110
  </h3>
111
  <div class="inside">
112
  <textarea name="anarcho_cfunctions_settings[anarcho_cfunctions-content]" id="anarcho_cfunctions_settings[anarcho_cfunctions-content]" ><?php echo esc_attr( $content ); ?></textarea>
@@ -126,4 +131,4 @@ function MCFunctions_render_submenu_page() {
126
 
127
  </div>
128
  <?php
129
- }
10
  /**
11
  * Render Settings Page
12
  *
13
+ * @since 2.4
14
  */
15
  function MCFunctions_render_submenu_page() {
16
 
102
  <?php
103
  // Declare variables
104
  $options = get_option( 'anarcho_cfunctions_settings' );
105
+ $content = isset( $options['anarcho_cfunctions-content'] ) && !empty( $options['anarcho_cfunctions-content'] ) ? $options['anarcho_cfunctions-content'] : '/* Enter Your Custom Functions Here */';
106
+ $enable = isset( $options['enable'] ) && !empty( $options['enable'] ) && $options['enable'] == "on" ? 'checked' : ' ';
107
  ?>
108
  <div class="postbox">
109
  <h3 class="title">
110
  <label for="anarcho_cfunctions_settings[anarcho_cfunctions-content]" ><?php _e( 'Functions', 'my-custom-functions' ) ?></label>
111
+ <div class="slider">
112
+ <input type="checkbox" name="anarcho_cfunctions_settings[enable]" id="anarcho_cfunctions_settings[enable]" <?php echo $enable; ?> >
113
+ <label for="anarcho_cfunctions_settings[enable]"></label>
114
+ </div>
115
  </h3>
116
  <div class="inside">
117
  <textarea name="anarcho_cfunctions_settings[anarcho_cfunctions-content]" id="anarcho_cfunctions_settings[anarcho_cfunctions-content]" ><?php echo esc_attr( $content ); ?></textarea>
131
 
132
  </div>
133
  <?php
134
+ }
inc/style.css CHANGED
@@ -1,7 +1,7 @@
1
  /*
2
  * My Custom Functions
3
  * Style sheet for plugin's page
4
- * @since 2.3
5
  * @agareginyan
6
  */
7
 
@@ -32,6 +32,7 @@ h2 span a {
32
  h3.title {
33
  font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif;
34
  border-bottom: 1px solid #eee;
 
35
  }
36
 
37
  /* Messages */
@@ -75,3 +76,78 @@ h3.title {
75
  #donate p {
76
  margin-top: 5px;
77
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  /*
2
  * My Custom Functions
3
  * Style sheet for plugin's page
4
+ * @since 2.4
5
  * @agareginyan
6
  */
7
 
32
  h3.title {
33
  font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif;
34
  border-bottom: 1px solid #eee;
35
+ height: 25px;
36
  }
37
 
38
  /* Messages */
76
  #donate p {
77
  margin-top: 5px;
78
  }
79
+
80
+ /* On/Off Trigger
81
+ -------------------------------------------------------------- */
82
+ .slider {
83
+ margin-left: auto;
84
+ margin-right: 0;
85
+ margin-top: -20px;
86
+ height: 23px;
87
+ padding-top: 5px;
88
+ width: 80px;
89
+ background: #f2f2f2;
90
+ -webkit-border-radius: 3px;
91
+ -moz-border-radius: 3px;
92
+ border-radius: 3px;
93
+ position: relative;
94
+ -webkit-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
95
+ -moz-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
96
+ box-shadow: inset 0px 1px 1px rgba(0,0,0,0.5), 0px 1px 0px rgba(255,255,255,0.2);
97
+ }
98
+
99
+ .slider:after {
100
+ content: 'ON';
101
+ margin-top: -3px;
102
+ font: 12px/26px Arial, sans-serif;
103
+ color: #00bf00;
104
+ position: absolute;
105
+ right: 10px;
106
+ z-index: 0;
107
+ font-weight: bold;
108
+ text-shadow: 1px 1px 0px rgba(255,255,255,.15);
109
+ }
110
+
111
+ .slider:before {
112
+ content: 'OFF';
113
+ margin-top: -3px;
114
+ font: 12px/26px Arial, sans-serif;
115
+ color: #ff0000;
116
+ position: absolute;
117
+ left: 10px;
118
+ z-index: 0;
119
+ font-weight: bold;
120
+ }
121
+
122
+ .slider label {
123
+ display: block;
124
+ width: 37px;
125
+ height: 24px;
126
+ -webkit-transition: all .4s ease;
127
+ -moz-transition: all .4s ease;
128
+ -o-transition: all .4s ease;
129
+ -ms-transition: all .4s ease;
130
+ transition: all .4s ease;
131
+ cursor: pointer;
132
+ position: absolute;
133
+ top: 1px;
134
+ left: 1px;
135
+ z-index: 1;
136
+ -webkit-border-radius: 3px;
137
+ border-radius: 3px;
138
+ background: #0085ba;
139
+ border-color: #0073aa #006799 #006799;
140
+ -webkit-box-shadow: 0 1px 0 #006799;
141
+ box-shadow: 0 2px 0 #006799;
142
+ }
143
+ .slider label:hover {
144
+ background: #008ec2;
145
+ }
146
+
147
+ .slider input[type=checkbox] {
148
+ visibility: hidden;
149
+ }
150
+
151
+ .slider input[type=checkbox]:checked + label {
152
+ left: 43px;
153
+ }
my-custom-functions.php CHANGED
@@ -5,7 +5,7 @@
5
  * Description: Easily and safely add your own functions, snippets or any custom codes directly out of your WordPress Dashboard without need of an external editor.
6
  * Author: Arthur Gareginyan
7
  * Author URI: http://www.arthurgareginyan.com
8
- * Version: 2.3
9
  * License: GPL3
10
  * Text Domain: my-custom-functions
11
  * Domain Path: /languages/
@@ -128,14 +128,10 @@ add_action( 'admin_enqueue_scripts', 'MCFunctions_enqueue_codemirror_scripts' );
128
  /**
129
  * Prepare the user entered code for execution
130
  *
131
- * @since 2.3
132
  */
133
- function MCFunctions_prepare() {
134
 
135
- // Read from DB
136
- $options = get_option( 'anarcho_cfunctions_settings' );
137
- $content = $options['anarcho_cfunctions-content'];
138
-
139
  // Cleaning
140
  $content = trim( $content );
141
  $content = ltrim( $content, '<?php' );
@@ -148,13 +144,12 @@ function MCFunctions_prepare() {
148
  /**
149
  * Check the user entered code for duplicate names of functions
150
  *
151
- * @since 2.3
152
  */
153
- function MCFunctions_duplicates() {
154
 
155
  // Find names of user entered functions and check for duplicates
156
- $user_func = MCFunctions_prepare();
157
- preg_match_all('/function[\s\n]+(\S+)[\s\n]*\(/i', $user_func, $user_func_names);
158
  $user_func_a = count( $user_func_names[1] );
159
  $user_func_b = count( array_unique( $user_func_names[1] ) );
160
 
@@ -164,46 +159,57 @@ function MCFunctions_duplicates() {
164
  $declared_func_internal = array_intersect( $user_func_names[1], $declared_func['internal'] );
165
 
166
  // Update error status
167
- if ( $user_func_a != $user_func_b OR count( $declared_func_user ) > 0 OR count( $declared_func_internal ) > 0 ) {
168
  update_option( 'anarcho_cfunctions_error', '1' ); // ERROR
169
- $duplicates_status = '1';
170
  } else {
171
- update_option( 'anarcho_cfunctions_error', '0' ); // RESET ERROR VALUE
172
- $duplicates_status = '0';
173
  }
174
 
175
  // Return error status
176
- return $duplicates_status;
177
  }
178
 
179
  /**
180
  * Execute the user code
181
  *
182
- * @since 2.3
183
  */
184
  function MCFunctions_exec() {
185
-
 
 
 
 
 
186
  // Get the error status
187
- $error_status = get_option( 'anarcho_cfunctions_error' );
188
- $duplicates_status = MCFunctions_duplicates();
189
 
190
- if ( $error_status > 0 OR $duplicates_status > 0 ) {
 
191
  return; // EXIT
192
- } else {
193
 
194
- // Get the user entered functions by calling the "prepare" function
195
- $content = MCFunctions_prepare();
 
 
196
 
197
- if ( !empty($content) AND $content != ' ' ) {
 
198
 
199
- // Parsing and execute by Eval
200
- if( false === @eval( $content ) ) {
201
- update_option( 'anarcho_cfunctions_error', '1' ); // ERROR
202
- return; // EXIT
203
- } else {
204
- update_option( 'anarcho_cfunctions_error', '0' ); // RESET ERROR VALUE
205
- }
206
- }
 
 
 
207
  }
208
  }
209
  MCFunctions_exec();
5
  * Description: Easily and safely add your own functions, snippets or any custom codes directly out of your WordPress Dashboard without need of an external editor.
6
  * Author: Arthur Gareginyan
7
  * Author URI: http://www.arthurgareginyan.com
8
+ * Version: 2.4
9
  * License: GPL3
10
  * Text Domain: my-custom-functions
11
  * Domain Path: /languages/
128
  /**
129
  * Prepare the user entered code for execution
130
  *
131
+ * @since 2.4
132
  */
133
+ function MCFunctions_prepare($content) {
134
 
 
 
 
 
135
  // Cleaning
136
  $content = trim( $content );
137
  $content = ltrim( $content, '<?php' );
144
  /**
145
  * Check the user entered code for duplicate names of functions
146
  *
147
+ * @since 2.4
148
  */
149
+ function MCFunctions_duplicates($content) {
150
 
151
  // Find names of user entered functions and check for duplicates
152
+ preg_match_all('/function[\s\n]+(\S+)[\s\n]*\(/i', $content, $user_func_names);
 
153
  $user_func_a = count( $user_func_names[1] );
154
  $user_func_b = count( array_unique( $user_func_names[1] ) );
155
 
159
  $declared_func_internal = array_intersect( $user_func_names[1], $declared_func['internal'] );
160
 
161
  // Update error status
162
+ if ( $user_func_a != $user_func_b OR count( $declared_func_user ) != 0 OR count( $declared_func_internal ) != 0 ) {
163
  update_option( 'anarcho_cfunctions_error', '1' ); // ERROR
164
+ $error_status = '1';
165
  } else {
166
+ update_option( 'anarcho_cfunctions_error', '0' ); // RESET ERROR VALUE
167
+ $error_status = '0';
168
  }
169
 
170
  // Return error status
171
+ return $error_status;
172
  }
173
 
174
  /**
175
  * Execute the user code
176
  *
177
+ * @since 2.4
178
  */
179
  function MCFunctions_exec() {
180
+
181
+ // Read data from DB
182
+ $options = get_option( 'anarcho_cfunctions_settings' );
183
+ $content = isset( $options['anarcho_cfunctions-content'] ) && ! empty( $options['anarcho_cfunctions-content'] ) ? $options['anarcho_cfunctions-content'] : ' ';
184
+ $enable = isset( $options['enable'] ) && !empty( $options['enable'] ) ? $options['enable'] : ' ';
185
+
186
  // Get the error status
187
+ $duplicates = MCFunctions_duplicates($content);
 
188
 
189
+ // If the custom code is disabled...
190
+ if ( $enable == "on") {
191
  return; // EXIT
192
+ }
193
 
194
+ // If the duplicates functions finded...
195
+ if ( $duplicates != 0 ) {
196
+ return; // EXIT
197
+ }
198
 
199
+ // Get the user entered functions by calling the "prepare" function
200
+ $content = MCFunctions_prepare($content);
201
 
202
+ // If content is empty...
203
+ if ( empty($content) OR $content == ' ' ) {
204
+ return; // EXIT
205
+ }
206
+
207
+ // Parsing and execute by Eval
208
+ if( false === @eval( $content ) ) {
209
+ update_option( 'anarcho_cfunctions_error', '1' ); // ERROR
210
+ return; // EXIT
211
+ } else {
212
+ update_option( 'anarcho_cfunctions_error', '0' ); // RESET ERROR VALUE
213
  }
214
  }
215
  MCFunctions_exec();
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: code, php, function, snippet, custom, execute, edit, editing, editor, func
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=8A88KC7TFF6CS
5
  Requires at least: 3.9
6
  Tested up to: 4.5
7
- Stable tag: 2.2
8
  License: GPL3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -17,17 +17,17 @@ An easy to use WordPress plugin that gives you the ability to easily and safely
17
 
18
  = Features =
19
 
20
- **Current features:**
21
-
22
  * Checks the entered code for fatal errors (simple checks)
23
  * Syntax highlighting (by CodeMirror)
24
  * Line numbering (by CodeMirror)
 
25
  * Ready for translation (.pot file included)
26
  * Russian translation
27
 
28
- **Coming soon:**
29
  * Reload the settings page at same position after pushing the save button
30
  * Multisite network support
 
31
  * Easier disable/enable option for WSOD
32
 
33
 
@@ -135,6 +135,9 @@ Commercial licensing (e.g. for projects that can’t use an open-source license)
135
 
136
 
137
  == Changelog ==
 
 
 
138
  = 2.3 =
139
  * Added function to check for duplicate function names. Compares the names of all functions (internal, user).
140
  * Added function for automatic remove the message about successful saving after 3 seconds.
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=8A88KC7TFF6CS
5
  Requires at least: 3.9
6
  Tested up to: 4.5
7
+ Stable tag: 2.3
8
  License: GPL3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
17
 
18
  = Features =
19
 
 
 
20
  * Checks the entered code for fatal errors (simple checks)
21
  * Syntax highlighting (by CodeMirror)
22
  * Line numbering (by CodeMirror)
23
+ * Trigger for temporary disable the custom functions (PHP code)
24
  * Ready for translation (.pot file included)
25
  * Russian translation
26
 
27
+ **Coming soon:**
28
  * Reload the settings page at same position after pushing the save button
29
  * Multisite network support
30
+ * Backup of all custom functions to file
31
  * Easier disable/enable option for WSOD
32
 
33
 
135
 
136
 
137
  == Changelog ==
138
+ = 2.4 =
139
+ * Added trigger for temporary disable the custom functions (PHP code).
140
+ * Functions _prepare, _duplicates, and _exec optimized.
141
  = 2.3 =
142
  * Added function to check for duplicate function names. Compares the names of all functions (internal, user).
143
  * Added function for automatic remove the message about successful saving after 3 seconds.