-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
1407 lines (1141 loc) · 39.4 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
define( 'WP_SOFTCATALA_VERSION', '1.2.25' );
include ('php73.php');
if( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
} else if( file_exists( ABSPATH . '/../vendor/autoload.php' ) ) {
require ABSPATH . '/../vendor/autoload.php';
} else {
if ( is_admin() ) {
add_action( 'admin_notices', function () {
echo '<div class="error">' .
'<p>Composer autoload is not working. Theme wp-softcatala depends on composer autoloading.</p>' .
'</div>';
}
);
return;
} else if ( ! is_admin() ) {
header( 'HTTP/1.1 500 Internal Server Error' );
echo 'Aquest és un error 500. Alguna cosa no funciona bé al servidor.';
die();
}
}
$timber = new \Timber\Timber();
include( 'inc/perfils.php' );
Timber::$dirname = array( 'templates', 'views' );
class StarterSite extends TimberSite {
function __construct() {
if ( ! defined( 'WP_TESTS_DOMAIN' ) ) {
add_theme_support( 'post-thumbnails' );
add_theme_support( 'menus' );
add_theme_support( 'title-tag' );
}
add_filter( 'timber_context', array( $this, 'add_user_nav_info_to_context' ) );
add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
add_filter( 'xv_planeta_feed', '__return_true' );
add_filter( 'xv_podcasts_log_file', function( $v ) {
return ABSPATH . '../podcast.log';
} );
add_filter( 'xv_podcasts_log_fields', function( $f ) {
return array_merge( $f, [
'ip' => $_SERVER['HTTP_X_REAL_IP'],
'accept' => $_SERVER['HTTP_ACCEPT'],
'encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'],
'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'],
'language' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
'referer' => $_SERVER['HTTP_REFERER'],
'ua' => $_SERVER['HTTP_USER_AGENT']
]);
} );
add_filter( 'wpseo_twitter_creator_account', function ( $twitter ) {
return '@softcatala';
} );
add_filter( 'wpseo_opengraph_author_facebook', function ( $twitter ) {
return 'https://facebook.com/Softcatala';
} );
add_action( 'init', array( $this, 'sc_rewrite_search' ) );
add_action( 'init', array( $this, 'register_post_types' ) );
add_action( 'template_redirect', array( $this, 'sc_change_programs_search_url_rewrite' ) );
add_action( 'init', array( $this, 'sc_author_rewrite_base' ) );
add_action( 'template_redirect', array( $this, 'fix_woosidebar_hooks' ), 1 );
add_action( 'template_redirect', array( $this, 'sc_change_search_url_rewrite' ) );
add_action( 'after_setup_theme', array( $this, 'include_theme_conf' ) );
//SC Dashboard settings
add_action( 'admin_menu', array( $this, 'include_sc_settings' ) );
add_action( 'admin_init', array( $this, 'add_caps' ) );
spl_autoload_register( array( $this, 'autoload' ) );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
spl_autoload_register( array( $this, 'autoload_wpcli' ) );
require __DIR__ . '/wp-cli/loader.php';
}
add_post_type_support( 'programa', 'woosidebars' );
$this->init_services();
parent::__construct();
}
public function init_services() {
SC_Multilingue::init();
SC_NavegaEnCatala::init();
\Softcatala\Content\JsonToTable::init();
SC_Sitemaps::init();
}
function autoload_wpcli( $cls ) {
$path = __DIR__ . '/wp-cli/' . strtolower( $cls ) . '.php';
is_readable( $path ) && require_once( $path );
}
function autoload( $cls ) {
$this->tryLoadFromNamespace( $cls ) || $this->tryLoadFromClasses( $cls );
}
function tryLoadFromClasses( $cls ) {
if ( 0 !== strpos( $cls, 'SC_' ) ) {
return;
}
$name = str_replace( 'SC_', '', $cls );
$name = str_replace( '_', '-', $name );
$path = __DIR__ . '/classes/' . strtolower( $name ) . '.php';
if ( is_readable( $path ) && require_once( $path ) ) {
return;
}
}
function tryLoadFromNamespace( $cls ) {
if ( 0 !== strpos( $cls, 'Softcatala' ) && 0 !== strpos( $cls, '\Softcatala' ) ) {
return;
}
$path = __DIR__ . DIRECTORY_SEPARATOR . str_replace( '\\', DIRECTORY_SEPARATOR, $cls ) . '.php';
$path = $this->decamelize( str_replace( 'Softcatala' . DIRECTORY_SEPARATOR, 'classes' . DIRECTORY_SEPARATOR, $path ) );
return is_readable( $path ) && require_once( $path );
}
function decamelize( $string ) {
return strtolower(
str_replace(
DIRECTORY_SEPARATOR . '-', DIRECTORY_SEPARATOR,
preg_replace( [ '/([a-z\d])([A-Z])/', '/([^-])([A-Z][a-z])/' ], '$1-$2', $string )
)
);
}
function include_theme_conf() {
locate_template( array( 'inc/widgets.php' ), true, true );
locate_template( array( 'inc/post_types_functions.php' ), true, true );
locate_template( array( 'inc/ajax_operations.php' ), true, true );
locate_template( array( 'inc/rewrites.php' ), true, true );
load_theme_textdomain('softcatala', get_template_directory() . '/languages');
}
function register_ui_settings() {
wp_localize_script( 'sc-js-main', 'sc_settings', SC_Settings::get_instance()->get_setting_values() );
}
/**
* This function implements the rewrite tags for the different sections of the website
*/
function sc_change_programs_search_url_rewrite() {
$post_type = get_query_var( 'post_type' );
$params_query = '';
if ( $post_type == 'programa' ) {
if ( isset( $_GET['cerca'] ) || isset( $_GET['sistema_operatiu'] ) || isset( $_GET['categoria_programa'] ) ) {
$available_query_vars = array(
'cerca' => 'p',
'sistema_operatiu' => 'so',
'categoria_programa' => 'cat'
);
foreach ( $available_query_vars as $query_var => $key ) {
if ( get_query_var( $query_var ) ) {
$params_query .= $key . '/' . urlencode( get_query_var( $query_var ) ) . '/';
}
}
if ( ! empty( $params_query ) ) {
wp_redirect( home_url( "/programes/" ) . $params_query );
}
}
} elseif ( empty( $post_type ) ) {
if ( isset( $_GET['cerca'] ) && isset( $_GET['form_cerca_noticies'] ) ) {
$available_query_vars = array( 'cerca' => 'cerca' );
foreach ( $available_query_vars as $query_var => $key ) {
$params_query .= $key . '/' . urlencode( get_query_var( $query_var ) ) . '/';
}
if ( ! empty( $params_query ) ) {
wp_redirect( home_url( "/noticies/" ) . $params_query );
}
}
}
}
/**
*
* esta funció s'encarrega de que si arriba alguna URL tipus /?s=XXX la converteix
*/
function sc_change_search_url_rewrite() {
if ( is_search() ) {
if ( ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/cerca/" ) . urlencode( get_query_var( 's' ) ) . '/' );
exit();
} else {
$real = get_search_query();
$converted = $this->convert_smart_quotes( $real );
$real = html_entity_decode( $real, ENT_QUOTES, "UTF-8" );
if ( $converted != $real ) {
wp_redirect( home_url( "/cerca/" ) . urlencode( $converted ) . '/' );
exit();
}
}
}
}
function convert_smart_quotes( $str ) {
$chr_map = array(
// Windows codepage 1252
"\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark
"\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark
"\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark
"\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark
"\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark
"\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark
"\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark
"\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark
// Regular Unicode // U+0022 quotation mark (")
// U+0027 apostrophe (')
"\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark
"\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark
"\xE2\x80\x98" => "'", // U+2018 left single quotation mark
"\xE2\x80\x99" => "'", // U+2019 right single quotation mark
"\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark
"\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark
"\xE2\x80\x9C" => '"', // U+201C left double quotation mark
"\xE2\x80\x9D" => '"', // U+201D right double quotation mark
"\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark
"\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark
"\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark
"\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark
);
$chr = array_keys( $chr_map ); // but: for efficiency you should
$rpl = array_values( $chr_map ); // pre-calculate these two arrays
return str_replace( $chr, $rpl, html_entity_decode( $str, ENT_QUOTES, "UTF-8" ) );
}
/**
* Change "search" by "cerca"
*/
function sc_rewrite_search() {
global $wp_rewrite;
$wp_rewrite->search_base = 'cerca';
$wp_rewrite->pagination_base = 'pagina';
}
function sc_author_rewrite_base() {
global $wp_rewrite;
$author_slug = 'membres';
$wp_rewrite->author_base = $author_slug;
$wp_rewrite->author_structure = '/membres/%author%';
}
/**
* Custom Softcatalà settings
*/
function include_sc_settings() {
register_setting( 'softcatala-group', 'llistes_access' );
register_setting( 'softcatala-group', 'api_diccionari_multilingue' );
register_setting( 'softcatala-group', 'api_diccionari_sinonims' );
register_setting( 'softcatala-group', 'api_conjugador' );
register_setting( 'softcatala-group', 'api_memory_base' );
register_setting( 'softcatala-group', 'api_diccionari_engcat' );
register_setting( 'softcatala-group', 'api_cerca_corpus' );
register_setting( 'softcatala-group', 'catalanitzador_post_id' );
register_setting( 'softcatala-group', 'aparells_post_id' );
register_setting( 'softcatala-group', 'sc_text_programes' );
register_setting( 'softcatala-group', 'api_languagetool' );
add_option('api_languagetool', 'https://api.softcatala.org/corrector/v2/check');
$ui_settings = SC_Settings::get_instance()->get_setting_names();
foreach ( $ui_settings as $setting ) {
register_setting( 'softcatala-group', $setting );
}
//Email contact parameters
$sections = $this->get_email_sections();
foreach ( $sections as $key => $section ) {
register_setting( 'softcatala-group', 'email_' . $key );
}
if ( function_exists( 'add_submenu_page' ) ) {
add_submenu_page( 'options-general.php', 'Softcatalà Settings', 'Softcatalà Settings', 'manage_options', __FILE__, array(
$this,
'softcatala_dash_page'
) );
}
}
function add_caps() {
$roles = array();
$roles[] = get_role( 'contributor' );
$roles[] = get_role( 'author' );
foreach ( $roles as $role ) {
$role->add_cap( 'edit_pages' );
$role->add_cap( 'edit_published_pages' );
$role->add_cap( 'upload_files' );
}
}
function get_email_sections() {
$sections = array(
'general' => 'General',
'traductor_neuronal' => 'Traductor Neuronal',
'traductor' => 'Traductor',
'corrector' => 'Corrector',
'recursos' => 'Recursos',
'rebost' => 'Programes',
'sinonims' => 'Sinonims'
);
return $sections;
}
/**
* Renders the Softcatalà dashboard settings page
*/
function softcatala_dash_page() {
wp_enqueue_script( 'sc-js-dash', get_template_directory_uri() . '/static/js/sc-admin.js', array( 'jquery' ), WP_SOFTCATALA_VERSION, true );
$admin_template = dirname( __FILE__ ) . '/templates/admin/sc-dash.twig';
$sections = $this->get_email_sections();
$settings = SC_Settings::get_instance();
$section_html_content = Timber::fetch( $admin_template, array(
'sections' => $sections,
'settings' => $settings
) );
echo $section_html_content;
}
function register_post_types() {
\Softcatala\TypeRegisters\Slider::get_instance();
\Softcatala\TypeRegisters\Esdeveniment::get_instance();
\Softcatala\TypeRegisters\Aparell::get_instance();
\Softcatala\TypeRegisters\Programa::get_instance();
\Softcatala\TypeRegisters\Projecte::get_instance();
\Softcatala\TypeRegisters\DadesObertes::get_instance();
}
function add_user_nav_info_to_context( $context ) {
$context['user_info'] = $this->get_user_information();
$context['search_params'] = $this->get_search_params();
$context['site'] = $this;
$context['themepath'] = get_template_directory_uri();
$context['current_url'] = get_current_url();
return $context;
}
function add_to_twig( $twig ) {
/* this is where you can add your own fuctions to twig */
$twig->addExtension( new Twig_Extension_StringLoader() );
$twig->addFilter( new Twig_Filter( 'get_caption_from_media_url', 'get_caption_from_media_url' ) );
$twig->addFilter( new Twig_Filter( 'get_img_from_id', 'get_img_from_id' ) );
$twig->addFilter( new Twig_Filter( 'get_full_img_from_id', 'get_full_img_from_id' ) );
$twig->addFilter( new Twig_Filter( 'truncate_words', 'sc_truncate_words' ) );
$twig->addFilter( new Twig_Filter( 'print_definition', 'print_definition' ) );
$twig->addFilter( new Twig_Filter( 'clean_number', 'clean_number' ) );
$twig->addFilter( new Twig_filter( 'home_thumb', 'home_thumb' ) );
/* Diccionari eng cat functions */
$twig->addFilter( new Twig_filter( 'fullGrammarTag', 'fullGrammarTag' ) );
$twig->addFilter( new Twig_filter( 'prepareLemmaHeading', 'prepareLemmaHeading' ) );
$twig->addFilter( new Twig_filter( 'prepareSubLemma', 'prepareSubLemma' ) );
$twig->addFilter( new Twig_filter( 'prepareWord', 'prepareWord' ) );
$twig->addFilter( new Twig_filter( 'presentFeminine', 'presentFeminine' ) );
return $twig;
}
function get_search_params() {
$search_params = array();
$search_params['current_url'] = get_current_url();
$search_params['current_url_filtre'] = remove_querystring_var( $search_params['current_url'], 'filtre' );
$search_params['current_url_filtre_addition'] = get_filter_addition( $search_params['current_url_filtre'] );
$search_params['current_url_nocat'] = get_current_url( 'filtre' );
$search_params['current_url_params'] = get_current_querystring();
$search_params['current_url_noparams'] = str_replace( $search_params['current_url_params'], '', $search_params['current_url'] );
return $search_params;
}
function get_user_information() {
$user_info = array();
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$user_info['current_url'] = get_current_url();
if ( $user_id ) {
$user_info['is_connected'] = true;
$user_info['wp_logout_url'] = wp_logout_url( '/' );
$user_info['avatar'] = get_avatar( $user_id, 19, null, 'fotografia-usuari-sofcatala' );
$user_info['avatar_48'] = get_avatar( $user_id, 48, null, 'fotografia-usuari-sofcatala' );
$user_info['name'] = $current_user->display_name;
$user_info['profile_url'] = get_edit_profile_url( $user_id );
} else {
$user_info['avatar'] = get_avatar( $user_id, 19, null, 'fotografia-usuari-sofcatala' );
$user_info['is_connected'] = false;
$user_info['wp_login_url'] = wp_login_url( get_current_url() );
}
return $user_info;
}
public function fix_woosidebar_hooks() {
global $wp_filter;
if ( ! isset ( $wp_filter['get_header'] ) ) {
return;
}
$priorities = $wp_filter['get_header'];
foreach ( $priorities as $p => $filters ) {
foreach ( $filters as $f => $v ) {
$to_add = $v['function'];
if ( is_array( $to_add ) && count( $to_add ) == 2 ) {
$class = get_class( $to_add[0] );
if ( strpos( $class, 'Woo_' ) >= 0 ) {
remove_action( 'get_header', $to_add );
add_action( 'template_redirect', $to_add, 10 + $p );
}
}
}
}
}
}
global $sc_site;
$sc_site = new StarterSite();
function softcatala_scripts() {
global $sc_site;
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.js' ), false, null, true );
wp_register_script( 'sc-js-metacookie', get_template_directory_uri() . '/static/js/jquery.metacookie.js', array( 'jquery' ), '20210928', true );
wp_enqueue_script( 'jquery' );
wp_enqueue_style( 'sc-css-main', get_template_directory_uri() . '/static/css/main.min.css', array(), WP_SOFTCATALA_VERSION );
wp_enqueue_script( 'sc-js-main', get_template_directory_uri() . '/static/js/main.min.js', array( 'jquery' ), WP_SOFTCATALA_VERSION, true );
$sc_site->register_ui_settings();
wp_enqueue_script( 'sc-jquery-cookie', '/../ssi/js/cookies/jquery.cookie.js', array( 'jquery' ), WP_SOFTCATALA_VERSION, true );
//wp_enqueue_script( 'sc-js-ads', get_template_directory_uri() . '/static/js/ads.js', array(), WP_SOFTCATALA_VERSION, true );
wp_enqueue_script( 'sc-js-comments', get_template_directory_uri() . '/static/js/comments.js', array( 'sc-js-main' ), WP_SOFTCATALA_VERSION, true );
}
add_action( 'wp_enqueue_scripts', 'softcatala_scripts' );
/**
* This function retrieves the media caption from
* a given url. It is used because the «secondary image»
* created from Types doesn't return the media caption
* Author: https://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/
*
* @param string $url
*
* @return string $caption
*/
function get_caption_from_media_url( $attachment_url = '', $return_id = false ) {
global $wpdb;
$attachment_id = false;
// If there is no url, return.
if ( '' == $attachment_url ) {
return;
}
// Get the upload directory paths and clean the attachment url
$upload_dir_paths = wp_upload_dir();
$attachment_url = str_replace( 'wp/../', '', $attachment_url );
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {
// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );
// Remove the upload path base directory from the attachment URL
$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );
// Finally, run a custom database query to get the attachment ID from the modified attachment URL
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );
}
//Not in the original function from the author
$attachment_meta = get_post_field( 'post_excerpt', $attachment_id );
if ( $return_id ) {
return $attachment_id;
}
return $attachment_meta;
}
/**
* Twig function to truncate text
*
* @param string
*
* @return string
*/
function sc_truncate_words( $string, $size ) {
$splitstring = wp_trim_words( str_replace( '_', ' ', $string ), $size );
return $splitstring;
}
/**
* Removes useless decimal 0
*
* @param string $n number to clean.
*
* @return string
*/
function clean_number( $n ) {
return str_replace( ',00', '', $n );
}
/**
* Creates home thumbnail style
*
* @param string $img img for background.
*
* @return string
*/
function home_thumb( $img ) {
// $img: 370x150
$style = <<<STYLE
background: url('$img') no-repeat center left #eae8e8; height: 150px; margin-bottom: 70px;
STYLE;
return $style;
}
/**
* Twig function specific for Diccionari multilingüe
*
* @param string
*
* @return string
*/
function print_definition( $def ) {
$def = trim( $def );
$pos = strpos( $def, '#' );
if ( $pos === false ) {
$result = ' - ' . $def;
} else {
$def = str_replace( '#', '', $def );
$entries = explode( "\n", $def );
$filtered = array_filter( array_map( 'trim_entries', $entries ) );
$result = ' - ' . implode( '<br />- ', $filtered ) . '<br />';
}
return $result;
}
function trim_entries( $entry ) {
$trimmed = trim( $entry );
return empty( $trimmed ) ? null : $trimmed;
}
function get_full_img_from_id( $img_id ) {
$image = wp_get_attachment_image_src( $img_id, 'full' );
return $image[0];
}
function get_img_from_id( $img_id ) {
$image = wp_get_attachment_image_src( $img_id );
return $image[0];
}
function get_img_id_from_url($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
/**
* Twig function specific for Diccionari Eng Cat
*
* @param string
*
* @return string
*/
function fullGrammarTag($word) {
$grammarTag = $word->grammarClass;
if (!empty($word->feminine) && $grammarTag == "m") {
$grammarTag = "mf";
}
if (!empty($word->grammarAux)) {
$grammarTag .= ' ' . $word->grammarAux;
}
return $grammarTag;
}
function prepareLemmaHeading($word) {
$output = '';
$output .= '<h2 class="originalword">';
$output .= $word->text;
if (!empty($word->feminine)) {
$output .= ' <span class="engcat-gray">' . $word->feminine . '</span> ';
}
$fullGTag = fullGrammarTag($word);
$output .= ' <span class="engcat-italics">' . $fullGTag . '</span> ';
$output .= '</h2>';
if (!empty($word->tags)) {
$output .= '[' . $word->tags . '] ';
}
if (!empty($word->def)) {
$output .= '[' . $word->def . '] ';
}
if (!empty($word->remark)) {
$output .= ' [⇒ ' . $word->remark . '] ';
}
return trim($output);
}
function prepareSubLemma($word) {
$output = '';
#print_r($word);
if (!empty($word->before) || !empty($word->after)) {
$output .= '<b>';
if (!empty($word->before)) {
$output .= '(' . $word->before . ') ';
}
$output .= $word->text;
if (!empty($word->after)) {
$output .= ' (' . $word->after . ')';
}
$output .= '</b> ';
}
if (!empty($word->area)) {
$output .= '<span class="engcat-smallcaps">' . $word->area . '</span> ';
}
if (!empty($word->plural)) {
$output .= ' [pl. ' . $word->plural . '] ';
}
if (!empty($word->def)) {
$output .= '[' . $word->def . '] ';
}
if (!empty($word->remark)) {
$output .= ' [⇒ ' . $word->remark . '] ';
}
return trim($output);
}
function prepareWord($word, $prevFullGTag) {
$output = '';
if (!empty($word->area)) {
$output .= '<span class="engcat-smallcaps">' . $word->area . '</span> ';
}
if (!empty($word->tags)) {
$output .= '[' . $word->tags . '] ';
}
if (!empty($word->def)) {
$output .= '[' . $word->def . '] ';
}
if (!empty($word->before)) {
$output .= '(' . $word->before . ') ';
}
$output .= $word->text;
if (!empty($word->after)) {
$output .= ' (' . $word->after . ')';
}
if (!empty($word->feminine)) {
$output .= ' <span class="engcat-gray">' . $word->feminine . '</span>';
}
if (!empty($word->plural)) {
$output .= ' [pl. ' . $word->plural . '] ';
}
$fullGTag = fullGrammarTag($word);
if ($fullGTag != $prevFullGTag && $fullGTag != "n") {
$output .= ' <span class="engcat-italics">' . $fullGTag . '</span>';
}
if (!empty($word->remark)) {
$output .= ' [⇒ ' . $word->remark . '] ';
}
return trim($output);
}
function presentFeminine($word) {
if (!empty($word->feminineForm)) {
return ' <span class="engcat-gray">' . $word->feminineForm . '</span>';
} else {
return '';
}
}
/**
* This function retrieves the current url, either on http or https format
* depending on the current navigation
*
* @return string $url
*/
function get_current_url( $remove = false ) {
$current_url = ( isset( $_SERVER['HTTPS'] ) ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if ( $remove ) {
$current_url = remove_query_arg( $remove, $current_url );
}
return $current_url;
}
abstract class SearchQueryType {
const All = 0;
const FilteredDate = 1;
const Search = 2;
const Aparell = 4;
const Post = 6;
const PagePrograma = 7;
const FilteredTema = 8;
const PageProjecte = 9;
}
/*
* Returns the arguments to apply to the mysql query
*/
function get_post_query_args( $post_type, $queryType, $filter = array() ) {
//Retrieve posts
switch ( $post_type ) {
case 'aparell':
$base_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => - 1
);
break;
case 'programa':
$base_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'paged' => get_is_paged(),
'posts_per_page' => 18,
'tax_query' => array(
array(
'taxonomy' => 'classificacio',
'field' => 'slug',
'terms' => 'arxivat',
'operator' => 'NOT IN'
)
)
);
break;
case 'projecte':
$base_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'paged' => get_is_paged(),
'posts_per_page' => 36,
'tax_query' => array(
array(
'taxonomy' => 'classificacio',
'field' => 'slug',
'terms' => 'arxivat',
'operator' => 'NOT IN'
)
)
);
break;
case 'page':
$base_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'order' => 'ASC',
'meta_query' => array(
get_meta_query_value( $filter['subpage_type'], $filter['post_id'], '=', 'NUMERIC' )
)
);
break;
case 'post':
$base_args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'order' => 'DESC',
'paged' => get_is_paged(),
'posts_per_page' => 10
);
break;
}
$filter_args = array();
if ( $queryType == SearchQueryType::Post ) {
if ( ! empty ( $filter['s'] ) ) {
$filter_args['s'] = $filter['s'];
}
if ( ! empty ( $filter['categoria'] ) ) {
$filter_args['category__and'] = $filter['categoria'];
}
} else if ( $queryType == SearchQueryType::Search ) {
$filter_args = array(
's' => $filter,
'meta_query' => array(
get_meta_query_value( 'data_fi', time(), '>=', 'NUMERIC' )
)
);
} else if ( $queryType == SearchQueryType::FilteredDate ) {
$filter_args = array(
'meta_query' => array(
'relation' => 'AND',
array(
get_meta_query_value( 'data_fi', $filter['start_time'], '>=', 'NUMERIC' )
),
array(
get_meta_query_value( 'data_inici', $filter['final_time'], '<=', 'NUMERIC' )
)
)
);
} else if ( $queryType == SearchQueryType::Aparell ) {
$filter_args = array();
if ( ! empty ( $filter['s'] ) ) {
$filter_args['s'] = $filter['s'];
}
if ( ! empty ( $filter['so_aparell'] ) ) {
$filter_args['tax_query'][] = array(
'taxonomy' => 'so_aparell',
'field' => 'slug',
'terms' => $filter['so_aparell']
);
$filter_args['filter_so'] = $filter['so_aparell'];
}
if ( ! empty ( $filter['tipus_aparell'] ) ) {
$filter_args['tax_query'][] = array(
'taxonomy' => 'tipus_aparell',
'field' => 'slug',
'terms' => $filter['tipus_aparell']
);
$filter_args['filter_tipus'] = $filter['tipus_aparell'];
}
if ( ! empty ( $filter['fabricant'] ) ) {
$filter_args['tax_query'][] = array(
'taxonomy' => 'fabricant',
'field' => 'slug',
'terms' => $filter['fabricant']
);
$filter_args['filter_fabricant'] = $filter['fabricant'];
}
} else if ( $queryType == SearchQueryType::FilteredTema ) {
if ( ! empty ( $filter ) ) {
$filter_args['tax_query'][] = array(
'taxonomy' => 'esdeveniment_cat',
'field' => 'slug',
'terms' => $filter
);
}
} else if ( $queryType == SearchQueryType::PagePrograma || $queryType == SearchQueryType::PageProjecte ) {
$filter_args = array(
'posts_per_page' => 20
);
} else {
$filter_args = array(
'meta_query' => array(
get_meta_query_value( 'data_fi', time(), '>=', 'NUMERIC' )
)
);
}
return array_merge( $base_args, $filter_args );
}
/*
* Creates a param to query using a meta field
*/
function get_meta_query_value( $key, $value, $compare, $type ) {
return array(
'key' => $key,
'value' => $value,
'compare' => $compare,
'type' => $type
);
}
/*
* Returns global paged variable
*/
function get_is_paged() {
global $paged;
return ( ! isset( $paged ) || ! $paged ) ? 1 : $paged;
}
/*
* Function to handle the date filter for events
*/
function add_query_vars_filter( $vars ) {
$vars[] = "cerca";
$vars[] = "sistema_operatiu";
$vars[] = "tipus";
$vars[] = "categoria_programa";
$vars[] = "paraula";
$vars[] = "tema";
$vars[] = "data";
$vars[] = "project";
$vars[] = "lletra";
$vars[] = "llengua";
$vars[] = "verb";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
/*
* Retrieve all url active parameters
*/
function get_current_querystring() {
$output = '';
$firstRun = true;
foreach ( $_GET as $key => $val ) {
if ( ! $firstRun ) {
$output .= "&";
} else {
$output = "?";
$firstRun = false;
}
$output .= sanitize_text_field( $key ) . "=" . sanitize_text_field( $val );
}