| 1 |
<?php |
| 2 |
|
| 3 |
function wpatt_plugin_options() |
| 4 |
{ |
| 5 |
if (!current_user_can('manage_options')) { |
| 6 |
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'wp-attachments')); |
| 7 |
} |
| 8 |
|
| 9 |
$updated = false; |
| 10 |
|
| 11 |
// Handle general settings submission securely |
| 12 |
if (isset($_POST['submit-general'])) { |
| 13 |
check_admin_referer('wpatt_general_settings'); |
| 14 |
|
| 15 |
update_option('wpatt_option_localization', sanitize_text_field($_POST["wpatt_option_localization_n"] ?? '')); |
| 16 |
update_option('wpatt_option_date_localization', sanitize_text_field($_POST["wpatt_option_date_localization_n"] ?? '')); |
| 17 |
|
| 18 |
update_option('wpatt_show_orderby', !empty($_POST['wpatt_show_orderby_n']) ? '1' : '0'); |
| 19 |
update_option('wpatt_option_includeimages', !empty($_POST['wpatt_option_includeimages_n']) ? '1' : '0'); |
| 20 |
update_option('wpatt_option_targetblank', !empty($_POST['wpatt_option_targetblank_n']) ? '1' : '0'); |
| 21 |
update_option('wpatt_option_restrictload', !empty($_POST['wpatt_option_restrictload_n']) ? '1' : '0'); |
| 22 |
update_option('wpatt_counter', !empty($_POST['wpatt_counter_n']) ? '1' : '0'); |
| 23 |
update_option('wpatt_excludelogged_counter', !empty($_POST['wpatt_excludelogged_counter_n']) ? '1' : '0'); |
| 24 |
|
| 25 |
// Save per-post-type metabox and default display settings |
| 26 |
$post_types = get_post_types(['public' => true], 'objects'); |
| 27 |
foreach ($post_types as $post_type) { |
| 28 |
if ($post_type->name === 'attachment') continue; |
| 29 |
|
| 30 |
// Save metabox enabled/disabled |
| 31 |
$metabox_enabled = !empty($_POST['wpatt_enable_' . $post_type->name]) ? '1' : '0'; |
| 32 |
update_option('wpatt_enable_metabox_' . $post_type->name, $metabox_enabled); |
| 33 |
|
| 34 |
// Save default display enabled/disabled |
| 35 |
$display_enabled = !empty($_POST['wpatt_defaulton_' . $post_type->name]) ? '1' : '0'; |
| 36 |
update_option('wpatt_enable_display_' . $post_type->name, $display_enabled); |
| 37 |
} |
| 38 |
$updated = true; |
| 39 |
} |
| 40 |
|
| 41 |
// Handle appearance settings submission securely |
| 42 |
if (isset($_POST['submit-appearance'])) { |
| 43 |
check_admin_referer('wpatt_appearance_settings'); |
| 44 |
|
| 45 |
update_option('wpa_ict', sanitize_text_field($_POST['style'] ?? '')); |
| 46 |
update_option('wpa_template', sanitize_text_field($_POST['template'] ?? '')); |
| 47 |
update_option('wpa_template_custom', wp_kses_post($_POST['wpa_template_custom'] ?? '')); |
| 48 |
$updated = true; |
| 49 |
} |
| 50 |
|
| 51 |
if ($updated) { |
| 52 |
add_settings_error('wpatt_messages', 'wpatt_message', __('Settings Saved', 'wp-attachments'), 'updated'); |
| 53 |
} |
| 54 |
|
| 55 |
wpa_register_initial_settings(); |
| 56 |
|
| 57 |
echo '<div class="wrap wpatt-settings-wrap">'; |
| 58 |
|
| 59 |
echo '<div style="float:right; margin-top: 20px;"> |
| 60 |
<a href="https://wordpress.org/support/plugin/wp-attachments/reviews/#new-post" target="_blank" class="button">' . esc_html__('Rate this plugin � |
| 61 |
� |
| 62 |
� |
| 63 |
� |
| 64 |
� |
| 65 |
', 'wp-attachments') . '</a> |
| 66 |
<a href="https://wordpress.org/plugins/wp-attachments/#developers" target="_blank" class="button">' . esc_html__('Changelog', 'wp-attachments') . '</a> |
| 67 |
</div>'; |
| 68 |
|
| 69 |
echo '<h1>' . esc_html__('WP Attachments Settings', 'wp-attachments') . '</h1>'; |
| 70 |
|
| 71 |
settings_errors('wpatt_messages'); |
| 72 |
|
| 73 |
echo '<style> |
| 74 |
.wpatt-settings-wrap .nav-tab-wrapper { margin-bottom: 24px; } |
| 75 |
.wpatt-settings-wrap .form-table th { width: 220px; vertical-align: top; padding-top: 16px; } |
| 76 |
.wpatt-settings-wrap .form-table td { padding-top: 12px; } |
| 77 |
.wpatt-settings-wrap input[type="text"] { width: 100%; max-width: 400px; } |
| 78 |
.wpatt-settings-wrap .wpatt-desc { color: #666; font-size: 0.9em; margin-top: 4px; display: block; } |
| 79 |
.wpatt-settings-wrap .wpatt-checkbox-group label { display: block; margin-top: 8px; } |
| 80 |
.wpatt-settings-wrap .wpatt-template-radio { margin-bottom: 20px; display: block; padding: 12px; background: #fff; border: 1px solid #ccd0d4; border-radius: 4px; } |
| 81 |
.wpatt-settings-wrap .wpatt-template-radio:hover { background: #f6f7f7; } |
| 82 |
.wpatt-settings-wrap .wpatt-template-radio input[type="radio"] { margin-right: 10px; } |
| 83 |
.wpatt-settings-wrap textarea { font-family: monospace; width: 100%; max-width: 600px; } |
| 84 |
.wpatt-cpt-table { border-collapse: collapse; width: 100%; max-width: 600px; margin-top: 10px; background: #fff; border: 1px solid #ccd0d4; } |
| 85 |
.wpatt-cpt-table th, .wpatt-cpt-table td { text-align: left; padding: 10px; border-bottom: 1px solid #ccd0d4; } |
| 86 |
.wpatt-cpt-table th { background: #f6f7f7; font-weight: 600; } |
| 87 |
.wpatt-cpt-table tr:last-child td { border-bottom: none; } |
| 88 |
</style>'; |
| 89 |
|
| 90 |
$current = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : 'general'; |
| 91 |
|
| 92 |
$tabs = array( |
| 93 |
'general' => esc_html__('General Settings', 'wp-attachments'), |
| 94 |
'appearance' => esc_html__('Appearance & Templates', 'wp-attachments') |
| 95 |
); |
| 96 |
|
| 97 |
echo '<h2 class="nav-tab-wrapper">'; |
| 98 |
foreach ($tabs as $tab => $name) { |
| 99 |
$class = ($tab === $current) ? ' nav-tab-active' : ''; |
| 100 |
echo "<a class='nav-tab{$class}' href='" . esc_url(add_query_arg('tab', $tab)) . "'>{$name}</a>"; |
| 101 |
} |
| 102 |
echo '</h2>'; |
| 103 |
|
| 104 |
echo '<form method="post" action="">'; |
| 105 |
|
| 106 |
switch ($current) { |
| 107 |
case 'general': |
| 108 |
wp_nonce_field('wpatt_general_settings'); |
| 109 |
echo '<table class="form-table">'; |
| 110 |
echo '<tr valign="top"> |
| 111 |
<th scope="row">' . esc_html__('List Header', 'wp-attachments') . '</th> |
| 112 |
<td> |
| 113 |
<input type="text" name="wpatt_option_localization_n" value="' . esc_attr(get_option('wpatt_option_localization')) . '" /> |
| 114 |
<span class="wpatt-desc">' . esc_html__('Text displayed above the attachments list (e.g., "Downloads" or "Attachments").', 'wp-attachments') . '</span> |
| 115 |
</td> |
| 116 |
</tr>'; |
| 117 |
|
| 118 |
echo '<tr valign="top"> |
| 119 |
<th scope="row">' . esc_html__('Display Options', 'wp-attachments') . '</th> |
| 120 |
<td> |
| 121 |
<div class="wpatt-checkbox-group"> |
| 122 |
<label> |
| 123 |
<input type="checkbox" name="wpatt_show_orderby_n" ' . (get_option('wpatt_show_orderby') == '1' ? 'checked' : '') . '/> |
| 124 |
' . esc_html__('Show the sort links above the list (Date, Name)', 'wp-attachments') . ' |
| 125 |
</label> |
| 126 |
<label> |
| 127 |
<input type="checkbox" name="wpatt_option_includeimages_n" ' . (get_option('wpatt_option_includeimages') == '1' ? 'checked' : '') . '/> |
| 128 |
' . esc_html__('Include images in the attachments list (.jpg, .png, etc.)', 'wp-attachments') . ' |
| 129 |
</label> |
| 130 |
<label> |
| 131 |
<input type="checkbox" name="wpatt_option_targetblank_n" ' . (get_option('wpatt_option_targetblank') == '1' ? 'checked' : '') . '/> |
| 132 |
' . esc_html__('Open links in a new tab', 'wp-attachments') . ' |
| 133 |
</label> |
| 134 |
<label> |
| 135 |
<input type="checkbox" name="wpatt_option_restrictload_n" ' . (get_option('wpatt_option_restrictload') == '1' ? 'checked' : '') . '/> |
| 136 |
' . esc_html__('Restrict loading to single posts/pages only (disable on archives/home)', 'wp-attachments') . ' |
| 137 |
</label> |
| 138 |
</div> |
| 139 |
</td> |
| 140 |
</tr>'; |
| 141 |
|
| 142 |
echo '<tr valign="top"> |
| 143 |
<th scope="row">' . esc_html__('Date Format', 'wp-attachments') . '</th> |
| 144 |
<td> |
| 145 |
<input type="text" name="wpatt_option_date_localization_n" value="' . esc_attr(get_option('wpatt_option_date_localization')) . '" /> |
| 146 |
<span class="wpatt-desc">' . sprintf( |
| 147 |
esc_html__('Format for the %%DATE%% tag, following PHP date standards. Leave empty to use the WordPress setting. Default: %s', 'wp-attachments'), |
| 148 |
'<code>d.m.Y</code>' |
| 149 |
) . '</span> |
| 150 |
</td> |
| 151 |
</tr>'; |
| 152 |
|
| 153 |
echo '<tr valign="top"> |
| 154 |
<th scope="row">' . esc_html__('Download Tracker', 'wp-attachments') . '</th> |
| 155 |
<td> |
| 156 |
<div class="wpatt-checkbox-group"> |
| 157 |
<label> |
| 158 |
<input type="checkbox" name="wpatt_counter_n" ' . (get_option('wpatt_counter') == '1' ? 'checked' : '') . '/> |
| 159 |
' . esc_html__('Enable download counter', 'wp-attachments') . ' |
| 160 |
</label> |
| 161 |
<label> |
| 162 |
<input type="checkbox" name="wpatt_excludelogged_counter_n" ' . (get_option('wpatt_excludelogged_counter') == '1' ? 'checked' : '') . '/> |
| 163 |
' . esc_html__('Exclude logged-in users from counting', 'wp-attachments') . ' |
| 164 |
</label> |
| 165 |
</div> |
| 166 |
</td> |
| 167 |
</tr>'; |
| 168 |
|
| 169 |
echo '<tr valign="top"> |
| 170 |
<th scope="row">' . esc_html__('Post Type Permissions', 'wp-attachments') . '</th> |
| 171 |
<td> |
| 172 |
<p class="description">' . esc_html__('Configure where WP Attachments should be available and if they should show by default.', 'wp-attachments') . '</p> |
| 173 |
<table class="wpatt-cpt-table"> |
| 174 |
<thead> |
| 175 |
<tr> |
| 176 |
<th>' . esc_html__('Post Type', 'wp-attachments') . '</th> |
| 177 |
<th>' . esc_html__('Enable Metabox', 'wp-attachments') . '</th> |
| 178 |
<th>' . esc_html__('Display Default', 'wp-attachments') . '</th> |
| 179 |
</tr> |
| 180 |
</thead> |
| 181 |
<tbody>'; |
| 182 |
$post_types = get_post_types(['public' => true], 'objects'); |
| 183 |
foreach ($post_types as $post_type) { |
| 184 |
if ($post_type->name === 'attachment') continue; |
| 185 |
$mb_enabled = get_option('wpatt_enable_metabox_' . $post_type->name, '1'); |
| 186 |
$disp_enabled = get_option('wpatt_enable_display_' . $post_type->name, '1'); |
| 187 |
echo '<tr> |
| 188 |
<td><strong>' . esc_html($post_type->labels->singular_name) . '</strong></td> |
| 189 |
<td style="text-align:center;"> |
| 190 |
<input type="checkbox" name="wpatt_enable_' . esc_attr($post_type->name) . '" value="1" ' . checked($mb_enabled, '1', false) . ' /> |
| 191 |
</td> |
| 192 |
<td style="text-align:center;"> |
| 193 |
<input type="checkbox" name="wpatt_defaulton_' . esc_attr($post_type->name) . '" value="1" ' . checked($disp_enabled, '1', false) . ' /> |
| 194 |
</td> |
| 195 |
</tr>'; |
| 196 |
} |
| 197 |
echo '</tbody></table> |
| 198 |
</td> |
| 199 |
</tr>'; |
| 200 |
echo '</table>'; |
| 201 |
submit_button(__('Save General Settings', 'wp-attachments'), 'primary', 'submit-general'); |
| 202 |
break; |
| 203 |
|
| 204 |
case 'appearance': |
| 205 |
wp_nonce_field('wpatt_appearance_settings'); |
| 206 |
echo '<table class="form-table">'; |
| 207 |
echo '<tr valign="top"> |
| 208 |
<th scope="row">' . esc_html__('Icon Pack', 'wp-attachments') . '</th> |
| 209 |
<td> |
| 210 |
<fieldset>'; |
| 211 |
$icon_packs = [ |
| 212 |
0 => ['label' => 'Fugue Icons', 'author' => 'Yusuke Kamiyamane'], |
| 213 |
1 => ['label' => 'Crystal Clear', 'author' => 'Everaldo Coelho', 'url' => 'https://www.everaldo.com/'], |
| 214 |
2 => ['label' => 'Diagona Icons', 'author' => 'Asher Abbasi'], |
| 215 |
3 => ['label' => 'Page Icons', 'author' => 'Matthew Skiles', 'url' => 'https://iconblock.com/'], |
| 216 |
]; |
| 217 |
foreach ($icon_packs as $val => $pack) { |
| 218 |
$checked = (intval(get_option('wpa_ict')) === $val) ? 'checked' : ''; |
| 219 |
echo '<label class="wpatt-template-radio"> |
| 220 |
<input type="radio" value="' . esc_attr($val) . '" name="style" ' . $checked . '> |
| 221 |
<strong>' . esc_html($pack['label']) . '</strong> |
| 222 |
<span style="float:right;"> |
| 223 |
<img src="' . esc_url(plugins_url('wp-attachments/styles/' . $val . '/document.png')) . '" style="vertical-align:middle;"/> |
| 224 |
<img src="' . esc_url(plugins_url('wp-attachments/styles/' . $val . '/document-word.png')) . '" style="vertical-align:middle;"/> |
| 225 |
<img src="' . esc_url(plugins_url('wp-attachments/styles/' . $val . '/document-pdf.png')) . '" style="vertical-align:middle;"/> |
| 226 |
</span> |
| 227 |
<br><small class="wpatt-desc">' . esc_html__('Author', 'wp-attachments') . ': ' . (isset($pack['url']) ? '<a href="' . esc_url($pack['url']) . '" target="_blank">' . esc_html($pack['author']) . '</a>' : esc_html($pack['author'])) . '</small> |
| 228 |
</label>'; |
| 229 |
} |
| 230 |
echo '</fieldset></td></tr>'; |
| 231 |
|
| 232 |
echo '<tr valign="top"> |
| 233 |
<th scope="row">' . esc_html__('Display Template', 'wp-attachments') . '</th> |
| 234 |
<td> |
| 235 |
<fieldset>'; |
| 236 |
$templates = [ |
| 237 |
0 => [ |
| 238 |
'label' => 'Simple List', |
| 239 |
'code' => '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small>', |
| 240 |
], |
| 241 |
1 => [ |
| 242 |
'label' => 'List with date', |
| 243 |
'code' => '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small> <span class="wpa-attachment-date">%DATE%</span>', |
| 244 |
], |
| 245 |
2 => [ |
| 246 |
'label' => 'Detailed List', |
| 247 |
'code' => '<a href="%URL%">%TITLE%</a> <small>• %SIZE% • %DOWNLOADS% clicks</small> <span class="wpa-attachment-date">%DATE%</span><br><small>%CAPTION%</small>', |
| 248 |
], |
| 249 |
]; |
| 250 |
foreach ($templates as $val => $tpl) { |
| 251 |
$checked = (intval(get_option('wpa_template')) === $val) ? 'checked' : ''; |
| 252 |
echo '<label class="wpatt-template-radio"> |
| 253 |
<input type="radio" value="' . esc_attr($val) . '" name="template" ' . $checked . '> <strong>' . esc_html($tpl['label']) . '</strong> |
| 254 |
<br><code style="background:transparent; padding:0;">' . esc_html($tpl['code']) . '</code> |
| 255 |
</label>'; |
| 256 |
} |
| 257 |
echo '<label class="wpatt-template-radio"> |
| 258 |
<input type="radio" value="3" name="template" ' . (intval(get_option('wpa_template')) === 3 ? 'checked' : '') . '> <strong>' . esc_html__('Custom Template', 'wp-attachments') . '</strong> |
| 259 |
<textarea name="wpa_template_custom" rows="4">' . esc_textarea(get_option('wpa_template_custom')) . '</textarea> |
| 260 |
<br><span class="wpatt-desc">' . esc_html__('Tags:', 'wp-attachments') . ' <code>%URL%</code>, <code>%TITLE%</code>, <code>%SIZE%</code>, <code>%DATE%</code>, <code>%DOWNLOADS%</code>, <code>%CAPTION%</code></span> |
| 261 |
</label>'; |
| 262 |
echo '</fieldset></td></tr>'; |
| 263 |
echo '</table>'; |
| 264 |
submit_button(__('Save Appearance Settings', 'wp-attachments'), 'primary', 'submit-appearance'); |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
echo '</form>'; |
| 269 |
echo '</div>'; |
| 270 |
} |
| 271 |
|
| 272 |
?> |
| 273 |
|