forked from not-only-code/qtranslate-slug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtranslate-slug.php
2274 lines (1686 loc) · 63.2 KB
/
qtranslate-slug.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
/*
Plugin Name: qTranslate slug
Plugin URI: http://not-only-code.github.com/qtranslate-slug/
Description: Allows to define a slug for each language and some qTranslate bug fixes
Version: 1.1
Author: Carlos Sanz Garcia
Author URI: http://github.com/not-only-code
*/
////////////////////////////////////////////////////////////////////////////////////////
if ( !function_exists('_debug') ):
function _debug( $message ) {
if ( WP_DEBUG === true ):
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
endif;
}
endif;
////////////////////////////////////////////////////////////////////////////////////////
/**
* QtranslateSlugWidget class
*
* @since 1.0
*/
class QtranslateSlugWidget extends WP_Widget {
function QtranslateSlugWidget() {
$widget_ops = array('classname' => 'qts_widget', 'description' => __('Allows your visitors to choose a Language.','qtranslate') );
$this->WP_Widget('qtranslateslug', __('Language selector', 'qts'), $widget_ops);
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = empty($instance['title']) ? __('Language', 'qtranslate') : apply_filters('widget_title', $instance['title']);
$hide_title = empty($instance['hide-title']) ? false : 'on';
$type = $instance['type'];
$short_text = ($instance['short_text'] == 'on') ? true : false ;
if( $type!='text' && $type!='image' && $type!='both' && $type!='dropdown' ) $type='text';
if( $hide_title!='on')
echo $before_title . $title . $after_title;
qts_language_menu($type, array( 'id' => $this->id, 'short' => $short_text ) );
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['hide-title'] = $new_instance['hide-title'];
$instance['type'] = $new_instance['type'];
$instance['short_text'] = $new_instance['short_text'];
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'hide-title' => false, 'type' => 'text' ) );
$title = $instance['title'];
$hide_title = $instance['hide-title'];
$type = $instance['type'];
$short_text = $instance['short_text'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'qtranslate'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('hide-title'); ?>"><?php _e('Hide Title:', 'qtranslate'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('hide-title'); ?>" name="<?php echo $this->get_field_name('hide-title'); ?>" <?php echo ($hide_title=='on')?'checked="checked"':''; ?>/></label></p>
<p><?php _e('Display:', 'qtranslate'); ?></p>
<p><label for="<?php echo $this->get_field_id('type'); ?>1"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>1" value="text"<?php echo ($type=='text')?' checked="checked"':'' ?>/> <?php _e('Text only', 'qtranslate'); ?></label></p>
<p><label for="<?php echo $this->get_field_id('type'); ?>2"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>2" value="image"<?php echo ($type=='image')?' checked="checked"':'' ?>/> <?php _e('Image only', 'qtranslate'); ?></label></p>
<p><label for="<?php echo $this->get_field_id('type'); ?>3"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>3" value="both"<?php echo ($type=='both')?' checked="checked"':'' ?>/> <?php _e('Text and Image', 'qtranslate'); ?></label></p>
<p><label for="<?php echo $this->get_field_id('type'); ?>4"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>4" value="dropdown"<?php echo ($type=='dropdown')?' checked="checked"':'' ?>/> <?php _e('Dropdown Box', 'qtranslate'); ?></label></p>
<p><label for="<?php echo $this->get_field_id('short_text'); ?>"><?php _e('Show short name (en):', 'qts'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('short_text'); ?>" name="<?php echo $this->get_field_name('short_text'); ?>" <?php checked($short_text, 'on') ?>/></label></p>
<p><?php _e('Display:', 'qtranslate'); ?></p>
<?php
}
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* QtranslateSlug class
*
* @since 1.0
*/
class QtranslateSlug {
/**
* array with old data system
*
* @var bool
*
* @since 1.0
*/
private $old_data = null;
/**
* stores permalink_structure option, for save queries to db
*
* @var string
*
* @since 1.0
*/
private $permalink_structure;
/**
* Stores options slugs from database
*
* @var array
*
* @since 1.0
*/
protected $options;
/**
* Variable used to override the language
*
* @var string
*
* @since 1.0
*/
private $lang = false;
/**
* slug in meta_key name in meta tables
*
* @var string
*
* @since 1.0
*/
private $meta_key = "_qts_slug_%s";
/**
* Array of translated versions of the current url
*
* @var array
*
* @since 1.0
*/
private $current_url = array();
/**
* return the current / temp language
*
* @since 1.0
*/
private function get_lang() {
global $q_config;
return ($this->lang) ? $this->lang : $q_config['language'];
}
/**
* getter: options
*
* @since 1.0
*/
public function get_options() {
$this->set_options();
return $this->options;
}
/**
* setter: options | permalink_structure
*
* @since 1.0
*/
public function set_options() {
if (empty($this->options))
$this->options = get_option(QTS_OPTIONS_NAME);
if (!$this->options)
add_option(QTS_OPTIONS_NAME, array());
if (is_null($this->permalink_structure))
$this->permalink_structure = get_option('permalink_structure');
}
/**
* setter: options | permalink_structure
*
* @since 1.0
*/
public function save_options($new_options = false) {
if (!$new_options || empty($new_options)) return;
if (count($this->options) != count($new_options)) return;
update_option(QTS_OPTIONS_NAME, $new_options);
$this->options = $new_options;
}
/**
* getter: meta key
*
* @since 1.0
*/
public function get_meta_key( $force_lang = false ) {
global $q_config;
$lang = $this->get_lang();
if ($force_lang) $lang = $force_lang;
return sprintf($this->meta_key, $lang); // returns: _qts_slug_en
}
/**
* check dependences for activation
*
* @since 1.0
*/
static function block_activate() {
global $wp_version;
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
return ( version_compare($wp_version, "3.3", "<" ) || !is_plugin_active('qtranslate/qtranslate.php') );
}
/**
* check if exists qtranslate and do the installation, support multisite
*
* @since 1.0
*/
public function install() {
global $wpdb;
if ( self::block_activate() ) return;
if ( function_exists('is_multisite') && is_multisite() ) {
if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
$old_blog = $wpdb->blogid;
$blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
foreach ($blogids as $blog_id) {
switch_to_blog($blog_id);
$this->activate();
}
switch_to_blog($old_blog);
return;
}
}
$this->activate();
}
/**
* activates and do the installation
*
* @since 1.0
*/
private function activate() {
global $wp_rewrite;
$this->set_options();
$qts_version = get_option('qts_version');
// checks version and do the installation
if ( !$qts_version || $qts_version != QTS_VERSION ) {
// install termmeta table using functions from Simple-Term-Meta ( http://wordpress.org/extend/plugins/simple-term-meta/ )
install_term_meta_table();
// update installed option
update_option('qts_version', QTS_VERSION);
}
// regenerate rewrite rules in db
add_action( 'generate_rewrite_rules', array($this, 'modify_rewrite_rules') );
flush_rewrite_rules();
}
/**
* actions when deactivating the plugin
*
* @since 1.0
*/
public function deactivate() {
global $wp_rewrite;
// regenerate rewrite rules in db
remove_action( 'generate_rewrite_rules', array($this, 'modify_rewrite_rules') );
$wp_rewrite->flush_rules();
}
/**
* admin notice: update your old data
*
* @since 1.0
*/
function notice_update(){
global $current_screen;
if ($current_screen->id != 'settings_page_qtranslate-slug-settings'):
echo "<div class=\"updated\">" . PHP_EOL;
echo "<p><strong>Qtranslate Slug:</strong></p>" . PHP_EOL;
printf("<p>%s <a href=\"%s\" class=\"button\">%s</a></p>", __('Please update your old data to the new system.', 'qts'), add_query_arg(array('page' => 'qtranslate-slug-settings'), 'options-general.php'), __('upgrade now', 'qts')) . PHP_EOL;
echo "</div>" . PHP_EOL;
endif;
}
/**
* admin notice: update your old data
*
* @since 1.0
*/
function notice_dependences(){
global $current_screen;
echo "<div class=\"error\">" . PHP_EOL;
echo "<p><strong>Qtranslate Slug:</strong></p>" . PHP_EOL;
echo "<p>" . __('This plugin requires at least <strong>Wordpress 3.3</strong> and <strong>Qtranslate(2.5.8 or newer)</strong>', 'qts') . "</p>" . PHP_EOL;
echo "</div>" . PHP_EOL;
}
/**
* checks if old table 'qtranslate_slug' exists and is not empty
*
* @return object | false
*
* @since 1.0
*/
public function check_old_data() {
global $wpdb;
if ($this->old_data === false) return false;
$table_name = $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}qtranslate_slug'");
if (!empty($table_name))
$this->old_data = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}qtranslate_slug");
if ( empty($table_name) || empty($this->old_data) )
$this->old_data = false;
return $this->old_data;
}
/**
* actions when deactivating the plugin
*
* @since 1.0
*/
private function check_old_versions() {
if ( $this->check_old_data() )
add_action('admin_notices', array($this, 'notice_update'));
}
/**
* Initialise the Class with all hooks
*
* @since 1.0
*/
function init() {
load_plugin_textdomain( 'qts', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// checking plugin activate
if ( self::block_activate() ) {
if (is_admin())
add_action('admin_notices', array($this, 'notice_dependences'));
return;
}
// caching qts options
$this->set_options();
if ( is_admin() ) {
$this->check_old_versions();
// add filters
add_filter( 'term_name', array($this, 'term_name'), 0, 2 );
add_filter( 'qts_validate_post_slug', array($this, 'validate_post_slug'), 0, 3 );
add_filter( 'qts_validate_post_slug', array($this, 'unique_post_slug'), 1, 3 );
add_filter( 'qts_validate_term_slug', array($this, 'validate_term_slug'), 0, 3 );
add_filter( 'qts_validate_term_slug', array($this, 'unique_term_slug'), 1, 3 );
// admin actions
add_action( 'admin_menu', array($this, 'add_slug_meta_box') );
add_action( 'save_post', array($this, 'save_postdata'), 605, 2 );
add_action( 'delete_term', array($this, 'delete_term'), 0, 3);
add_action( 'created_term', array($this, 'save_term'), 605, 3);
add_action( 'edited_term', array($this, 'save_term'), 605, 3 );
add_action( 'admin_head', array($this, 'hide_slug_box'), 900 );
add_action( 'init', array($this, 'taxonomies_hooks'), 805 );
add_action( 'wp_dashboard_setup', array($this, 'remove_dashboard_widgets') );
add_action( 'admin_init', array($this, 'fix_nav_menu') );
} else {
add_filter( 'request', array($this, 'filter_request') );
}
add_filter( 'query_vars', array($this, 'query_vars'));
add_action( 'generate_rewrite_rules', array($this, 'modify_rewrite_rules') );
// remove some Qtranslate filters
remove_filter( 'page_link', 'qtrans_convertURL' );
remove_filter( 'post_link', 'qtrans_convertURL' );
remove_filter( 'category_link', 'qtrans_convertURL' );
remove_filter( 'tag_link', 'qtrans_convertURL' );
add_filter( 'qts_permastruct' , array($this, 'get_extra_permastruct'), 0, 2);
add_filter( 'qts_url_args', array($this, 'parse_url_args'), 0, 1);
add_filter( 'home_url', array($this, 'home_url'), 10, 4);
add_filter( 'post_type_link', array($this, 'post_type_link'), 600, 4 );
add_filter( 'post_link', array($this, 'post_link'), 0, 3 );
add_filter( '_get_page_link', array($this, '_get_page_link'), 0, 2 );
add_filter( 'term_link', array($this, 'term_link'), 600, 3 );
add_filter( 'single_term_title', 'qtrans_useTermLib', 805 );
add_filter( 'get_blogs_of_user', array($this, 'blog_names'), 1 );
add_action( 'widgets_init', array($this, 'widget_init'), 100 );
}
/**
* Adds news rules to translate the URL bases, this function must be called on flush_rewrite or 'flush_rewrite_rules'
*
* @param object $wp_rewrite
*
* @since 1.0
*/
public function modify_rewrite_rules() {
global $wp_rewrite;
// post types rules
$post_types = get_post_types( array('_builtin' => false ), 'objects');
foreach ( $post_types as $post_type )
$this->generate_extra_rules( $post_type->name );
// taxonomies rules
$taxonomies = $this->get_public_taxonomies();
foreach ( $taxonomies as $taxonomy )
$this->generate_extra_rules( $taxonomy->name );
}
/**
* Helper: news rules to translate the URL bases
*
* @param string $name name of extra permastruct
* @param string $type 'post_type' or 'taxonomy'
*
* @since 1.0
*/
private function generate_extra_rules( $name = false ) {
global $wp_rewrite, $q_config;
foreach ($q_config['enabled_languages'] as $lang):
if ( $base = $this->get_base_slug( $name, $lang) ):
$struct = $wp_rewrite->extra_permastructs[$name];
if ( is_array( $struct ) ) {
if ( count( $struct ) == 2 )
$rules = $wp_rewrite->generate_rewrite_rules( "/$base/%$name%", $struct[1] );
else
$rules = $wp_rewrite->generate_rewrite_rules( "/$base/%$name%", $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] );
} else {
$rules = $wp_rewrite->generate_rewrite_rules( "/$base/%$name%" );
}
$wp_rewrite->rules = array_merge($rules, $wp_rewrite->rules);
endif;
endforeach;
}
/**
* Helper that gets a base slug stored in options
*
* @param string $name of extra permastruct
* @return string base slug for 'post_type' and 'language' or false
*
* @since 1.0
*/
public function get_base_slug($name = false, $lang = false) {
if ( !$name || !$lang ) return false;
if ( taxonomy_exists($name) ) {
$type = 'taxonomy';
} else if ( post_type_exists($name) ) {
$type = 'post_type';
} else {
return false;
}
$qts_options = $this->get_options();
$option_name = QTS_PREFIX . $type . '_' . $name;
if ( !isset($qts_options[$option_name]) || empty($qts_options[$option_name]) ) return false;
$option = $qts_options[$option_name][$lang];
if (isset($option)) return $option;
return false;
}
/**
* Helper: returns public built-in and not built-in taxonomies
*
* @return array of public taxonomies objects
*
* @since 1.0
*/
private function get_public_taxonomies() {
$builtin = get_taxonomies( array( 'public' => true, 'show_ui' => true, '_builtin' => true ), 'object');
$taxonomies = get_taxonomies( array( 'public' => true, 'show_ui' => true, '_builtin' => false ), 'object' );
return array_merge( $builtin, $taxonomies );
}
/**
* parse and adds $_GET args passed to an url
*
* @param string $url parameters
* @param string $lang processed
* @return string converted url
*
* @since 1.0
*/
public function parse_url_args( $url ) {
global $q_config;
if (is_admin()) return $url;
$url = preg_replace('/&/', '&', $url);
// if no permalink structure ads ?lang=en
$base_query = parse_url($q_config['url_info']['original_url']);
$base_args = isset($base_query['query']) ? wp_parse_args($base_query['query']) : array();
if ( empty($this->permalink_structure) || $q_config['url_mode'] == 1 )
$base_args['lang'] = $this->get_lang();
// rebulid query with all args
$url = add_query_arg($base_args, $url);
return $url;
}
/**
* Fix get_page_by_path when querying vars
*
* @param $query_vars objec query vars founded
* @return object $query_vars processed
*
* @since 1.0
*/
public function query_vars( $query_vars ) {
global $wp, $wp_rewrite;
$wp->query_vars = array();
$post_type_query_vars = array();
// Fetch the rewrite rules.
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ( ! empty($rewrite) ) {
// If we match a rewrite rule, this will be cleared.
$error = '404';
$wp->did_permalink = true;
if ( isset($_SERVER['PATH_INFO']) )
$pathinfo = $_SERVER['PATH_INFO'];
else
$pathinfo = '';
$pathinfo_array = explode('?', $pathinfo);
$pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
$req_uri = $_SERVER['REQUEST_URI'];
$req_uri_array = explode('?', $req_uri);
$req_uri = $req_uri_array[0];
$self = $_SERVER['PHP_SELF'];
$home_path = parse_url(home_url());
if ( isset($home_path['path']) )
$home_path = $home_path['path'];
else
$home_path = '';
$home_path = trim($home_path, '/');
// Trim path info from the end and the leading home path from the
// front. For path info requests, this leaves us with the requesting
// filename, if any. For 404 requests, this leaves us with the
// requested permalink.
$req_uri = str_replace($pathinfo, '', $req_uri);
$req_uri = trim($req_uri, '/');
$req_uri = preg_replace("|^$home_path|", '', $req_uri);
$req_uri = trim($req_uri, '/');
$pathinfo = trim($pathinfo, '/');
$pathinfo = preg_replace("|^$home_path|", '', $pathinfo);
$pathinfo = trim($pathinfo, '/');
$self = trim($self, '/');
$self = preg_replace("|^$home_path|", '', $self);
$self = trim($self, '/');
// The requested permalink is in $pathinfo for path info requests and
// $req_uri for other requests.
if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
$request = $pathinfo;
} else {
// If the request uri is the index, blank it out so that we don't try to match it against a rule.
if ( $req_uri == $wp_rewrite->index )
$req_uri = '';
$request = $req_uri;
}
$wp->request = $request;
// Look for matches.
$request_match = $request;
if ( empty( $request_match ) ) {
// An empty request could only match against ^$ regex
if ( isset( $rewrite['$'] ) ) {
$wp->matched_rule = '$';
$query = $rewrite['$'];
$matches = array('');
}
} else if ( $req_uri != 'wp-app.php' ) {
foreach ( (array) $rewrite as $match => $query ) {
// If the requesting file is the anchor of the match, prepend it to the path info.
if ( ! empty($req_uri) && strpos($match, $req_uri) === 0 && $req_uri != $request )
$request_match = $req_uri . '/' . $request;
if ( preg_match("#^$match#", $request_match, $matches) ||
preg_match("#^$match#", urldecode($request_match), $matches) ) {
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
// this is a verbose page match, lets check to be sure about it
if ( ! $page_foundid = $this->get_page_by_path( $matches[ $varmatch[1] ] ) ) {
continue;
} else {
wp_cache_set('qts_page_request', $page_foundid); // caching query :)
}
}
// Got a match.
$wp->matched_rule = $match;
break;
}
}
}
if ( isset( $wp->matched_rule ) ) {
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
$wp->matched_query = $query;
// Parse the query.
parse_str($query, $perma_query_vars);
// If we're processing a 404 request, clear the error var
// since we found something.
unset( $_GET['error'] );
unset( $error );
}
// If req_uri is empty or if it is a request for ourself, unset error.
if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
unset( $_GET['error'] );
unset( $error );
if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
unset( $perma_query_vars );
$wp->did_permalink = false;
}
}
return $wp->public_query_vars;
}
/**
* Function called when query parameters are processed by Wordpress.
*
* @param $query query parameters
* @return array() $query processed
*
* @since 1.0
*/
function filter_request( $query ) {
global $q_config, $wp_query, $wp;
if (isset($wp->matched_query))
$query = wp_parse_args($wp->matched_query);
foreach (get_post_types() as $post_type)
if ( array_key_exists($post_type, $query) && !in_array($post_type, array('post', 'page')) ) $query['post_type'] = $post_type;
$page_foundit = false;
// -> page
if ( isset($query['pagename']) || isset($query['page_id']) ):
$page = wp_cache_get('qts_page_request');
if (!$page)
$page = isset($query['page_id']) ? get_page($query['page_id']) : $this->get_page_by_path($query['pagename']);
if (!$page) return $query;
$id = $page->ID;
$cache_array = array($page);
update_post_caches($cache_array, 'page'); // caching query :)
wp_cache_delete('qts_page_request');
$query['pagename'] = get_page_uri($page);
$function = 'get_page_link';
// -> custom post type
elseif ( isset($query['post_type']) ):
$page_slug = ( isset($query['name']) && !empty($query['name']) ) ? $query['name'] : $query[$query['post_type']];
$page = $this->get_page_by_path($page_slug, OBJECT, $query['post_type']);
if (!$page) return $query;
$id = $page->ID;
$cache_array = array($page);
update_post_caches($cache_array, $query['post_type']); // caching query :)
$query['name'] = $query[$query['post_type']] = get_page_uri($page);
$function = 'get_post_permalink';
// -> post
elseif ( isset($query['name']) || isset($query['p']) ):
$post = isset($query['p']) ? get_post($query['p']) : $this->get_page_by_path($query['name'], OBJECT, 'post');
if (!$post) return $query;
$query['name'] = $post->post_name;
$id = $post->ID;
$cache_array = array($post);
update_post_caches($cache_array);
$function = 'get_permalink';
// -> category
elseif ( ( isset($query['category_name']) || isset($query['cat'])) ):
if ( isset($query['category_name']) )
$term_slug = $this->get_last_slash( $query['category_name'] );
$term = isset($query['cat']) ? get_term($query['cat'], 'category') : $this->get_term_by('slug', $term_slug, 'category');
if (!$term) return $query;
$cache_array = array($term);
update_term_cache($cache_array, 'category'); // caching query :)
$id = $term->term_id;
$query['category_name'] = $term->slug; // uri
$function = 'get_category_link';
// -> tag
elseif ( isset($query['tag']) ):
$term = $this->get_term_by('slug', $query['tag'], 'post_tag');
if (!$term) return $query;
$cache_array = array($term);
update_term_cache($cache_array, 'post_tag'); // caching query :)
$id = $term->term_id;
$query['tag'] = $term->slug;
$function = 'get_tag_link';
endif;
// -> taxonomy
$taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) );
foreach ($taxonomies as $term_name):
if ( isset($query[$term_name]) ) {
$term_slug = $this->get_last_slash( $query[$term_name] );
$term = $this->get_term_by('slug', $term_slug, $term_name);
if (!$term) return $query;
$cache_array = array($term);
update_term_cache($cache_array, $term_name); // caching query :)
$id = $term;
$query[$term_name] = $term->slug;
$function = 'get_term_link';
}
endforeach;
// -> home url
if ( empty($query) ):
$function = 'home_url';
$id = '';
endif;
if ( isset($function) ):
// parse all languages links
foreach( $q_config['enabled_languages'] as $lang ) {
$this->lang = $lang;
$this->current_url[$lang] = apply_filters('qts_url_args', call_user_func($function, $id));
}
$this->lang = false;
endif;
return $query;
}
/**
* Parse a hierarquical name and extract the last one
*
* @param string $lang Page path
* @return string
*
* @since 1.0
*/
public function get_current_url( $lang = false ) {
global $q_config;
if (!$lang) $lang = $this->get_lang();
if (isset($this->current_url[$lang]) && !empty($this->current_url[$lang]))
return $this->current_url[$lang];
return '';
}
/**
* Parse a hierarquical name and extract the last one
*
* @param string $slug Page path
* @return string
*
* @since 1.0
*/
private function get_last_slash($slug) {
$slug = rawurlencode( urldecode( $slug ) );
$slug = str_replace('%2F', '/', $slug);
$slug = str_replace('%20', ' ', $slug);
return array_pop( explode('/', $slug) );
}
/**
* Retrieves a page id given its path.
*
* @param string $page_path Page path
* @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
* @param string $post_type Optional. Post type. Default page.
* @return mixed Null when complete.
*
* @since 1.0
*/
private function get_page_id_by_path($page_path, $output = OBJECT, $post_type = 'page') {
global $wpdb;
$page_path = rawurlencode(urldecode($page_path));
$page_path = str_replace('%2F', '/', $page_path);
$page_path = str_replace('%20', ' ', $page_path);
$parts = explode( '/', trim( $page_path, '/' ) );
$parts = array_map( 'esc_sql', $parts );
$parts = array_map( 'sanitize_title_for_query', $parts );
$in_string = "'". implode( "','", $parts ) . "'";
$meta_key = $this->get_meta_key();
$post_type_sql = $post_type;
$wpdb->escape_by_ref( $post_type_sql );
$pages = $wpdb->get_results( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent, $wpdb->postmeta.meta_value FROM $wpdb->posts,$wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value IN ($in_string) AND ($wpdb->posts.post_type = '$post_type_sql' OR $wpdb->posts.post_type = 'attachment')", OBJECT_K );
$revparts = array_reverse( $parts );
$foundid = 0;
foreach ( (array) $pages as $page ) {
if ( $page->meta_value == $revparts[0] ) {
$count = 0;
$p = $page;
while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) {
$count++;
$parent = $pages[ $p->post_parent ];
if ( ! isset( $revparts[ $count ] ) || $parent->meta_value != $revparts[ $count ] )
break;
$p = $parent;
}
if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->meta_value == $revparts[ $count ] ) {
$foundid = $page->ID;
break;
}
}
}
if ( $foundid ) {
return $foundid;
} else {
$last_part = array_pop($parts);
$page_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '$last_part' AND (post_type = '$post_type_sql' OR post_type = 'attachment')" );
if ( $page_id )
return $page_id;
}
return null;
}
/**
* Retrieves a page given its path.
*
* @param string $page_path Page path
* @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
* @param string $post_type Optional. Post type. Default page.
* @return mixed Null when complete.