| 1 |
<?php |
| 2 |
/** |
| 3 |
* AVCP Single Post Content Filter |
| 4 |
* |
| 5 |
* Enhances the content display for AVCP post types with additional metadata |
| 6 |
* and contractor information. |
| 7 |
* |
| 8 |
* @package AVCP |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Prevent direct access |
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* AVCP Single Post Content Filter |
| 19 |
* |
| 20 |
* Enhances the content display for AVCP post types with additional metadata |
| 21 |
* and contractor information. |
| 22 |
* |
| 23 |
* @package AVCP |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
|
| 27 |
// Prevent direct access |
| 28 |
if (!defined('ABSPATH')) { |
| 29 |
exit; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Generates the basic contract information table |
| 34 |
* |
| 35 |
* @param int $post_id The post ID |
| 36 |
* @param array $options Cached options array |
| 37 |
* @return string HTML table content |
| 38 |
*/ |
| 39 |
function avcp_generate_contract_info_table($post_id, $options) { |
| 40 |
$html = '<table>'; |
| 41 |
|
| 42 |
// CIG |
| 43 |
$html .= '<tr><td><acronym title="' . esc_attr__('Codice Identificativo Gara', 'avcp') . '">CIG:</acronym></td><td>' . esc_html(get_post_meta($post_id, 'avcp_cig', true)) . '</td></tr>'; |
| 44 |
|
| 45 |
// Proposing structure |
| 46 |
$html .= '<tr><td>' . esc_html__('Struttura proponente:', 'avcp') . '</td><td>' . esc_html($options['denominazione_ente']); |
| 47 |
|
| 48 |
// Area sectors |
| 49 |
$html .= avcp_generate_area_sectors($post_id); |
| 50 |
|
| 51 |
$html .= '<br/>' . esc_html($options['codicefiscale_ente']) . '</td></tr>'; |
| 52 |
|
| 53 |
// Contract details |
| 54 |
$html .= '<tr><td>' . esc_html__('Oggetto del bando:', 'avcp') . '</td><td>' . esc_html(get_the_title($post_id)) . '</td></tr>'; |
| 55 |
|
| 56 |
// Check if avcp_get_contraente function exists |
| 57 |
if (function_exists('avcp_get_contraente')) { |
| 58 |
$contraente = avcp_get_contraente(get_post_meta($post_id, 'avcp_contraente', true)); |
| 59 |
$html .= '<tr><td>' . esc_html__('Procedura di scelta del contraente:', 'avcp') . '</td><td>' . esc_html(strtolower($contraente)) . '</td></tr>'; |
| 60 |
} |
| 61 |
|
| 62 |
// Award amount |
| 63 |
$aggiudicazione_amount = get_post_meta($post_id, 'avcp_aggiudicazione', true); |
| 64 |
$html .= '<tr><td>' . esc_html__('Importo di aggiudicazione:', 'avcp') . '</td><td>€ <strong>' . esc_html($aggiudicazione_amount) . '</strong></td></tr>'; |
| 65 |
|
| 66 |
// Dates |
| 67 |
$html .= avcp_generate_contract_dates($post_id); |
| 68 |
|
| 69 |
// Liquidated amounts |
| 70 |
$html .= avcp_generate_liquidated_amounts($post_id); |
| 71 |
|
| 72 |
// Reference year |
| 73 |
$html .= avcp_generate_reference_year($post_id, $options); |
| 74 |
|
| 75 |
$html .= '</table>'; |
| 76 |
|
| 77 |
return $html; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Generates area sectors information |
| 82 |
* |
| 83 |
* @param int $post_id The post ID |
| 84 |
* @return string HTML content for area sectors |
| 85 |
*/ |
| 86 |
function avcp_generate_area_sectors($post_id) { |
| 87 |
$html = ''; |
| 88 |
$terms = get_the_terms($post_id, 'areesettori'); |
| 89 |
|
| 90 |
if ($terms && !is_wp_error($terms)) { |
| 91 |
foreach($terms as $term) { |
| 92 |
if (!is_object($term) || empty($term->name)) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
$get_term = get_term_by('name', $term->name, 'areesettori'); |
| 97 |
if (!$get_term || is_wp_error($get_term)) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$tsr_id = absint($get_term->term_id); |
| 102 |
|
| 103 |
// Check if get_tax_meta function exists before using it |
| 104 |
if (function_exists('get_tax_meta')) { |
| 105 |
$id_sec_red_var = get_tax_meta($tsr_id, 'aree_settori_cc_url'); |
| 106 |
|
| 107 |
if ($id_sec_red_var) { |
| 108 |
$permalink = get_permalink(absint($id_sec_red_var)); |
| 109 |
if ($permalink) { |
| 110 |
$html .= ' - <a href="' . esc_url($permalink) . '">' . esc_html($term->name) . '</a>'; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$id_sec_cc_var = get_tax_meta($tsr_id, 'aree_settori_cc_responsabile'); |
| 115 |
if (!empty($id_sec_cc_var)) { |
| 116 |
$html .= ' - [<acronym title="' . esc_attr__('Responsabile del Centro di Costo', 'avcp') . '">resp. <b>' . esc_html($id_sec_cc_var) . '</b></acronym>]'; |
| 117 |
} |
| 118 |
} else { |
| 119 |
// Fallback if get_tax_meta doesn't exist |
| 120 |
$html .= ' - ' . esc_html($term->name); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $html; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Generates contract dates section |
| 130 |
* |
| 131 |
* @param int $post_id The post ID |
| 132 |
* @return string HTML content for dates |
| 133 |
*/ |
| 134 |
function avcp_generate_contract_dates($post_id) { |
| 135 |
$d_inizio = sanitize_text_field(get_post_meta($post_id, 'avcp_data_inizio', true)); |
| 136 |
$d_fine = sanitize_text_field(get_post_meta($post_id, 'avcp_data_fine', true)); |
| 137 |
|
| 138 |
$avcp_data_inizio = $d_inizio ? date_i18n("d/m/Y", strtotime($d_inizio)) : esc_html__('n.d.', 'avcp'); |
| 139 |
$avcp_data_fine = $d_fine ? date_i18n("d/m/Y", strtotime($d_fine)) : esc_html__('n.d.', 'avcp'); |
| 140 |
|
| 141 |
$html = '<tr><td>' . esc_html__('Data di effettivo inizio:', 'avcp') . '</td><td>' . esc_html($avcp_data_inizio) . '</td></tr>'; |
| 142 |
$html .= '<tr><td>' . esc_html__('Data di ultimazione:', 'avcp') . '</td><td>' . esc_html($avcp_data_fine) . '</td></tr>'; |
| 143 |
|
| 144 |
return $html; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Generates liquidated amounts section |
| 149 |
* |
| 150 |
* @param int $post_id The post ID |
| 151 |
* @return string HTML content for liquidated amounts |
| 152 |
*/ |
| 153 |
function avcp_generate_liquidated_amounts($post_id) { |
| 154 |
$html = '<tr><td>' . esc_html__('Importo delle somme liquidate:', 'avcp') . '</td><td>'; |
| 155 |
|
| 156 |
$current_year = date('Y'); |
| 157 |
for ($i = 2013; $i <= $current_year + 10; $i++) { |
| 158 |
$amount = get_post_meta($post_id, 'avcp_s_l_' . absint($i), true); |
| 159 |
if ($amount && floatval($amount) > 0) { |
| 160 |
$html .= '<strong>' . absint($i) . '</strong>: ' . esc_html($amount) . '<br>'; |
| 161 |
} |
| 162 |
} |
| 163 |
$html .= '</td></tr>'; |
| 164 |
|
| 165 |
return $html; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Generates reference year section |
| 170 |
* |
| 171 |
* @param int $post_id The post ID |
| 172 |
* @param array $options Cached options array |
| 173 |
* @return string HTML content for reference year |
| 174 |
*/ |
| 175 |
function avcp_generate_reference_year($post_id, $options) { |
| 176 |
global $post; |
| 177 |
|
| 178 |
$html = '<tr><td>' . esc_html__('Anno di riferimento:', 'avcp') . '</td><td>'; |
| 179 |
|
| 180 |
if ($options['dis_archivioanni'] == '1') { |
| 181 |
$term_list = get_the_term_list($post->ID, 'annirif', '', ' - ', ''); |
| 182 |
$html .= wp_strip_all_tags($term_list); |
| 183 |
} else { |
| 184 |
$html .= get_the_term_list($post->ID, 'annirif', '', ' - ', ''); |
| 185 |
} |
| 186 |
|
| 187 |
$html .= '</td></tr>'; |
| 188 |
|
| 189 |
return $html; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Generates the participating operators table |
| 194 |
* |
| 195 |
* @param int $post_id The post ID |
| 196 |
* @param array $options Cached options array |
| 197 |
* @return string HTML table content |
| 198 |
*/ |
| 199 |
function avcp_generate_participating_operators($post_id, $options) { |
| 200 |
global $post; |
| 201 |
|
| 202 |
$html = '<h3>' . esc_html__('Elenco degli operatori partecipanti', 'avcp') . '</h3>'; |
| 203 |
$html .= '<table>'; |
| 204 |
|
| 205 |
$terms = get_the_terms($post->ID, 'ditte'); |
| 206 |
if ($terms && !is_wp_error($terms)) { |
| 207 |
foreach($terms as $term) { |
| 208 |
if (!is_object($term) || empty($term->name)) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
$get_term = get_term_by('name', $term->name, 'ditte'); |
| 213 |
if (!$get_term || is_wp_error($get_term)) { |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
$t_id = absint($get_term->term_id); |
| 218 |
$term_meta = get_option("taxonomy_$t_id"); |
| 219 |
$term_return = ''; |
| 220 |
|
| 221 |
if (is_array($term_meta) && isset($term_meta['avcp_codice_fiscale'])) { |
| 222 |
$term_return = esc_attr($term_meta['avcp_codice_fiscale']); |
| 223 |
} |
| 224 |
|
| 225 |
$is_estera = '<acronym title="' . esc_attr__('Identificativo Fiscale Italiano', 'avcp') . '">IT</acronym>'; |
| 226 |
|
| 227 |
// Check if get_tax_meta function exists before using it |
| 228 |
if (function_exists('get_tax_meta')) { |
| 229 |
$stato_var = get_tax_meta($t_id, 'avcp_is_ditta_estera'); |
| 230 |
$is_estera = empty($stato_var) ? |
| 231 |
'<acronym title="' . esc_attr__('Identificativo Fiscale Italiano', 'avcp') . '">IT</acronym>' : |
| 232 |
'<acronym title="' . esc_attr__('Identificativo Fiscale Estero', 'avcp') . '">EE</acronym>'; |
| 233 |
} |
| 234 |
|
| 235 |
$html .= '<tr><td>'; |
| 236 |
|
| 237 |
if ($options['dis_archivioditte'] == '1') { |
| 238 |
$html .= esc_html($term->name); |
| 239 |
} else { |
| 240 |
$term_link = get_term_link($t_id, 'ditte'); |
| 241 |
if (!is_wp_error($term_link)) { |
| 242 |
$html .= '<a href="' . esc_url($term_link) . '" title="' . esc_attr($term->name) . '">' . esc_html($term->name) . '</a>'; |
| 243 |
} else { |
| 244 |
$html .= esc_html($term->name); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
$html .= '</td><td>' . esc_html($term_return) . ' - <b>' . $is_estera . '</b></td></tr>'; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
$html .= '</table>'; |
| 253 |
|
| 254 |
return $html; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Generates the winning operators table |
| 259 |
* |
| 260 |
* @param int $post_id The post ID |
| 261 |
* @param array $options Cached options array |
| 262 |
* @return string HTML table content |
| 263 |
*/ |
| 264 |
function avcp_generate_winning_operators($post_id, $options) { |
| 265 |
global $post; |
| 266 |
|
| 267 |
$html = '<h3>' . esc_html__('Elenco degli operatori aggiudicatari', 'avcp') . '</h3>'; |
| 268 |
$html .= '<table>'; |
| 269 |
|
| 270 |
$dittepartecipanti = get_the_terms($post->ID, 'ditte'); |
| 271 |
$cats = get_post_meta($post->ID, 'avcp_aggiudicatari', true); |
| 272 |
|
| 273 |
if (is_array($dittepartecipanti) && !is_wp_error($dittepartecipanti)) { |
| 274 |
$has_winners = false; |
| 275 |
|
| 276 |
foreach ($dittepartecipanti as $term) { |
| 277 |
if (!is_object($term) || empty($term->name)) { |
| 278 |
continue; |
| 279 |
} |
| 280 |
|
| 281 |
$cterm = get_term_by('name', $term->name, 'ditte'); |
| 282 |
if (!$cterm || is_wp_error($cterm)) { |
| 283 |
continue; |
| 284 |
} |
| 285 |
|
| 286 |
$cat_id = absint($cterm->term_id); |
| 287 |
$term_meta = get_option("taxonomy_$cat_id"); |
| 288 |
$term_return = ''; |
| 289 |
|
| 290 |
if (is_array($term_meta) && isset($term_meta['avcp_codice_fiscale'])) { |
| 291 |
$term_return = esc_attr($term_meta['avcp_codice_fiscale']); |
| 292 |
} |
| 293 |
|
| 294 |
$checked = (in_array($cat_id, (array)$cats) ? ' checked="checked"' : ""); |
| 295 |
|
| 296 |
$is_estera = '<acronym title="' . esc_attr__('Identificativo Fiscale Italiano', 'avcp') . '">IT</acronym>'; |
| 297 |
|
| 298 |
// Check if get_tax_meta function exists before using it |
| 299 |
if (function_exists('get_tax_meta')) { |
| 300 |
$stato_var = get_tax_meta($cat_id, 'avcp_is_ditta_estera'); |
| 301 |
$is_estera = empty($stato_var) ? |
| 302 |
'<acronym title="' . esc_attr__('Identificativo Fiscale Italiano', 'avcp') . '">IT</acronym>' : |
| 303 |
'<acronym title="' . esc_attr__('Identificativo Fiscale Estero', 'avcp') . '">EE</acronym>'; |
| 304 |
} |
| 305 |
|
| 306 |
if ($checked) { |
| 307 |
$has_winners = true; |
| 308 |
$html .= '<tr><td>'; |
| 309 |
|
| 310 |
if ($options['dis_archivioditte'] != '1') { |
| 311 |
$term_link = get_term_link($cterm->term_id, 'ditte'); |
| 312 |
if (!is_wp_error($term_link)) { |
| 313 |
$html .= '<a href="' . esc_url($term_link) . '" title="' . esc_attr($term->name) . '">'; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
$html .= esc_html($term->name); |
| 318 |
|
| 319 |
if ($options['dis_archivioditte'] != '1' && !is_wp_error($term_link)) { |
| 320 |
$html .= '</a>'; |
| 321 |
} |
| 322 |
|
| 323 |
$html .= '</td><td>' . esc_html($term_return) . ' - <b>' . $is_estera . '</b></td></tr>'; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (!$has_winners) { |
| 328 |
$html .= '<tr><td>' . esc_html__('Nessun aggiudicatario...', 'avcp') . '</td></tr>'; |
| 329 |
} |
| 330 |
} else { |
| 331 |
$html .= '<tr><td>' . esc_html__('Nessun aggiudicatario...', 'avcp') . '</td></tr>'; |
| 332 |
} |
| 333 |
|
| 334 |
$html .= '</table>'; |
| 335 |
|
| 336 |
return $html; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Generates the WPGov footer if enabled |
| 341 |
* |
| 342 |
* @param array $options Cached options array |
| 343 |
* @return string HTML content for footer |
| 344 |
*/ |
| 345 |
function avcp_generate_footer($options) { |
| 346 |
$html = ''; |
| 347 |
|
| 348 |
if ($options['wpgov_show_love']) { |
| 349 |
$html .= '<center><a href="http://www.wpgov.it" target="_blank" title="' . esc_attr__('Software © WPGov', 'avcp') . '"><img style="margin:5px;" src="' . esc_url(plugin_dir_url(__FILE__) . 'images/wpgov.png') . '" alt="' . esc_attr__('WPGov Logo', 'avcp') . '" /></a></center>'; |
| 350 |
} |
| 351 |
|
| 352 |
return $html; |
| 353 |
} |
| 354 |
|
| 355 |
add_filter('the_content', function($content) { |
| 356 |
global $post; |
| 357 |
|
| 358 |
// Validate post object and type |
| 359 |
if (!is_object($post) || empty($post->post_type) || $post->post_type !== 'avcp') { |
| 360 |
return $content; |
| 361 |
} |
| 362 |
|
| 363 |
// Validate post ID |
| 364 |
$post_id = absint($post->ID); |
| 365 |
if (!$post_id || !get_post($post_id)) { |
| 366 |
return $content; |
| 367 |
} |
| 368 |
|
| 369 |
// Cache options to avoid multiple queries |
| 370 |
$options = array( |
| 371 |
'denominazione_ente' => get_option('avcp_denominazione_ente', ''), |
| 372 |
'codicefiscale_ente' => get_option('avcp_codicefiscale_ente', ''), |
| 373 |
'dis_archivioanni' => get_option('avcp_dis_archivioanni'), |
| 374 |
'dis_archivioditte' => get_option('avcp_dis_archivioditte'), |
| 375 |
'wpgov_show_love' => get_option('wpgov_show_love') |
| 376 |
); |
| 377 |
|
| 378 |
// Build the content |
| 379 |
$prev = '<br/>'; |
| 380 |
$prev .= avcp_generate_contract_info_table($post_id, $options); |
| 381 |
$prev .= avcp_generate_participating_operators($post_id, $options); |
| 382 |
$prev .= avcp_generate_winning_operators($post_id, $options); |
| 383 |
$prev .= avcp_generate_footer($options); |
| 384 |
|
| 385 |
return $prev . $content; |
| 386 |
} |
| 387 |
); |
| 388 |
?> |
| 389 |
|