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