number field for restricting the textarea chars
authorLaury GvR <laury@gaslightmedia.com>
Wed, 1 Mar 2017 21:16:02 +0000 (16:16 -0500)
committerLaury GvR <laury@gaslightmedia.com>
Wed, 1 Mar 2017 21:16:02 +0000 (16:16 -0500)
Number field type added for the creation of a character amount
restriction to be used for the textarea field, which can be edited
in the admin side if the user has manage_options - except it seems
to currently pass current_user_can too easily, no matter what it
checks against.

controllers/Admin.php
views/number.php [new file with mode: 0644]
views/text.php
views/textArea.php

index 9e5ac9e..a58b565 100644 (file)
@@ -98,6 +98,9 @@ class Glmsimplemessageboard_Admin_Controller
     public function glmsimplemessageboardAddSettingField($name, $label, $type, $simpleMessageboardSection)
     {
         switch ($type) {
+            case 'number':
+                $callback = 'glmsimplemessageboardRenderNumber';
+                break;
             case 'text':
                 $callback = 'glmsimplemessageboardRenderText';
                 break;
@@ -151,6 +154,11 @@ class Glmsimplemessageboard_Admin_Controller
                 'label' => 'Message',
                 'type'  => 'textarea'
             ),
+            array(
+                'name'  => 'characterlimit',
+                'label' => 'Character Limit',
+                'type'  => 'number'
+            ),
         );
         
         foreach ($fieldNames as $field) {
@@ -164,6 +172,15 @@ class Glmsimplemessageboard_Admin_Controller
         }
     }
 
+    public function glmsimplemessageboardRenderNumber($fieldName)
+    {
+        static $options;
+        if (!$options) {
+            $options = get_option(GLM_SIMPLEMESSAGEBOARD_SETTINGS);
+        }
+        include $this->path . 'views/number.php';
+    }
+
     public function glmsimplemessageboardRenderText($fieldName)
     {
         static $options;
diff --git a/views/number.php b/views/number.php
new file mode 100644 (file)
index 0000000..9d565ad
--- /dev/null
@@ -0,0 +1,9 @@
+
+
+<?php 
+// This does not seem to restrct on privileges properly
+// e.g. current_user_can("bluriglurp") also passes as true
+if ( current_user_can( "manage_options" ) ) { ?>
+    <input type="number" min="0" step="1" max="99999" name="glmsimplemessageboard_settings[<?php echo $fieldName;?>]"
+        value="<?php echo str_replace( '"', '&quote;', $options[$fieldName] ); ?>">
+<?php } ?>
index d58813c..6342536 100644 (file)
@@ -1,3 +1,2 @@
 <input type="text" name="glmsimplemessageboard_settings[<?php echo $fieldName;?>]"
-    value="<?php echo str_replace('"', '&quote;', $options[$fieldName]); ?>">
-<?php 
\ No newline at end of file
+    value="<?php echo str_replace('"', '&quote;', $options[$fieldName]); ?>">
\ No newline at end of file
index 7c5d976..fdd41e3 100644 (file)
@@ -1,9 +1,19 @@
-<textarea class='message' cols="40" maxlength="220" rows="5" name="glmsimplemessageboard_settings[<?php echo $fieldName;?>]"><?php echo htmlspecialchars($options[$fieldName]); ?></textarea>
+<?php
+    if ($options && array_key_exists( 'characterlimit', $options ) ) {
+        $characterLimit = $options['characterlimit'];
+    } else {
+        $characterLimit = "250";
+    }
+?>
+
+<textarea class='message' cols="40" maxlength="<?php echo $characterLimit ?>" rows="5" name="glmsimplemessageboard_settings[<?php echo $fieldName;?>]"><?php echo htmlspecialchars($options[$fieldName]); ?></textarea>
 <br><span class="countdown"></span>
 <script type="text/javascript">
+    
+    var characterLimit = <?php echo $characterLimit ?>;
+     
     function updateCountdown() {
-        // 220 is the max message length
-        var remaining = 220 - jQuery('.message').val().length;
+        var remaining = characterLimit - jQuery('.message').val().length;
         jQuery('.countdown').text(remaining + ' characters remaining.');
     }
     jQuery(document).ready(function($) {