Skip to content

Commit

Permalink
Merge pull request #9 from RRZE-Webteam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rvdforst authored Nov 28, 2023
2 parents 9055092 + c571424 commit b0c51e6
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 30 deletions.
8 changes: 8 additions & 0 deletions src/Settings/Options/Password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace RRZE\WP\Settings\Options;

class Password extends Field
{
public $template = 'password';
}
53 changes: 53 additions & 0 deletions src/Settings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# RRZE\WP\Settings

The objective of this package is to simplify the process of creating settings pages for WordPress plugins. Traditionally, developers have utilized the Settings API or custom code for this purpose. Although the Settings API functions well, it necessitates substantial setup effort. For instance, you must manually write the HTML code for your options. Additionally, incorporating tabs and tab-sections can become rather complex. This package aims to streamline these tasks and make settings page creation more straightforward.

## Usage

### Basic example

```php
use RRZE\WP\Settings;

$settings = new Settings(__('My Plugin Settings'));

$tab = $settings->addTab(__('General', 'textdomain'));

$section = $tab->addSection('General Section');

$section->addOption('text', [
'name' => 'title',
'label' => __('Title', 'textdomain')
]);

$settings->build();
```

### Initializing the Settings class

```php
use RRZE\WP\Settings;

$settings = new Settings(__('Custom Settings'));
```

The constructor supports two parameters, namely `$title` and `$slug`. By default, the page slug is generated by sanitizing the title. However, if you have a specific slug preference, you can pass it as the second parameter to the constructor.

Additional methods available in this class include:

```php
$settings->setCapability('manage_options')
->setOptionName('my_plugin_option')
->setMenuTitle(__('My Plugin', 'textdomain'))
->setMenuIcon('dashicons-admin-generic')
->setMenuPosition(5)
->setMenuParentSlug('options-general.php');
```

### Tabs

Tabs will be displayed only if there is more than one tab available.

```php
$settings->addTab(__('General', 'textdomain'));
```
5 changes: 2 additions & 3 deletions src/Settings/templates/options/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">

<td class="rrze-wp-form rrze-wp-form-input">
<label>
<input name="<?php echo esc_attr($option->getNameAttribute()); ?>" id="<?php echo $option->getIdAttribute(); ?>" type="checkbox" value="<?php echo $option->getValueAttribute(); ?>" <?php echo $option->is_checked() ? 'checked' : null; ?> class="<?php echo $option->getInputClassAttribute(); ?>">
<?php echo $option->getArg('description'); ?>
Expand Down
7 changes: 2 additions & 5 deletions src/Settings/templates/options/choices.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">

<td class="rrze-wp-form rrze-wp-form-input">
<?php foreach ($option->getArg('options', []) as $key => $label) { ?>
<div>
<label>
Expand All @@ -18,11 +17,9 @@
</label>
</div>
<?php } ?>

<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down
5 changes: 2 additions & 3 deletions src/Settings/templates/options/code-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<textarea name="<?php echo esc_attr($option->getNameAttribute()); ?>" id="<?php echo $option->getIdAttribute(); ?>" class="wp-settings-code-editor <?php echo $option->getInputClassAttribute(); ?>"><?php echo wp_unslash($option->getValueAttribute()); ?></textarea>
<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down
6 changes: 2 additions & 4 deletions src/Settings/templates/options/color.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<input name="<?php echo esc_attr($option->getNameAttribute()); ?>" id="<?php echo $option->getIdAttribute(); ?>" type="text" value="<?php echo $option->getValueAttribute(); ?>" class="wps-color-picker <?php echo $option->getInputClassAttribute(); ?>">

<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down
20 changes: 20 additions & 0 deletions src/Settings/templates/options/password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace RRZE\WP\Settings;

defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="rrze-wp-form rrze-wp-form-password">
<input name="<?php echo esc_attr($option->getNameAttribute()); ?>" id="<?php echo $option->getIdAttribute(); ?>" type="password" value="<?php echo $option->getValueAttribute(); ?>" class="<?php echo $option->getInputClassAttribute(); ?>">
<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>
<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
</td>
</tr>
4 changes: 2 additions & 2 deletions src/Settings/templates/options/select-multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<select id="<?php echo $option->getIdAttribute(); ?>" name="<?php echo esc_attr($option->getNameAttribute()); ?>" multiple class="<?php echo $option->getInputClassAttribute(); ?>">
<?php foreach ($option->getArg('options', []) as $key => $label) { ?>
<option value="<?php echo $key; ?>" <?php echo in_array($key, $option->getValueAttribute() ?? []) ? 'selected' : null; ?>><?php echo $label; ?></option>
Expand Down
5 changes: 2 additions & 3 deletions src/Settings/templates/options/select.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<select id="<?php echo $option->getIdAttribute(); ?>" name="<?php echo esc_attr($option->getNameAttribute()); ?>" class="<?php echo $option->getInputClassAttribute(); ?>">
<?php foreach ($option->getArg('options', []) as $key => $label) { ?>
<option value="<?php echo $key; ?>" <?php selected($option->getValueAttribute(), $key); ?>><?php echo $label; ?></option>
Expand All @@ -17,7 +17,6 @@
<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down
6 changes: 2 additions & 4 deletions src/Settings/templates/options/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<input name="<?php echo esc_attr($option->getNameAttribute()); ?>" id="<?php echo $option->getIdAttribute(); ?>" type="text" value="<?php echo $option->getValueAttribute(); ?>" class="<?php echo $option->getInputClassAttribute(); ?>">

<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down
5 changes: 2 additions & 3 deletions src/Settings/templates/options/textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<textarea name="<?php echo esc_attr($option->getNameAttribute()); ?>" id="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getInputClassAttribute(); ?>"><?php echo $option->getValueAttribute(); ?></textarea>
<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down
5 changes: 2 additions & 3 deletions src/Settings/templates/options/wp-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
defined('ABSPATH') || exit;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<th scope="row" class="rrze-wp-form-label">
<label for="<?php echo $option->getIdAttribute(); ?>" class="<?php echo $option->getLabelClassAttribute(); ?>"><?php echo $option->getLabel(); ?></label>
</th>
<td class="forminp forminp-text">
<td class="rrze-wp-form rrze-wp-form-input">
<?php wp_editor($option->getValueAttribute(), $option->getIdAttribute(), [
'textarea_name' => $option->getNameAttribute(),
'wpautop' => $option->getArg('wpautop', true),
Expand All @@ -27,7 +27,6 @@
<?php if ($description = $option->getArg('description')) { ?>
<p class="description"><?php echo $description; ?></p>
<?php } ?>

<?php if ($error = $option->hasError()) { ?>
<div class="rrze-wp-settings-error"><?php echo $error; ?></div>
<?php } ?>
Expand Down

0 comments on commit b0c51e6

Please sign in to comment.