| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Class UICategories |
| 10 |
* |
| 11 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 12 |
*/ |
| 13 |
class CategoryOptions |
| 14 |
{ |
| 15 |
private static $_instance = null; |
| 16 |
|
| 17 |
public static function getInstance() |
| 18 |
{ |
| 19 |
if (null === self::$_instance) { |
| 20 |
self::$_instance = new CategoryOptions(); |
| 21 |
} |
| 22 |
return self::$_instance; |
| 23 |
} |
| 24 |
|
| 25 |
private function __construct() |
| 26 |
{ |
| 27 |
// Add additional actions |
| 28 |
add_action('f12_cf7_doubleoptin_ui_table_options', array($this, 'addOption')); |
| 29 |
|
| 30 |
add_action('admin_init', array($this, 'init')); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* After initializing the WordPress Admi |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function init() |
| 39 |
{ |
| 40 |
if (isset($_POST['update-category'])) { |
| 41 |
$this->update_categories(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Assign the new categories to the selected opt ins. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
private function update_categories() |
| 51 |
{ |
| 52 |
if (!\wp_verify_nonce($_POST['f12_cf7_doubleoptin_ui_table_options_nonce'], 'f12_cf7_doubleoptin_ui_table_options_action')) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
if (!isset($_POST['optin-id']) || !is_array($_POST['optin-id'])) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if (!isset($_POST['category'])) { |
| 61 |
return; |
| 62 |
} |
| 63 |
$category_id = (int)$_POST['category']; |
| 64 |
|
| 65 |
$Category = Category::get_by_id($category_id); |
| 66 |
|
| 67 |
if (null == $Category && 0 !== $category_id) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$optinIds = array_map('intval', $_POST['optin-id']); |
| 72 |
|
| 73 |
foreach ($optinIds as $id) { |
| 74 |
OptIn::update_category_by_id($id, $category_id); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Return an Select Field for Categories |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function select_category($selected = 0) |
| 84 |
{ |
| 85 |
$atts = array( |
| 86 |
'perPage' => -1, |
| 87 |
'orderBy' => 'name', |
| 88 |
'order' => 'ASC' |
| 89 |
); |
| 90 |
|
| 91 |
$list = Category::get_list($atts, $numberOfPages); |
| 92 |
|
| 93 |
$response = '<option value="0">' . __('Please select', 'double-opt-in') . '</option>'; |
| 94 |
foreach ($list as $Category/** @var Category $Category */) { |
| 95 |
$selected_html = $selected == $Category->get_id() ? 'selected="selected"' : ''; |
| 96 |
$response .= '<option value="' . esc_attr($Category->get_id()) . '" '.$selected_html.'>' . esc_attr($Category->get_name()) . '</option>'; |
| 97 |
} |
| 98 |
return '<select name="category">' . $response . '</select>'; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add a Option to update the given Categories |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function addOption() |
| 107 |
{ |
| 108 |
?> |
| 109 |
<li> |
| 110 |
<?php _e('Change Category to: '); ?> |
| 111 |
<?php echo wp_kses($this->select_category(), array('select' => array('name' => array()), 'option' => array('value' => array(), 'selected' => array()))); ?> |
| 112 |
<input type="submit" class="button" name="update-category" |
| 113 |
value="<?php _e('Update', 'double-opt-in'); ?>"/> |
| 114 |
</li> |
| 115 |
<?php |
| 116 |
} |
| 117 |
} |
| 118 |
} |