| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Admin\Forms; |
| 4 |
|
| 5 |
use Photonic_Plugin\Core\Photonic; |
| 6 |
|
| 7 |
if (!current_user_can('edit_posts')) { |
| 8 |
wp_die(esc_html__('You are not authorized to use this capability.', 'photonic')); |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Creates a form in the "Add Media" screen under the new "Photonic" tab. This form lets you insert the gallery shortcode with |
| 13 |
* the right arguments for native WP galleries, Flickr, Google Photos, SmugMug, Zenfolio and Instagram. |
| 14 |
*/ |
| 15 |
class Add_Gallery { |
| 16 |
private static ?Add_Gallery $instance = null; |
| 17 |
|
| 18 |
private function __construct() { |
| 19 |
} |
| 20 |
|
| 21 |
public static function get_instance(): Add_Gallery { |
| 22 |
if (null === self::$instance) { |
| 23 |
self::$instance = new Add_Gallery(); |
| 24 |
} |
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
public function build_form() { |
| 29 |
global $photonic_alternative_shortcode; |
| 30 |
$shortcode = empty($photonic_alternative_shortcode) ? 'gallery' : $photonic_alternative_shortcode; |
| 31 |
|
| 32 |
$selected_tab = sanitize_text_field($_GET['photonic-tab'] ?? 'default'); // phpcs:ignore WordPress.Security.NonceVerification |
| 33 |
if (!in_array($selected_tab, ['default', 'flickr', 'google', 'smugmug', 'zenfolio', 'instagram'], true)) { |
| 34 |
$selected_tab = 'default'; |
| 35 |
} |
| 36 |
|
| 37 |
?> |
| 38 |
<script type="text/javascript"> |
| 39 |
jQuery(document).ready(function ($) { |
| 40 |
window.photonicAdminHtmlEncode = function photonicAdminHtmlEncode(value) { |
| 41 |
return $('<div/>').text(value).html(); |
| 42 |
}; |
| 43 |
|
| 44 |
$('#photonic-shortcode-form input[type="text"], #photonic-shortcode-form select').change(function () { |
| 45 |
var comboValues = $('#photonic-shortcode-form').serializeArray(); |
| 46 |
var newValues = []; |
| 47 |
var len = comboValues.length; |
| 48 |
|
| 49 |
$(comboValues).each(function (i, obj) { |
| 50 |
var individual = this; |
| 51 |
if (individual['name'].trim() !== 'photonic-shortcode' && individual['name'].trim() !== 'photonic-submit' && |
| 52 |
individual['name'].trim() !== 'photonic-cancel' && individual['value'].trim() !== '') { |
| 53 |
newValues.push(individual['name'] + "='" + photonicAdminHtmlEncode(decodeURIComponent(individual['value'].trim())) + "'"); |
| 54 |
} |
| 55 |
}); |
| 56 |
|
| 57 |
var shortcode = "[<?php echo esc_html($shortcode); ?> type='<?php echo esc_attr($selected_tab); ?>' "; |
| 58 |
$(newValues).each(function () { |
| 59 |
shortcode += this + ' '; |
| 60 |
}); |
| 61 |
shortcode += ']'; |
| 62 |
|
| 63 |
$('#photonic-preview').text(shortcode); |
| 64 |
$('#photonic-shortcode').val(shortcode); |
| 65 |
}); |
| 66 |
$('#photonic-shortcode-form select').change(); |
| 67 |
}); |
| 68 |
</script> |
| 69 |
<?php |
| 70 |
require_once PHOTONIC_PATH . '/Admin/Forms/Vanilla_Form.php'; |
| 71 |
$form = Vanilla_Form::get_instance(); |
| 72 |
$fields = $form->get_fields(); |
| 73 |
|
| 74 |
echo "<form id='photonic-shortcode-form' method='post' action=''>"; |
| 75 |
$this->build_tabs($selected_tab, $fields); |
| 76 |
} |
| 77 |
|
| 78 |
private function build_tabs($selected_tab, $fields) { |
| 79 |
$tab_list = ''; |
| 80 |
$field_list = []; |
| 81 |
$prelude = ''; |
| 82 |
|
| 83 |
$user = get_current_user_id(); |
| 84 |
if (0 === $user) { |
| 85 |
$user = wp_rand(1); |
| 86 |
} |
| 87 |
|
| 88 |
foreach ($fields as $tab => $field_group) { |
| 89 |
$tab_list .= "<li><a href='" . esc_url(add_query_arg(['photonic-tab' => $tab, 'nonce' => wp_create_nonce('photonic-vanilla-form-' . $user)])) . "' class='" . ($tab === $selected_tab ? 'current' : '') . "'>" . esc_attr($field_group['name']) . "</a> | </li>"; |
| 90 |
if ($tab === $selected_tab) { |
| 91 |
$field_list = $field_group['fields']; |
| 92 |
$prelude = $field_group['prelude'] ?? ''; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
echo "<ul class='subsubsub'>"; |
| 97 |
if (strlen($tab_list) > 8) { |
| 98 |
$tab_list = substr($tab_list, 0, -8); |
| 99 |
} |
| 100 |
echo wp_kses_post($tab_list); |
| 101 |
echo "</ul>"; |
| 102 |
|
| 103 |
if (!empty($prelude)) { |
| 104 |
echo "<p class='prelude'>"; |
| 105 |
echo wp_kses_post($prelude); |
| 106 |
echo "</p>"; |
| 107 |
} |
| 108 |
|
| 109 |
$this->build_table($field_list); |
| 110 |
$this->show_shortcode_preview(); |
| 111 |
} |
| 112 |
|
| 113 |
private function build_table($field_list) { |
| 114 |
echo "<table class='photonic-form'>"; |
| 115 |
foreach ($field_list as $field) { |
| 116 |
echo "<tr>"; |
| 117 |
echo wp_kses_post("<th scope='row'>{$field['name']} " . (isset($field['req']) && $field['req'] ? '(*)' : '') . " </th>"); |
| 118 |
switch ($field['type']) { |
| 119 |
case 'text': |
| 120 |
echo "<td><input type='text' name='" . esc_attr($field['id']) . "' value='" . esc_attr($field['std'] ?? '') . "' /></td>"; |
| 121 |
break; |
| 122 |
|
| 123 |
case 'select': |
| 124 |
echo "<td><select name='" . esc_attr($field['id']) . "'>"; |
| 125 |
$default = $field['std'] ?? ''; |
| 126 |
foreach ($field['options'] as $option_name => $option_value) { |
| 127 |
if ($option_name === $default) { |
| 128 |
$selected = 'selected'; |
| 129 |
} |
| 130 |
else { |
| 131 |
$selected = ''; |
| 132 |
} |
| 133 |
echo "<option value='" . esc_attr($option_name) . "' " . esc_attr($selected) . ">" . esc_attr($option_value) . "</option>"; |
| 134 |
} |
| 135 |
echo "</select></td>"; |
| 136 |
break; |
| 137 |
|
| 138 |
case 'raw': |
| 139 |
echo "<td>" . wp_kses($field['std'], ['select' => ['name'], 'option' => ['value', 'selected']]) . "</td>"; |
| 140 |
break; |
| 141 |
} |
| 142 |
echo "<td class='hint'>" . wp_kses_post($field['hint'] ?? '') . "</td>"; |
| 143 |
echo "</tr>"; |
| 144 |
} |
| 145 |
echo "</table>"; |
| 146 |
} |
| 147 |
|
| 148 |
private function show_shortcode_preview() { |
| 149 |
echo "<div class='preview'>"; |
| 150 |
echo "<script type='text/javascript'></script>"; |
| 151 |
echo "<h4>" . esc_html__('Shortcode preview', 'photonic') . "</h4>"; |
| 152 |
echo "<pre class='html' id='photonic-preview' name='photonic-preview'></pre>"; |
| 153 |
echo "<input type='hidden' id='photonic-shortcode' name='photonic-shortcode' />"; |
| 154 |
echo "</div>"; |
| 155 |
|
| 156 |
echo "<div class='button-panel'>"; |
| 157 |
echo wp_kses(get_submit_button(esc_html__('Insert into post', 'photonic'), 'primary', 'photonic-submit', false), Photonic::$safe_tags); |
| 158 |
echo wp_kses(get_submit_button(esc_html__('Cancel', 'photonic'), 'delete', 'photonic-cancel', false), Photonic::$safe_tags); |
| 159 |
echo "</div>"; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (current_user_can('edit_posts') && isset($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], 'photonic-vanilla-form-' . get_current_user_id())) { |
| 164 |
if (isset($_POST['photonic-submit'])) { |
| 165 |
$photonic_user_shortcode = stripslashes(sanitize_text_field($_POST['photonic-shortcode'] ?? '')); |
| 166 |
media_send_to_editor($photonic_user_shortcode); |
| 167 |
return; |
| 168 |
} |
| 169 |
elseif (isset($_POST['photonic-cancel'])) { |
| 170 |
media_send_to_editor(''); |
| 171 |
return; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$photonic_add_gallery_form = Add_Gallery::get_instance(); |
| 176 |
$photonic_add_gallery_form->build_form(); |
| 177 |
|