-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfunctions.php
More file actions
1607 lines (1433 loc) · 44.6 KB
/
Copy pathfunctions.php
File metadata and controls
1607 lines (1433 loc) · 44.6 KB
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
/**
* Miscellaneous utility functions
*
* @since 1.7
*/
/**
* Register a PressForward module.
*
* This function allows developers to register modules into the array
* of PressForward modules. Developers can do this to take advantage
* of a variaty of PressForward function and to make it appear in
* the PressForward dashboard.
*
* @since 2.x.x
*
* @param array $args {
* Required. An array of arguments describing the module.
*
* @var string $slug A non-capatalized safe string.
* @var string $class The name of the module's class.
* Must match the folder name.
* }
* @return null
*/
function pressforward_register_module( $args ) {
$defaults = array(
'slug' => '',
'class' => '',
);
$r = wp_parse_args( $args, $defaults );
// We need the 'class' and 'slug' terms
if ( empty( $r['class'] ) || empty( $r['slug'] ) ) {
return;
}
// Ensure the class exists before attempting to initialize it
// @todo Should probably have better error reporting
if ( ! class_exists( $r['class'] ) ) {
return;
}
add_filter( 'pressforward_register_modules', create_function( '$modules', '
return array_merge( $modules, array( array(
"slug" => "' . $r['slug'] . '",
"class" => "' . $r['class'] . '",
) ) );
' ) );
}
/**
* Echoes the URL of the admin page
*
* @since 1.7
*/
function pf_admin_url() {
echo pf_get_admin_url();
}
/**
* Returns the URL of the admin page
*
* @return string
*/
function pf_get_admin_url() {
return add_query_arg( 'page', PF_SLUG . '-options', admin_url( 'admin.php' ) );
}
/**
* Echoes the Nominate This bookmarklet link
*
* @since 1.7
*/
function pf_shortcut_link() {
echo pf_get_shortcut_link();
}
function start_pf_nom_this(){
global $pagenow;
//var_dump('2test2<pre>',$pagenow); die();
if( 'edit.php' == $pagenow && array_key_exists( 'pf-nominate-this', $_GET ) && 2 == $_GET['pf-nominate-this']) {
//var_dump(dirname(__FILE__),$wp_query->get('pf-nominate-this'),file_exists(dirname(__FILE__).'/nomthis/nominate-this.php'),(dirname(__FILE__).'/nomthis/nominate-this.php')); die();
//$someVar = $wp_query->get('some-var');
include(dirname(__FILE__).'/nomthis/nominate-this.php');
die();
}
return '';
}
/**
* Retrieve the Nominate This bookmarklet link.
*
* Use this in 'a' element 'href' attribute.
*
* @since 1.7
* @see get_shortcut_link()
*
* @return string
*/
function pf_get_shortcut_link() {
return pf_nomthis_bookmarklet();
$url = trailingslashit(get_bloginfo('wpurl')).'wp-admin/edit.php?pf-nominate-this=2';
// In case of breaking changes, version this. #WP20071
$link = "javascript:
var d=document,
w=window,
e=w.getSelection,
k=d.getSelection,
x=d.selection,
s=(e?e():(k)?k():(x?x.createRange().text:0)),
f='" . $url . "',
l=d.location,
e=encodeURIComponent,
u=f+'&u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720px,height=620px'))l.href=u;};
if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
void(0)";
$link = "javascript:
var d=document,
w=window,
e=w.getSelection,
k=d.getSelection,
x=d.selection,
s=(e?e():(k)?k():(x?x.createRange().text:0)),
f='" . rest_url(pressforward('api.nominatethis')->endpoint_for_nominate_this_endpoint) . "',
l=d.location,
e=encodeURIComponent,
u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=620'))l.href=u;};
if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
void(0)";
$link = str_replace( array( "\r", "\n", "\t" ), '', $link );
return apply_filters( 'shortcut_link', $link );
}
/**
* Retrieve the Nominate This bookmarklet link.
*
* Use this in 'a' element 'href' attribute.
*
* @since 1.7
* @see get_shortcut_link()
*
* @return string
*/
function pf_nomthis_bookmarklet() {
//get_site_url(null, 'wp-json/'
$user = wp_get_current_user();
$user_id = $user->ID;
$link = "javascript:
var d=document,
w=window,
e=w.getSelection,
k=d.getSelection,
x=d.selection,
s=(e?e():(k)?k():(x?x.createRange().text:0)),
l=d.location,
e=encodeURIComponent,
ku='".bin2hex(pressforward('controller.jwt')->get_a_user_public_key())."',
ki='".get_user_meta($user_id, 'pf_jwt_private_key', true)."',
p='" . rest_url().pressforward('api.nominatethis')->endpoint_for_nominate_this_script . "?k='+ku,
pe=document.createElement('script'),
a=function(){pe.src=p;document.getElementsByTagName('head')[0].appendChild(pe);};
if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
void(0)";
$link = str_replace( array( "\r", "\n", "\t" ), '', $link );
return apply_filters( 'pf_nomthis_bookmarklet', $link );
}
/**
* Get the feed item post type name
*
* @since 1.7
*
* @return string The name of the feed item post_type for PressForward.
*/
function pf_feed_item_post_type() {
return pressforward( 'schema.feed_item' )->post_type;
}
/**
* Get the feed item tag taxonomy name
*
* @since 1.7
*
* @return string The slug for the taxonomy used by feed items.
*/
function pf_feed_item_tag_taxonomy() {
return pressforward( 'schema.feed_item' )->tag_taxonomy;
}
/**
* Get a feed excerpt
*/
function pf_feed_excerpt( $text ) {
$text = apply_filters( 'the_content', $text );
$text = str_replace( '\]\]\>', ']]>', $text );
$text = preg_replace( '@<script[^>]*?>.*?</script>@si', '', $text );
$text = strip_tags( $text );
$text = substr( $text, 0, 260 );
$excerpt_length = 28;
$words = explode( ' ', $text, $excerpt_length + 1 );
array_pop( $words );
array_push( $words, '...' );
$text = implode( ' ', $words );
$contentObj = pressforward( 'library.htmlchecker' );
$item_content = $contentObj->closetags( $text );
return $text;
}
/**
* Sanitize a string for use in URLs and filenames
*
* @since 1.7
* @link http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe
*
* @param string $string The string to be sanitized
* @param bool $force_lowercase True to force all characters to lowercase
* @param bool $anal True to scrub all non-alphanumeric characters
* @return string $clean The cleaned string
*/
function pf_sanitize( $string, $force_lowercase = true, $anal = false ) {
$strip = array(
'~',
'`',
'!',
'@',
'#',
'$',
'%',
'^',
'&',
'*',
'(',
')',
'_',
'=',
'+',
'[',
'{',
']',
'}',
'\\',
'|',
';',
':',
'"',
"'",
'‘',
'’',
'“',
'”',
'–',
'—',
'',
'',
',',
'<',
'.',
'>',
'/',
'?',
);
if ( is_array( $string ) ) {
$string = implode( ' ', $string );
}
$clean = trim( str_replace( $strip, '', strip_tags( $string ) ) );
$clean = preg_replace( '/\s+/', '-', $clean );
$clean = ($anal) ? preg_replace( '/[^a-zA-Z0-9]/', '', $clean ) : $clean ;
return ($force_lowercase) ?
(function_exists( 'mb_strtolower' )) ?
mb_strtolower( $clean, 'UTF-8' ) :
strtolower( $clean ) :
$clean;
}
/**
* Create a slug from a string
*
* @since 1.7
* @uses pf_sanitize()
*
* @param string $string The string to convert
* @param bool $case True to force all characters to lowercase
* @param bool $string True to scrub all non-alphanumeric characters
* @param bool $spaces False to strip spaces
* @return string $stringSlug The sanitized slug
*/
function pf_slugger( $string, $case = false, $strict = true, $spaces = false ) {
if ( $spaces == false ) {
$string = strip_tags( $string );
$stringArray = explode( ' ', $string );
$stringSlug = '';
foreach ( $stringArray as $stringPart ) {
$stringSlug .= ucfirst( $stringPart );
}
$stringSlug = str_replace( '&','&', $stringSlug );
// $charsToElim = array('?','/','\\');
$stringSlug = pf_sanitize( $stringSlug, $case, $strict );
} else {
// $string = strip_tags($string);
// $stringArray = explode(' ', $string);
// $stringSlug = '';
// foreach ($stringArray as $stringPart){
// $stringSlug .= ucfirst($stringPart);
// }
$stringSlug = str_replace( '&','&', $string );
// $charsToElim = array('?','/','\\');
$stringSlug = pf_sanitize( $stringSlug, $case, $strict );
}
return $stringSlug;
}
/**
* Convert data to the standardized item format expected by PF
*
* @since 1.7
* @todo Take params as an array and use wp_parse_args()
*
* @return array $itemArray
*/
function pf_feed_object( $itemTitle = '', $sourceTitle = '', $itemDate = '', $itemAuthor = '', $itemContent = '', $itemLink = '', $itemFeatImg = '', $itemUID = '', $itemWPDate = '', $itemTags = '', $addedDate = '', $sourceRepeat = '', $postid = '', $readable_status = '', $obj = array() ) {
// Assemble all the needed variables into our fancy object!
$itemArray = array(
'item_title' => $itemTitle,
'source_title' => $sourceTitle,
'item_date' => $itemDate,
'item_author' => $itemAuthor,
'item_content' => $itemContent,
'item_link' => $itemLink,
'item_feat_img' => $itemFeatImg,
'item_id' => $itemUID,
'item_wp_date' => $itemWPDate,
'item_tags' => $itemTags,
'item_added_date' => $addedDate,
'source_repeat' => $sourceRepeat,
'post_id' => $postid,
'readable_status' => $readable_status,
'obj' => $obj,
);
return $itemArray;
}
function create_feed_item_id( $url, $title ) {
$url = str_replace('http://', '', $url);
$url = str_replace('https://', '', $url);
$hash = md5( untrailingslashit(trim($url)) );
return $hash;
}
/**
* Get all posts with 'item_id' set to a given item id
*
* @since 1.7
*
* @param string $post_type The post type to limit results to.
* @param string $item_id The origin item id.
* @param bool $ids_only Set to true if you want only an array of IDs returned in the query.
*
* @return object A standard WP_Query object.
*/
function pf_get_posts_by_id_for_check( $post_type = false, $item_id, $ids_only = false ) {
global $wpdb;
// If the item is less than 24 hours old on nomination, check the whole database.
// $theDate = getdate();
// $w = date('W');
$r = array(
'meta_key' => pressforward('controller.metas')->get_key('item_id'),
'meta_value' => $item_id,
'post_type' => array( 'post', pf_feed_item_post_type() ),
);
if ( $ids_only ) {
$r['fields'] = 'ids';
$r['no_found_rows'] = true;
$r['cache_results'] = false;
}
if ( false != $post_type ) {
$r['post_type'] = $post_type;
}
$postsAfter = new WP_Query( $r );
pf_log( ' Checking for posts with item ID ' . $item_id . ' returned query with ' . $postsAfter->post_count . ' items.' );
// pf_log($postsAfter);
return $postsAfter;
}
/**
* Create the hidden inputs used when nominating a post from All Content
*
* @since 1.7
*/
function pf_prep_item_for_submit( $item ) {
$item['item_content'] = htmlspecialchars( $item['item_content'] );
$itemid = $item['item_id'];
foreach ( $item as $itemKey => $itemPart ) {
if ( $itemKey == 'item_content' ) {
$itemPart = htmlspecialchars( $itemPart );
}
if ( is_array( $itemPart ) ) {
if ( 'nominators' === $itemKey ) {
$itemPart = array_keys( $itemPart );
}
$itemPart = implode( ',',$itemPart );
}
echo '<input type="hidden" name="' . $itemKey . '" id="' . $itemKey . '_' . $itemid . '" id="' . $itemKey . '" value="' . $itemPart . '" />';
}
}
function pf_get_user_level( $option, $default_level ) {
}
/**
* Converts an https URL into http, to account for servers without SSL access.
* If a function is passed, pf_de_https will return the function result
* instead of the string.
*
* @since 1.7
*
* @param string $url
* @param string|array $function Function to call first to try and get the URL.
* @return string|object $r Returns the string URL, converted, when no function is passed.
*
* otherwise returns the result of the function after being checked for accessability.
*/
function pf_de_https( $url, $function = false ) {
$args = func_get_args();
$url_orig = $url;
$url = str_replace( '&','&', $url );
$url_first = $url;
if ( ! $function ) {
$r = set_url_scheme( $url, 'http' );
return $r;
} else {
return pressforward( 'controller.http_tools' )->get_url_content( $url_orig, $function );
}
}
/**
* Derived from WordPress's fetch feed function at: https://developer.wordpress.org/reference/functions/fetch_feed/
*/
function pf_fetch_feed( $url ){
$theFeed = fetch_feed( $url );
if ( is_wp_error( $theFeed ) ) {
if ( ! class_exists( 'SimplePie', false ) ) {
require_once( ABSPATH . WPINC . '/class-simplepie.php' );
}
require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
$feed = new SimplePie();
$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
// We must manually overwrite $feed->sanitize because SimplePie's
// constructor sets it before we have a chance to set the sanitization class
$feed->sanitize = new WP_SimplePie_Sanitize_KSES();
$feed->set_cache_class( 'WP_Feed_Cache' );
$feed->set_file_class( 'WP_SimplePie_File' );
add_filter( 'pf_encoding_retrieval_control', '__return_false' );
$feedXml = pf_de_https( $url, 'wp_remote_get' );
//$feedXml = mb_convert_encoding($feedXml['body'], 'UTF-8');
$feed->set_raw_data($feedXml['body']);
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
/**
* Fires just before processing the SimplePie feed object.
*
* @param object $feed SimplePie feed object (passed by reference).
* @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
*/
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
$feed->handle_content_type();
$feed->set_output_encoding( get_option( 'blog_charset' ) );
if ( $feed->error() ){
return new WP_Error( 'simplepie-error', $feed->error() );
}
return $feed;
} else {
return $theFeed;
}
}
/**
* Converts and echos a list of terms to a set of slugs to be listed in the nomination CSS selector
*/
function pf_nom_class_tagger( $array = array() ) {
foreach ( $array as $class ) {
if ( ($class == '') || (empty( $class )) || ( ! isset( $class )) ) {
// Do nothing.
} elseif ( is_array( $class ) ) {
foreach ( $class as $subclass ) {
echo ' ';
echo pf_slugger( $class, true, false, true );
}
} else {
echo ' ';
echo pf_slugger( $class, true, false, true );
}
}
}
/**
* Converts and returns a list of terms as a set of slugs useful for nominations
*
* @since 1.7
*
* @param array $array A set of terms.
*
* @return string|object $tags A string containing a comma-seperated list of slugged tags.
*/
function get_pf_nom_class_tags( $array = array() ) {
foreach ( $array as $class ) {
if ( ($class == '') || (empty( $class )) || ( ! isset( $class )) ) {
// Do nothing.
$tags = '';
} elseif ( is_array( $class ) ) {
foreach ( $class as $subclass ) {
$tags = ' ';
$tags = pf_slugger( $class, true, false, true );
}
} else {
$tags = ' ';
$tags = pf_slugger( $class, true, false, true );
}
}
return $tags;
}
/**
* Build an excerpt for a nomination. For filtering.
*
* @param string $text
*/
function pf_noms_filter( $text ) {
global $post;
$text = get_the_content( '' );
$text = apply_filters( 'the_content', $text );
$text = str_replace( '\]\]\>', ']]>', $text );
$text = preg_replace( '@<script[^>]*?>.*?</script>@si', '', $text );
$contentObj = pressforward( 'library.htmlchecker' );
$text = $contentObj->closetags( $text );
$text = strip_tags( $text, '<p>' );
$excerpt_length = 310;
$words = explode( ' ', $text, $excerpt_length + 1 );
if ( is_array( $words ) && ( count( $words ) > $excerpt_length ) ) {
array_pop( $words );
array_push( $words, '...' );
$text = implode( ' ', $words );
}
return $text;
}
/**
* Build an excerpt for nominations.
*
* @since 1.7
*
* @param string $text
*
* @return string $r Returns the adjusted excerpt.
*/
function pf_noms_excerpt( $text ) {
$text = apply_filters( 'the_content', $text );
$text = str_replace( '\]\]\>', ']]>', $text );
$text = preg_replace( '@<script[^>]*?>.*?</script>@si', '', $text );
$contentObj = pressforward( 'library.htmlchecker' );
$text = $contentObj->closetags( $text );
$text = strip_tags( $text, '<p>' );
$excerpt_length = 310;
$words = explode( ' ', $text, $excerpt_length + 1 );
if ( is_array( $words ) && ( count( $words ) > $excerpt_length ) ) {
array_pop( $words );
array_push( $words, '...' );
$text = implode( ' ', $words );
}
return $text;
}
/**
* Get an object with capabilities as keys pointing to roles that contain those capabilities.
*
* @since 3.x
*
* @param string $cap Optional. If given, the function will return a set of roles that have that capability.
*
* @return array $role_reversal An array with capailities as keys pointing to what roles they match to.
*/
function pf_get_capabilities( $cap = false ) {
// Get the WP_Roles object.
global $wp_roles;
// Set up array for storage.
$role_reversal = array();
// Walk through the roles object by role and get capabilities.
foreach ( $wp_roles->roles as $role_slug => $role_set ) {
foreach ( $role_set['capabilities'] as $capability => $cap_bool ) {
// Don't store a capability if it is false for the role (though none are).
if ( $cap_bool ) {
$role_reversal[ $capability ][] = $role_slug;
}
}
}
// Allow users to get specific capabilities.
if ( ! $cap ) {
return $role_reversal;
} else {
return $role_reversal[ $cap ];
}
}
/**
* Request a role string or object by asking for its capability.
*
* Function allows the user to find out a role by a capability that it holds.
* The user may specify the higest role with that capability or the lowest.
* The lowest is the default.
*
* @since 3.x
*
* @param string $cap The slug for the capacity being checked against.
* @param bool $lowest Optional. If the function should return the lowest capable role. Default true.
* @param bool $obj Optional. If the function should return a role object instead of a string. Default false.
*
* @return string|object Returns either the string name of the role or the WP object created by get_role.
*/
function pf_get_role_by_capability( $cap, $lowest = true, $obj = false ) {
// Get set of roles for capability.
$roles = pf_get_capabilities( $cap );
// We probobly want to get the lowest role with that capability
if ( $lowest ) {
$roles = array_reverse( $roles );
}
$arrayvalues = array_values( $roles );
$the_role = array_shift( $arrayvalues );
if ( ! $obj ) {
return $the_role;
} else {
return get_role( $the_role );
}
}
/**
* Get the capability that uniquely matches a specific role.
*
* If we want to allow users to set access by role, we need to give users the names
* of all roles. But Wordpress takes capabilities. This function matches the role with
* its first capability, so users can set by Role but WordPress takes capability.
*
* However, it will check against the system options and either attempt to return
* this information based on WordPress defaults or by checking the current system.
*
* @since 3.x
*
* @param string $role_slug The slug for the role being checked against.
*
* @return string The slug for the defining capability of the given role.
*/
function pf_get_defining_capability_by_role( $role_slug ) {
$pf_use_advanced_user_roles = get_option( 'pf_use_advanced_user_roles', 'no' );
// For those who wish to ignore the super-cool auto-detection for fringe-y sites that
// let their user capabilities go wild.
if ( 'no' != $pf_use_advanced_user_roles ) {
$caps = pf_get_capabilities();
foreach ( $caps as $slug => $cap ) {
$low_role = pf_get_role_by_capability( $slug );
// Return the first capability only applicable to that role.
if ( $role_slug == ($low_role) ) {
return $slug;
}
}
}
// Even if we use $pf_use_advanced_user_roles, if it doesn't find any actual lowest option (like it is the case with contributor currently), it should still go to the default ones below
$role_slug = strtolower( $role_slug );
switch ( $role_slug ) {
case 'administrator':
return 'manage_options';
break;
case 'editor':
return 'edit_others_posts';
break;
case 'author':
return 'publish_posts';
break;
case 'contributor':
return 'edit_posts';
break;
case 'subscriber':
return 'read';
break;
}
}
function pf_capability_mapper( $cap, $role_slug ) {
$feed_caps = pressforward( 'schema.feeds' )->map_feed_caps();
$feed_item_caps = pressforward( 'schema.feed_item' )->map_feed_item_caps();
if ( array_key_exists( $cap, $feed_caps ) ) {
$role = get_role( $role_slug );
$role->add_cap( $feed_caps[ $cap ] );
}
if ( array_key_exists( $cap, $feed_item_caps ) ) {
$role = get_role( $role_slug );
$role->add_cap( $feed_item_caps[ $cap ] );
}
}
function assign_pf_to_standard_roles() {
$roles = array(
'administrator',
'editor',
'author',
'contributor',
'subscriber',
);
$caps = pf_get_capabilities();
// $feed_caps = pressforward('schema.feeds')->map_feed_caps();
// $feed_item_caps = pressforward()->schema->map_feed_item_caps();
foreach ( $caps as $cap => $role ) {
foreach ( $role as $a_role ) {
pf_capability_mapper( $cap, $a_role );
}
}
}
/**
* A function to filter authors and, if available, replace their display with the origonal item author.
*
* Based on http://seoserpent.com/wordpress/custom-author-byline
*
* @since 3.x
*
* @param string $author The author string currently being displayed.
*
* @return string Returns the author.
*/
function pf_replace_author_presentation( $author ) {
global $post;
if ( 'yes' == get_option( 'pf_present_author_as_primary', 'yes' ) ) {
$custom_author = pressforward( 'controller.metas' )->retrieve_meta( $post->ID, 'item_author' );
if ( $custom_author ) {
return $custom_author; }
return $author;
} else {
return $author;
}
}
add_filter( 'the_author', 'pf_replace_author_presentation' );
/**
* A function to filter author urls and, if available, replace their display with the origonal item author urls.
*
* @since 3.x
*
* @param string $author_uri The author URI currently in use.
*
* @return string Returns the author URI.
*/
function pf_replace_author_uri_presentation( $author_uri ) {
global $post, $authordata;
if ( is_object( $post ) ) {
$id = $post->ID;
} elseif ( is_numeric( get_the_ID() ) ) {
$id = get_the_ID();
} else {
return $author_uri;
}
if ( 'yes' == get_option( 'pf_present_author_as_primary', 'yes' ) ) {
$custom_author_uri = pressforward( 'controller.metas' )->retrieve_meta( $id, 'item_link' );
if ( ! $custom_author_uri || 0 == $custom_author_uri || empty( $custom_author_uri ) ) {
return $author_uri;
} else {
return $custom_author_uri;
}
} else {
return $author_uri;
}
}
add_filter( 'author_link', 'pf_replace_author_uri_presentation' );
function pf_canonical_url() {
if ( is_single() ) {
$obj = get_queried_object();
$post_ID = $obj->ID;
$link = pressforward( 'controller.metas' )->get_post_pf_meta( $post_ID, 'item_link', true );
if (empty($link)){
return false;
}
return $link;
} else {
return false;
}
}
function pf_filter_canonical( $url ) {
if ( $link = pf_canonical_url() ) {
return $link;
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'pf_filter_canonical' );
add_filter( 'wpseo_opengraph_url', 'pf_filter_canonical' );
add_filter("wds_filter_canonical", 'pf_filter_canonical');
/**
* A function to set up the HEAD data to forward users to origonal articles.
*
* Echos the approprite code to forward users.
*
* @since 3.x
*/
function pf_forward_unto_source() {
if ( ! is_singular() ) {
return false;
}
$link = pf_canonical_url();
if ( ! empty( $link ) && false !== $link ) {
$obj = get_queried_object();
$post_id = $obj->ID;
if ( has_action( 'wpseo_head' ) ) {
} else {
echo '<link rel="canonical" href="' . $link . '" />';
echo '<meta property="og:url" content="' . $link . '" />';
add_filter( 'wds_process_canonical', '__return_false');
}
$wait = get_option( 'pf_link_to_source', 0 );
$post_check = pressforward( 'controller.metas' )->get_post_pf_meta( $post_id, 'pf_forward_to_origin', true );
// var_dump($post_check); die();
if ( isset( $_GET['noforward'] ) && true == $_GET['noforward'] ) {
} else {
if ( ( $wait > 0 ) && ( 'no-forward' !== $post_check ) ) {
echo '<META HTTP-EQUIV="refresh" CONTENT="' . $wait . ';URL=' . $link . '">';
?>
<script type="text/javascript">console.log('You are being redirected to the source item.');</script>
<?php
echo '</head><body></body></html>';
die();
}
}
}
}
add_action( 'wp_head', 'pf_forward_unto_source', 1000 );
/**
* Echos the script link to use phonegap's debugging tools.
*
* @since 3.x
*/
function pf_debug_ipads() {
echo '<script src="http://debug.phonegap.com/target/target-script-min.js#pressforward"></script>';
}
// add_action ('wp_head', 'pf_debug_ipads');
// add_action ('admin_head', 'pf_debug_ipads');
function pf_is_drafted( $item_id ) {
$a = array(
'no_found_rows' => true,
'fields' => 'ids',
'meta_key' => pressforward('controller.metas')->get_key('item_id'),
'meta_value' => $item_id,
'post_type' => get_option( PF_SLUG . '_draft_post_type', 'post' ),
);
$q = new WP_Query( $a );
if ( 0 < $q->post_count ) {
$draft = $q->posts;
return $draft[0];
} else {
return false;
}
}
/**
* Get a list of all drafted items.
*
* @return array
*/
function pf_get_drafted_items( $post_type = 'pf_feed_item' ) {
$drafts = new WP_Query( array(
'no_found_rows' => true,
'post_type' => get_option( PF_SLUG . '_draft_post_type', 'post' ),
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'item_id',
),
),
'update_post_meta_cache' => true,
'update_post_term_cache' => false,
) );
$item_hashes = array();
foreach ( $drafts->posts as $p ) {
$item_hashes[] = pressforward( 'controller.metas' )->get_post_pf_meta( $p->ID, 'item_id', true );
}
$drafted_query = new WP_Query( array(
'no_found_rows' => true,
'post_status' => 'any',
'post_type' => $post_type,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'item_id',
'value' => $item_hashes,
'compare' => 'IN',
),
),
) );
return array_map( 'intval', $drafted_query->posts );
}
function filter_for_pf_archives_only( $sql ) {
global $wpdb;
// if (isset($_GET['pf-see']) && ('archive-only' == $_GET['pf-see'])){
$relate = pressforward( 'schema.relationships' );
$rt = $relate->table_name;
$user_id = get_current_user_id();
$read_id = pf_get_relationship_type_id( 'archive' );
/** $sql .= " AND {$wpdb->posts}.ID
IN (
SELECT item_id
FROM {$rt}
WHERE {$rt}.user_id = {$user_id}
AND {$rt}.relationship_type = {$read_id}
AND {$rt}.value = 1
) ";
}
*/ // var_dump($sql);
return $sql;
}
/**
* Filter the Nominated query for the Drafted filter.
*
* @param WP_Query $query WP_Query object.
*/
function pf_filter_nominated_query_for_drafted( $query ) {
global $pagenow;
if ( 'admin.php' !== $pagenow
|| empty( $_GET['page'] )
|| 'pf-review' !== $_GET['page']