-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
class-creativecommons.php
1247 lines (1118 loc) · 39.2 KB
/
class-creativecommons.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
/**
* CC WordPress Plugin: Main Class
*
* @package CC_WordPress_Plugin
* @subpackage Main_Class
*/
class CreativeCommons {
// Make sure the plugin header has the same version number.
const VERSION = '2022.09.1';
/**
* Plugin URL.
*
* @since 2.0
* @var mixed $plugin_url
*/
private $plugin_url;
/**
* Store text-domain. Only used to load text-domain.
* Do not use this variable with strings, use 'CreativeCommons' instead
*
* @since 2.0
* @var mixed $localization_domain
*/
private $localization_domain = 'CreativeCommons';
/**
* To store locale.
*
* @since 2.0
* @var mixed $locale
*/
private $locale;
/**
* Instance
*
* @since 2.0
* @var null $instance
*/
private static $instance = null;
/**
* Constructor
*
* @return void
*/
private function __construct() {
}
/**
* Intializer
*
* @return void
*/
public function init() {
$this->plugin_url = plugin_dir_url( dirname( __FILE__ ) );
// language setup.
$lang_dir = dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/';
load_plugin_textdomain( $this->localization_domain, false, $lang_dir );
/*
* add admin.js to wp-admin pages and displays the site
* license settings in the Settings->General settings page
* unless you're running WordPress Multisite (Network) the
* superadmin has disabled this.
*/
add_action( 'admin_init', array( &$this, 'license_admin_init' ) );
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
// Adds CC License widget to display the license.
add_action( 'widgets_init', array( &$this, 'license_as_widget' ) );
}
/**
* Gets an instance
*
* @return $instance
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Register and add plugin settings.
*/
public function page_init() {
register_setting(
'cc-admin',
'license',
array( &$this, '_wrapper_settings_api_verify' )
);
register_setting(
'cc-admin',
'enable_attribution_box'
);
/**
* This section includes:
* License selector.
* license_current settings field which previews the current/selected license.
*/
add_settings_section(
'license-chooser',
'',
array( &$this, 'settings_license_chooser' ),
'cc-admin'
);
/**
* This section includes:
* Additional attribution text.
* More attribtion settings that will be added in the future.
*/
add_settings_section(
'license-attribution-settings',
'',
array( &$this, 'admin_license_attr_settings' ),
'cc-admin'
);
add_settings_field(
'license_current',
__(
'Current License',
'CreativeCommons'
),
array( &$this, 'settings_preview_current_license' ),
'cc-admin',
'license-chooser',
array( 'label_for' => 'license_current' )
);
add_settings_field(
'additional_attribution_txt',
__(
'Add additional attribution text',
'CreativeCommons'
),
array( &$this, 'setting_additional_text_field' ),
'cc-admin',
'license-attribution-settings',
array( 'label_for' => 'additional_attribution_txt' )
);
add_settings_field(
'attribution_to',
__(
'Attribution Details',
'CreativeCommons'
),
array( &$this, 'setting_attribution_field' ),
'cc-admin',
'license-attribution-settings',
array( 'label_for' => 'attribution_to' )
);
add_settings_field(
'display_as',
__(
'Display license as',
'CreativeCommons'
),
array( &$this, 'display_license_as' ),
'cc-admin',
'license-attribution-settings',
array( 'label_for' => 'display_as' )
);
add_settings_field(
'enable_attribution_box',
__(
'Display attribution information for images',
'CreativeCommons'
),
array( &$this, 'enable_attribution_box' ),
'cc-admin',
'license-attribution-settings',
array( 'label_for' => 'enable_attribution_box' )
);
}
/**
* Html output call-back for license section shown in Settings > Creative Commons.
* Uses radio buttons, and the selected license is stored in the $license
* array as $license['choice'].
*/
public function settings_license_chooser() {
$license = $this->get_license( $location = 'site' ); // Gets license array to store the selection.
?>
<table class="widefat" style="padding: 1.4em; padding-right: 3em;">
<thead>
<tr>
<th>
<h3><?php esc_html_e( 'Select the License', 'CreativeCommons' ); ?></h3>
<p>
<?php esc_html_e( 'Select your required default license for your website. Choose a license from Creative Commons licenses. If you are not sure about what license to use, let our ', 'CreativeCommons' ); ?>
<strong><a href="https://creativecommons.org/choose/" target="blank">
<?php esc_html_e( 'License Chooser', 'CreativeCommons' ); ?>
</a></strong>
<?php esc_html_e( ' help. The selected license can be displayed as a widget. In' ); ?>
<strong>
<?php esc_html_e( 'Appearance > Widgets', 'CreativeCommons' ); ?>
</strong>
<?php esc_html_e( 'drag CC License widget to the required area. You can also include the license in footer. We recommend using the widget for better compatibility with your theme. You can use individual licenses in posts or pages using Gutenberg blocks.', 'CreativeCommons' ); ?>
</p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><label>
<input name="license[choice]" type="radio" value="by" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'by' ) : ''; ?> />
<?php esc_html_e( 'Attribution 4.0 International License', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;" >(CC BY 4.0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/licenses/by/4.0" target="blank" rel="license"><img src="%1$s" alt="CC BY"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/by.png' );
?>
</td>
</tr>
<tr>
<td>
<p class="cc-test-css"><label>
<input name="license[choice]" type="radio" value="by-sa" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'by-sa' ) : ''; ?> /> <?php esc_html_e( 'Attribution-ShareAlike 4.0 International License', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;" >(CC BY-SA 4.0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/licenses/by-sa/4.0" target="blank" rel="license"><img src="%1$s" alt="CC BY-SA"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/by-sa.png' );
?>
</td>
</tr>
<tr>
<td>
<p><label>
<input name="license[choice]" type="radio" value="by-nc" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'by-nc' ) : ''; ?> /> <?php esc_html_e( 'Attribution-NonCommercial 4.0 International License', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;" >(CC BY-NC 4.0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/licenses/by-nc/4.0" target="blank" rel="license"><img src="%1$s" alt="CC BY-NC"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/by-nc.png' );
?>
</td>
</tr>
<tr>
<td>
<p><label>
<input name="license[choice]" type="radio" value="by-nc-sa" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'by-nc-sa' ) : ''; ?> /> <?php esc_html_e( 'Attribution-NonCommercial-ShareAlike 4.0 International License', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;" >(CC BY-NC-SA 4.0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/licenses/by-nc-sa/4.0" target="blank" rel="license"><img src="%1$s" alt="CC BY-NC-SA"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/by-nc-sa.png' );
?>
</td>
</tr>
<tr>
<td>
<p><label>
<input name="license[choice]" type="radio" value="by-nc-nd" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'by-nc-nd' ) : ''; ?> /> <?php esc_html_e( 'Attribution-NonCommercial-NoDerivatives 4.0 International License', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;" >(CC BY-NC-ND 4.0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/licenses/by-nc-nd/4.0" target="blank" rel="license"><img src="%1$s" alt="CC BY-NC-ND"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/by-nc-nd.png' );
?>
</td>
</tr>
<tr>
<td>
<p><label>
<input name="license[choice]" type="radio" value="by-nd" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'by-nd' ) : ''; ?> /> <?php esc_html_e( 'Attribution-NoDerivatives 4.0 International License', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;" >(CC BY-ND 4.0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/licenses/by-nd/4.0" target="blank" rel="license"><img src="%1$s" alt="CC BY-ND"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/by-nd.png' );
?>
</td>
</tr>
<tr>
<td>
<p><label>
<input name="license[choice]" type="radio" value="cc0" <?php isset( $license['choice'] ) ? checked( $license['choice'], 'cc0' ) : ''; ?> /> <?php esc_html_e( 'CC0 Universal Public Domain Dedication license', 'CreativeCommons' ); ?>
<strong style="color:#fc7303;">(CC0)</strong>
</p></label>
</td>
<td>
<?php
printf( '<a href="https://creativecommons.org/publicdomain/zero/1.0/" target="blank" rel="license"><img src="%1$s" alt="CC0"></a>', esc_attr( CCPLUGIN__URL ) . 'includes/images/cc0.png' );
?>
</td>
</tr>
</tbody>
</table>
<?php
}
/**
* Callback for 'license-attribution-settings' section.
* It is empty currently, but is required for the related settings-field to work.
*
* @return void
*/
public function admin_license_attr_settings() {
}
/**
* Function: settings_license_section
*/
public function settings_license_section() {
$this->display_settings_warning(
$echo = true
);
}
/**
* Displays a preview of current selected license. Used in 'license-chooser' settings section.
*/
public function settings_preview_current_license() {
$license = $this->get_license( $location = 'site' );
$deed = esc_html( $license['deed'] );
$name = esc_attr( $license['name'] );
$image = esc_attr( $license['image'] );
// Closing the settings table by </table>. Do not use any more setting fields in license-chooser section.
echo "</table><div style=' text-align: center; background: #fff; border: 1px solid #e5e5e5; box-shadow: 0 1px 1px rgba(0,0,0,.04); padding: 1em;'><img id='cc-current-license-image' src='$image'><br /><a href='$deed' target='blank' rel='license'>$name</a></div>";
}
/**
* Provides an option to the user to add an additional
* attribution text after the license.
*/
public function setting_additional_text_field() {
$license = $this->get_license( $location = 'site' );
$add_txt = esc_html( $license['additional_attribution_txt'] );
echo "<input style='padding:0.5rem;' name='license[additional_attribution_txt]' type='text' size='120' maxlength='300' id='additional-attribution-txt' value='$add_txt' />";
}
/**
* Calls attribution details html callback function.
*
*/
public function setting_attribution_field() {
$this->select_attribute_to_html( $location = 'site', $echo = true );
}
/**
* Display license as a widget or a footer or both.
*/
public function display_license_as() {
$license = $this->get_license( $location = 'site' );
?>
<br />
<input name="license[display_as_widget]" type="checkbox" value="true" id="display_as_widget" <?php checked( $license['display_as_widget'], 'true' ); ?> />
<label for="display_as_widget"><?php esc_html_e( 'Widget', 'CreativeCommons' ); ?>
<i><?php esc_html_e( '(recommended)', 'CreativeCommons' ); ?></i></label>
<br />
<input name="license[display_as_footer]" type="checkbox" value="true" id="display_as_footer" <?php checked( $license['display_as_footer'], 'true' ); ?> />
<label for="display_as_footer"><?php esc_html_e( 'Footer', 'CreativeCommons' ); ?></label>
<br />
<?php
}
public function enable_attribution_box() {
$enabled = get_option("enable_attribution_box");
?>
<input name="enable_attribution_box" type="checkbox" value="true" <?php checked($enabled, 'true'); ?> />
<?php
}
/**
* Check if a site may override the network license.
*
* If a network license override is allowed, a site admin may change
* their site's license. The rights are cascading: if a site admin may
* change a site's license, she may also allow a user to select their
* own license.
* if a site admin may not change the license she will also not be
* allowed to let a user pick their own license. A siteadmin may also
* enable to have a license per content.
*
* @return bool true if the network license override is allowed,
* otherwise false
*/
public function allow_site_override_network_license() {
$license = $this->get_license( $location = 'network' );
/*
* Using true as string instead of bool since it will be a
* string value returned from the settings form
*/
if ( 'true' == $license['site_override_license'] ) {
return true;
} else {
return false;
}
}
/**
* Set a default license.
*
* Hierarchy of license selection
* If the plugin is used in a Multisite Network WordPress setup there is
* an option added to the network options to set a default license for
* all sites in this particular network. Each site will inherit this
* default.
* A sit owner may change this license (at least if the Multisite Admin
* allows it). On a site level (or single WordPress install) an admin
* may allow this site default license to be changed. If the site allows
* a user may change the default license for her/his posts. The same
* goes for posts/pages: if the site admin allows it these can be
* changed per post or page.
**/
public function plugin_default_license() {
$this->_logger( 'Got default settings' );
$license = array(
'deed' => '',
'image' => '',
'attribute_to' => '',
'title' => '',
'title_url' => '',
'author' => '',
'author_url' => '',
'name' => '',
'sitename' => '',
'siteurl' => '',
'site_override_license' => false,
'user_override_license' => false,
'content_override_license' => false,
'version' => '',
'additional_attribution_txt' => '',
'choice' => '',
'display_as_widget' => false,
'display_as_footer' => false,
);
return $license;
}
/**
* Content for a license. No license found or not allowed? Continue 2
* 1) Check if it's allowed to have a license per content => check the
* 2) Check if it's allowed to have a license per user => check the
* content author and grab his/her license preference. No license found
* 3) Check if the site is part of a network => No? continue step 4a
* or user are not allowed to choose their own license? Continue 3
* 4a) the site is NOT part of a network => Get license from options or
* Yes? continue step 4b
* 4b) The site is part of a network => check if the site may choose its
* return plugin's default
* license found Continue step 5
* own license => check the options for a license. Not allowed or no
* return plugin default
* 5) Check the multisite options for a license and return if not found
*
* @param mixed $location null.
*/
public function get_license( $location = null ) {
switch ( $location ) {
case 'network':
$this->_logger( 'called network' );
$license = ( $network_license = get_site_option( 'license' ) )
? $network_license : $this->plugin_default_license();
break;
case 'site':
$this->_logger( 'called site' );
if ( is_multisite() ) {
$this->_logger( 'multisite, check network settings' );
$license = ( $site_license = get_option( 'license' ) )
? $site_license : $this->get_license( 'network' );
} else {
$this->_logger( 'single site, get site license or else default settings' );
$license = ( $site_license = get_option( 'license' ) )
? $site_license : $this->plugin_default_license();
}
break;
case 'profile':
$this->_logger( 'called profile' );
$license = ( $user_license = get_user_option( 'license' ) )
? $user_license : $this->get_license( 'site' );
break;
case 'post-page':
$this->_logger( 'called post-page' );
$license = ( $post_page_license = $this->get_post_page_license() )
? $post_page_license : $this->get_license( 'profile' );
break;
// TODO: need to check default structure below
// since this can cause way too many calls for the right license.
case 'frontend':
$this->_logger( 'get license for the frontend' );
$license = $this->get_license( 'site' );
if ( array_key_exists( 'user_override_license', $license )
&& 'true' == $license['user_override_license']
) {
$license = $this->get_license( 'profile' );
}
if ( array_key_exists( 'content_override_license', $license )
&& 'true' == $license['content_override_license']
) {
$license = $this->get_license( 'post-page' );
}
break;
}
return $license;
}
/**
* Function: get_post_page_license
*
* It will use a variable prefix with underscore to make really
* sure this postmeta value will NOT be displayed to other users. The option is
* the same structure as everywhere: a serialized array.
*
* (http://codex.wordpress.org/Function_Reference/add_post_meta)
*/
public function get_post_page_license() {
// TODO check if this can be done without a global.
global $post;
$license = get_post_meta( $post->ID, '_license', true );
if ( is_array( $license ) && count( $license ) > 0 ) {
return $license;
} else {
return false;
}
}
/**
* Function: get_attribution_options
*
* Returns default attribution options
*
* @param mixed $location
*
* @return $attribution_options
*/
public function get_attribution_options( $location ) {
switch ( $location ) {
case 'network':
$attribution_options = array(
'network_name' => sprintf(
__( 'The network name: %s', 'CreativeCommons' ),
get_site_option( 'site_name' )
),
'site_name' => __(
"A site's name",
'CreativeCommons'
),
'display_name' => __(
'The author display name',
'CreativeCommons'
),
'other' => __(
'Something completely differrent',
'CreativeCommons'
),
);
break;
case 'site':
$attribution_options = array(
'site_name' => sprintf(
__( 'The site name: %s', 'CreativeCommons' ),
get_bloginfo( 'site' )
),
'display_name' => __(
'The author display name',
'CreativeCommons'
),
'other' => __(
'Something completely differrent',
'CreativeCommons'
),
);
break;
default:
break;
}
return $attribution_options;
}
/**
* Funciton: select_attribute_to_html
*
* @param mixed $location null.
* @param mixed $echo true.
*/
public function select_attribute_to_html( $location = null, $echo = true ) {
$license = $this->get_license( $location = 'site' );
$title = ( isset( $license['title'] ) ) ? esc_html( $license['title'] ) : '';
$title_url = ( isset( $license['title_url'] ) ) ? esc_html( $license['title_url'] ) : '';
$author = ( isset( $license['author'] ) ) ? esc_html( $license['author'] ) : '';
$author_url = ( isset( $license['author_url'] ) ) ? esc_html( $license['author_url'] ) : '';
?>
<table class="widefat" style="background:none; width:0%; border:none; box-shadow:none;">
<tr>
<td style="padding:10px 10px;">
<span>
<?php esc_html_e( 'Title', 'CreativeCommons' ); ?>
</span>
</td>
<td style="padding:10px 10px;">
<?php
printf( '<input type="text" name="license[title]" value="%1$s" id="title" class="large" size="45" /><br />', esc_attr( $title ) );
?>
</td>
</tr>
<tr>
<td style="padding:10px 10px;">
<span>
<?php esc_html_e( 'Title URL', 'CreativeCommons' ); ?>
</span>
</td>
<td style="padding:10px 10px;">
<?php printf( '<input type="text" name="license[title_url]" value="%1$s" id="title_url" class="large" size="45" /><br />', esc_attr( $title_url ) ); ?>
</td>
</tr>
<tr>
<td style="padding:10px 10px;">
<span>
<?php esc_html_e( 'Author', 'CreativeCommons' ); ?>
</span>
</td>
<td style="padding:10px 10px;">
<?php printf( '<input type="text" name="license[author]" value="%1$s" id="author" class="large" size="45" /><br />', esc_attr( $author ) ); ?>
</td>
</tr>
<tr>
<td style="padding:10px 10px;">
<span>
<?php esc_html_e( 'Author URL', 'CreativeCommons' ); ?>
</span>
</td>
<td style="padding:10px 10px;">
<?php printf( '<input type="text" name="license[author_url]" value="%1$s" id="author_url" class="large" size="45" /><br />', esc_attr( $author_url ) ); ?>
</td>
</tr>
</table>
<?php
}
/**
* Function: display_settings_warning
*
* Adds a warning text at the site/network settings.
*
* @param mixed $echo false.
*/
public function display_settings_warning( $echo = false ) {
$html = '';
$html .= '<p>';
$html .= __( '', 'CreativeCommons' );
$html .= '</p>';
if ( $echo ) {
echo $html;
} else {
return $html;
}
}
/**
* Function: save_license
*
* Saves license from network settings, user profile and post/page interface.
*
* @param mixed $post_id false.
*/
public function save_license( $post_id = false ) {
if ( filter_has_var( INPUT_POST, 'license_wpnonce' )
&& wp_verify_nonce(
filter_input( INPUT_POST, 'license_wpnonce' ),
'license-update'
)
) {
if ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE ) {
$this->_save_user_license();
} elseif ( is_multisite()
&& defined( 'WP_NETWORK_ADMIN' )
&& WP_NETWORK_ADMIN
) {
$this->_save_network_license();
} else {
// Assume we're in a post or page wp-admin environment might need to deal with autosave.
$this->_save_post_page_license( $post_id );
}
} else {
return $post_id;
}
}
/**
* Function: _wrapper_settings_api_verify
*
* Wrapper so that we can use the _verify_license_data function.
* TODO: refactor in the future
*
* @param mixed $data license data.
*/
public function _wrapper_settings_api_verify( $data ) {
return $this->_verify_license_data( 'site', $data );
}
/**
* Checks the data before saving it
* You must include any addition in the $license array in this
* function for it to be saved.
*
* @param mixed $from Data from Network or Site.
* @param mixed $data Array of license data.
*/
private function _verify_license_data( $from, $data = null ) {
$license = array();
// if no data was provided assume the data is in $_POST['license'].
if ( is_null( $data )
&& isset( $_POST['license'] )
) {
$data = sanitize_text_field( wp_unslash( $_POST['license'] ) );
}
// Saves the license attribution information. MAke sure to save the current version.
$license['version'] = self::VERSION;
$license['attribute_to'] = ( isset( $data['attribute_to'] ) ) ? esc_attr( $data['attribute_to'] ) : '';
$license['attribute_other'] = ( isset( $data['attribute_other'] ) ) ? esc_html( $data['attribute_other'] ) : '';
$license['attribute_other_url'] = ( isset( $data['attribute_other_url'] ) ) ? esc_html( $data['attribute_other_url'] ) : '';
$license['choice'] = ( isset( $data['choice'] ) ) ? esc_attr( $data['choice'] ) : '';
$license['display_as_widget'] = ( isset( $data['display_as_widget'] ) ) ? esc_html( $data['display_as_widget'] ) : '';
$license['display_as_footer'] = ( isset( $data['display_as_footer'] ) ) ? esc_html( $data['display_as_footer'] ) : '';
// Gets the name, deed(url) and icon of the selected license and stores/saves it.
switch ( $data['choice'] ) {
case 'by':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/by.png' );
$license['name'] = esc_attr( 'Creative Commons Attribution 4.0 International' );
$license['deed'] = esc_url( 'http://creativecommons.org/licenses/by/4.0/' );
break;
case 'by-sa':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/by-sa.png' );
$license['name'] = esc_attr( 'Creative Commons Attribution-ShareAlike 4.0 International' );
$license['deed'] = esc_url( 'http://creativecommons.org/licenses/by-sa/4.0/' );
break;
case 'by-nc':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/by-nc.png' );
$license['name'] = esc_attr( 'Creative Commons Attribution-NonCommercial 4.0 International' );
$license['deed'] = esc_url( 'https://creativecommons.org/licenses/by-nc/4.0' );
break;
case 'by-nc-sa':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/by-nc-sa.png' );
$license['name'] = esc_attr( 'Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International' );
$license['deed'] = esc_url( 'https://creativecommons.org/licenses/by-nc-sa/4.0' );
break;
case 'by-nc-nd':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/by-nc-nd.png' );
$license['name'] = esc_attr( 'Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International' );
$license['deed'] = esc_url( 'https://creativecommons.org/licenses/by-nc-nd/4.0' );
break;
case 'by-nd':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/by-nd.png' );
$license['name'] = esc_attr( 'Creative Commons Attribution-NoDerivatives 4.0 International' );
$license['deed'] = esc_url( 'https://creativecommons.org/licenses/by-nd/4.0' );
break;
case 'cc0':
$license['image'] = esc_attr( CCPLUGIN__URL . 'includes/images/cc0.png' );
$license['name'] = esc_attr( 'Creative Commons CC0 Universal Public Domain Dedication' );
$license['deed'] = esc_url( 'https://creativecommons.org/publicdomain/zero/1.0/' );
break;
}
switch ( $from ) {
case 'network':
if ( filter_has_var( INPUT_POST, 'site_override_license' ) ) {
$license['site_override_license'] = esc_attr(
filter_input(
INPUT_POST,
'site_override_license'
)
);
}
break;
case 'site':
$license['user_override_license'] = ( isset( $data['user_override_license'] ) ) ? esc_attr( $data['user_override_license'] ) : '';
$license['content_override_license'] = ( isset( $data['user_override_license'] ) ) ? esc_attr( $data['user_override_license'] ) : '';
$license['additional_attribution_txt'] = ( isset( $data['additional_attribution_txt'] ) ) ? esc_html( $data['additional_attribution_txt'] ) : '';
$license['title'] = ( isset( $data['title'] ) ) ? esc_attr( $data['title'] ) : '';
$license['title_url'] = ( isset( $data['title_url'] ) ) ? esc_url( $data['title_url'] ) : '';
$license['author'] = ( isset( $data['author'] ) ) ? esc_attr( $data['author'] ) : '';
$license['author_url'] = ( isset( $data['author_url'] ) ) ? esc_url( $data['author_url'] ) : '';
break;
}
return $license;
}
/**
* Function: _save_network_license
*
* Validates & verifies the data and then saves the license in the site_options.
*/
private function _save_network_license() {
$license = $this->_verify_license_data( $from = 'network' );
return update_site_option( 'license', $license );
}
/**
* Function: _save_user_license
*
* TODO: need to decide if the user option is global for all sites/blogs in a network.
* Also need to make sure that we're using
* the current profile user_id which might not be the
* current_user (e.g. admin changing license for a user).
* validates & verifies the data and then saves the license in the
* user_options
*/
private function _save_user_license() {
$license = $this->_verify_license_data( $from = 'profile' );
$user_id = get_current_user_id();
return update_user_option(
$user_id,
'license',
$license,
$global = false
);
}
/**
* Function: _save_post_page_license
*
* Save post/page metadata due to it being an array it should not be
* shown in the post or page custom fields interface using _license to hide it from the custom fields
*
* @param mixed $post_id .
*/
private function _save_post_page_license( $post_id ) {
$license = $this->_verify_license_data( $from = 'post-page' );
return update_post_meta( $post_id, '_license', $license );
}
/**
* Function: license_admin_init
*
* @return void
*/
public function license_admin_init() {
// Register our script.
wp_register_script( 'license', $this->plugin_url . '/js/admin.js' );
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
wp_enqueue_script( 'license' );
}
/**
* Add options page, this page will be under "Settings"
*/
public function add_plugin_page() {
add_options_page(
'Settings Admin',
'Creative Commons',
'manage_options',
'cc-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page() {
// Set class property.
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<h2>Creative Commons licenses</h2>
<br />
<div style="background: white; border: 1px solid #e5e5e5; box-shadow: 0 1px 1px rgba(0,0,0,.04); padding: 2em; display: inline-table;">
<?php
printf( '<img src="%1$s" align="right" style="padding: 1em; width: 20%%; height: auto !important;" />', esc_attr( CCPLUGIN__URL ) . 'assets/icon-256x256.png' );
?>
<h3>About Creative Commons</h3>
<p><a href="https://creativecommons.org"
target="_blank">Creative Commons</a>
<?php
esc_html_e(
'is a
nonprofit organization that enables the sharing and use of
creativity and knowledge through free legal tools.',
'CreativeCommons'
);
?>
</p>
<p>
<?php
esc_html_e(
'Our free, easy-to-use copyright licenses
provide a simple, standardized way to give the public
permission to share and use your creative work — on
conditions of your choice. CC licenses let you easily
change your copyright terms from the default of "all rights
reserved" to "some rights reserved."',
'CreativeCommons'
);
?>
</p>
<p>
<?php
esc_html_e(
'Creative Commons licenses are not an
alternative to copyright. They work alongside copyright and
enable you to modify your copyright terms to best suit your
needs.',
'CreativeCommons'
);
?>
</p>
<p><?php esc_html_e( 'Please consider making a ', 'CreativeCommons' ); ?><a href="https://donate.creativecommons.org" target="_blank"><?php esc_html_e( 'donation (tax deductible in the US) to support our work', 'CreativeCommons' ); ?></a>.</p>
<h4><?php esc_html_e( 'Sign up for our newsletter', 'CreativeCommons' ); ?></h4>
<form id="Edit" target="_blank" action="https://donate.creativecommons.org/civicrm/profile/create?gid=30&reset=1" method="post" name="Edit">
<p><input id="email-Primary" class="form-control input-lg" maxlength="64" name="email-Primary" size="30" autofocus placeholder="example@example.com" type="email" required></p>
<p><input class="btn btn-success btn-block" id="_qf_Edit_next" accesskey="S" name="_qf_Edit_next" type="submit" value="Subscribe"><input name="postURL" type="hidden" value="https://creativecommons.org/thank-you"><input name="cancelURL" type="hidden" value="https://creativecommons.org/newsletter"><input name="group[121]" type="hidden" value="1"><input name="_qf_default" type="hidden" value="Edit:cancel"></p>
</form>
</div>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields.
settings_fields( 'cc-admin' );
do_settings_sections( 'cc-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys.
*/
public function sanitize( $input ) {
$new_input = array();
if ( isset( $input['id_number'] ) ) {