forked from creativecommons/wp-plugin-creativecommons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wplicense.php
738 lines (616 loc) · 32.1 KB
/
wplicense.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
<?php
/*
Plugin Name: Creative Commons
Description: Official Creative Commons plugin for WordPress. Allows users to select and display Creative Commons licenses for their content. Partially inspired by the License plugin by mitcho (Michael Yoshitaka Erlewine) and Brett Mellor, as well as the original WpLicense plugin by CC CTO Nathan R. Yergler.
Version: 2.0
Author: Bjorn Wijers <burobjorn@burobjorn.nl>, Tarmo Toikkanen <tarmo@iki.fi>, Matt Lee <mattl@creativecommons.org>
Plugin URI: http://wiki.creativecommons.org/WpLicense
License: GPLv2 or later versions
*/
if( ! class_exists('WPLicense') ) {
class WPLicense {
// MAKE SURE THE PLUGIN HEADER HAS THE SAME VERSION NUMBER!
const VERSION = '2.0-beta';
private $plugin_url;
private $localization_domain = 'WPLicense';
private $locale;
function __construct() {
$this->plugin_url = WP_PLUGIN_URL .'/' . str_replace( basename( __FILE__ ), "", plugin_basename( __FILE__ ) );
// language setup
$this->locale = get_locale();
$mo = dirname(__FILE__) . '/languages/' . $this->locale . '.mo';
load_textdomain($this->localization_domain, $mo);
// 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) and the superadmin has disabled this.
add_action( 'admin_init', array(&$this, 'license_admin_init') );
// Selecting a license for individual posts or pages is only possible if the settings of the site allow it
// by default it does allow it.
if( $this->allow_content_override_site_license() ) {
add_action( 'post_submitbox_misc_actions', array(&$this, 'post_page_license_settings_html') );
add_action( 'page_submitbox_misc_actions', array(&$this, 'post_page_license_settings_html') );
add_action( 'save_post', array(&$this, 'save_license') );
}
// Selecting a license as a user for all your content is only possible if the settings of the site allow it,
// by default it will allow it.
if( $this->allow_user_override_site_license() ) {
add_action( 'personal_options', array(&$this, 'user_license_settings_html') );
add_action( 'personal_options_update', array(&$this, 'save_license') );
}
// this implements the license plugin as a widget.
// TODO: Widget needs more testing with the new approach
add_action( 'widgets_init', array(&$this, 'license_as_widget') );
// if the plugin is installed in multisite environment allow to set the
// options for all sites as default from the network options
if( is_multisite() ) {
add_action('wpmu_options', array(&$this, 'network_license_settings_html') , 10, 0);
add_action('update_wpmu_options', array(&$this, 'save_license'), 10, 0 );
}
}
function register_site_settings() {
register_setting( 'general', 'license', array(&$this, '_wrapper_settings_api_verify') );
add_settings_section( 'license-section', 'License Settings', array(&$this, 'settings_license_section'), 'general', 'license-section');
add_settings_field( 'license', '<label for="license">' . __('License your site', $this->localization_domain) . '</label>', array(&$this, 'setting_license_field'), 'general', 'license-section');
add_settings_field( 'warning_txt', '<label for="warning_txt">' . __('Add license warning text', $this->localization_domain) . '</label>', array(&$this, 'setting_warning_field'), 'general', 'license-section');
add_settings_field( 'attribution_to', '<label for="attribution_to">' . __('Set attribution to', $this->localization_domain) . '</label>', array(&$this, 'setting_attribution_field'), 'general', 'license-section');
add_settings_field( 'allow_user_override', '<label for="allow_user_override">' . __('Allow users override site license', $this->localization_domain) . '</label>', array(&$this, 'setting_user_override_license_field'), 'general', 'license-section');
add_settings_field( 'allow_content_override', '<label for="allow_content_override">' . __('Allow license per post or page', $this->localization_domain) . '</label>', array(&$this, 'setting_content_override_license_field'), 'general', 'license-section');
}
function settings_license_section() {
$this->display_settings_warning( $echo = true );
}
function setting_license_field() {
$this->select_license_html( $location = 'site', $echo = true );
}
function setting_warning_field() {
$license = $this->get_license( $location = 'site');
$w = esc_html( $license['warning_txt'] );
echo "<input name='license[warning_txt]' type='text' maxlength='250' id='warning-txt' value='$w' />";
}
function setting_attribution_field() {
$this->select_attribute_to_html( $location = 'site', $echo = true );
}
// only used once in site admin
function setting_user_override_license_field() {
$license = $this->get_license( $location = 'site');
$checked = ( array_key_exists('user_override_license', $license) ) ? checked( $license['user_override_license'], 'true', false ) : '';
echo "<input name='license[user_override_license]' type='checkbox' " . $checked . " id='user-override-license' value='true' />";
}
// only used once in site admin
function setting_content_override_license_field() {
$license = $this->get_license( $location = 'site');
$checked = ( array_key_exists('content_override_license', $license) ) ? checked( $license['content_override_license'], 'true', false ) : '';
echo "<input name='license[content_override_license]' type='checkbox' " . $checked . " id='content-override-license' value='true' />";
}
/**
* 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, false
* otherwise
*/
function allow_site_override_network_license() {
$license = $this->get_license( $location = 'network' );
// gotcha: 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;
}
}
function allow_user_override_site_license() {
if( is_multisite() && ! $this->allow_site_override_network_license() ) {
return false;
} else {
$license = $this->get_license ( $location = 'site' );
if( array_key_exists('user_override_license', $license) && 'true' == $license['user_override_license'] ) {
return true;
} else {
return false;
}
}
}
function allow_content_override_site_license() {
if( is_multisite() && ! $this->allow_site_override_network_license() ) {
return false;
} else {
$license = $this->get_license ( $location = 'site' );
if( array_key_exists('content_override_license', $license) && 'true' == $license['content_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.
*
*
**/
function plugin_default_license() {
$this->_logger('Got default settings');
return $license = array(
'deed' => 'http://creativecommons.org/licenses/by-sa/4.0/',
'image' => 'http://i.creativecommons.org/l/by-sa/4.0/88x31.png',
'attribute_to' => '',
'title' => get_bloginfo('name'),
'name' => 'Creative Commons Attribution-Share Alike 4.0 License',
'sitename' => get_bloginfo(''),
'siteurl' => get_bloginfo('url'),
'author' => get_bloginfo(),
'site_override_license' => true,
'user_override_license' => true,
'content_override_license' => true,
'version' => self::VERSION,
'warning_txt' => __('The license shown may be overriden by individual content such as a single post or image.', $this->localization_domain)
);
}
// 1) Check if it's allowed to have a license per content => check the
// content for a license. No license found or not allowed? Continue 2
// 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
// or user are not allowed to choose their own license? Continue 3
// 3) Check if the site is part of a network => No? continue step 4a
// Yes? continue step 4b
// 4a) the site is NOT part of a network => Get license from options or
// return plugin's default
// 4b) The site is part of a network => check if the site may choose it's
// own license => check the options for a license. Not allowed or no
// license found Continue step 5
// 5) Check the multisite options for a license and return if not found
// return plugin default
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');
if( is_multisite() ) {
$this->_logger('get license: multisite');
$license = $this->get_license( 'network' );
$this->_logger('got network license');
if( $this->allow_site_override_network_license() ) {
$this->_logger('site may override network license');
$license = $this->get_license( 'site' );
$site_license = $license; // keep track of site license cause we need to check it twice...
$this->_logger('got site license');
if( array_key_exists( 'user_override_license', $site_license ) && 'true' == $site_license['user_override_license'] ) {
$this->_logger('user may override license');
$license = $this->get_license( 'profile' );
$this->_logger('got user license');
}
if( array_key_exists('content_override_license', $site_license) && 'true' == $site_license['content_override_license'] ) {
$this->_logger('content may override license');
$license = $this->get_license( 'post-page' );
$this->_logger('got content license');
}
}
} else {
$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;
}
// 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)
function get_post_page_license() {
global $post; // TODO check if this can be done without a global and if we're always getting what we want
$license = get_post_meta( $post->ID, '_license', true );
if( is_array($license) && sizeof($license) > 0 ) {
return $license;
} else {
return false;
}
}
// used in:
// - network settings
// - site settings
// - profile settings (personal & others)
// - post/page edit screen (my own & others)
function select_license_html( $location = null, $echo = true ) {
// get the previously selected license from this site's options or the plugin's default license
//$license = get_option('license', $this->plugin_default_license() );
$license = $this->get_license( $location );
// add lang option
$lang = ( isset($this->locale) && ! empty($this->locale) ) ? 'lang=' . esc_attr($this->locale) : '';
$html = '';
$html .= "<span id='license-display'></span>";
$html .= '<br id="license"><a title="' . __('Choose a Creative Commons license', $this->localization_domain) . '" class="thickbox edit-license" href="http://creativecommons.org/choose/?';
$html .= 'partner=WordPress+License+Plugin&';
$html .= $lang;
$html .= '&exit_url=' . $this->plugin_url . 'licensereturn.php?url=[license_url]%26name=[license_name]%26button=[license_button]%26deed=[deed_url]&';
$html .= '&KeepThis=true&TB_iframe=true&height=500&width=600">' . __('Change license', $this->localization_domain);
$html .= '</a>';
$html .= '<input type="hidden" value="'.$license['deed'].'" id="hidden-license-deed" name="license[deed]"/>';
$html .= '<input type="hidden" value="'.$license['image'].'" id="hidden-license-image" name="license[image]"/>';
$html .= '<input type="hidden" value="'.$license['name'].'" id="hidden-license-name" name="license[name]"/>';
if( $echo ) {
echo $html;
} else {
return $html;
}
}
// return default attribution options
function get_attribution_options( $location ) {
switch ( $location ) {
case 'network':
$attribution_options = array(
'network_name' => sprintf( __('The network name: %s', $this->localization_domain), get_site_option('site_name') ),
'site_name' => __("A site's name", $this->localization_domain),
'display_name' => __('The author display name', $this->localization_domain),
'other' => __('Something completely differrent', $this->localization_domain)
);
break;
case 'site':
$attribution_options = array(
'site_name' => sprintf( __('The site name: %s', $this->localization_domain), get_bloginfo('site') ),
'display_name' => __('The author display name', $this->localization_domain),
'other' => __('Something completely differrent', $this->localization_domain)
);
break;
default:
if( $this->allow_user_override_site_license() ) {
$attribution_options = array(
'display_name' => __('The author display name', $this->localization_domain),
'other' => __('Something completely differrent', $this->localization_domain)
);
}
break;
}
return $attribution_options;
}
function select_attribute_to_html( $location = null, $echo = true ) {
$license = $this->get_license( $location );
$attribute_options = $this->get_attribution_options( $location );
$html = '';
if( is_array($attribute_options) && sizeof($attribute_options) > 0 ) {
foreach( $attribute_options as $attr_val => $attr_text) {
$checked = '';
$id = "license-$attr_val";
$checked = checked($license['attribute_to'], $attr_val, false);
$html .= "<label for='$id'><input type='radio' class='license-attribution-options' id='$id' $checked name='license[attribute_to]' value='$attr_val' />" . $attr_text . "</label><br/>\n";
}
if ( 'other' == $license['attribute_to'] ) {
$class = '';
$value = esc_html( $license['attribute_other'] );
$url = esc_url ( $license['attribute_other_url']);
} else {
$class = 'hidden'; // hide the other input text field if the 'other' option is not ticked
$value = '';
$url = '';
}
$html .= "<p id='attribute-other-data' class='$class'>";
$html .= "<label for='license-other-value'>". __('Attribution text:', $this->localization_domain) . "<br /><input type='text' value='$value' name='license[attribute_other]' id='license-other-value' class='large' size='45' /></label><br />"; //might need max length? TODO need to check hidden value if other prev was selected
$html .= "<label for='license-other-value-url'>". __('Attribution url:', $this->localization_domain) . "<br /><input type='text' value='$url' name='license[attribute_other_url]' id='license-other-value-url' class='large' size='45' /></label>";
$html .= '</p>';
}
if( $echo ) {
echo $html;
} else {
return $html;
}
}
// add a warning text at the site/network settings
function display_settings_warning( $echo = false ) {
$html = '';
$html .= '<p>';
$html .= __('WARNING: Changing these license settings after content has been added may change the licenses authors on the site have selected, effectively relicensing possibly all content on the site!', $this->localization_domain);
$html .= '</p>';
if( $echo ) {
echo $html;
} else {
return $html;
}
}
/**
* Renders the license settings WordPress Network Settings page
*
* Using the settings rendered by this function a superadmin may set a
* default license for the WordPress Network. This will be used for all
* sites. If the superadmin allows it, siteadmins may change their site's
* license and choose a different license than the default Network license.
*
* Called by wpmu_options action
*
**/
function network_license_settings_html() {
// get the previously selected license from the network options or the plugin's default license
//$license = get_site_option('license', $this->plugin_default_license() );
$location = 'network';
$license = $this->get_license( $location );
$html = '';
$html .= "<h3>" . __('License settings', $this->localization_domain) . "</h3>\n";
$html .= $this->display_settings_warning();
$html .= wp_nonce_field('license-update', $name = 'license_wpnonce', $referer = true, $echo = false);
$html .= "<table class='form-table'>\n";
$html .= "<tbody>\n";
$html .= $this->_license_settings_html( $location );
$html .= "<tr valign='top'>\n";
$html .= "\t<th scope='row'><label for='override-license'>" . __("Allow siteadmins to change their site's license", $this->localization_domain) . "</label></th>\n";
$html .= "\t<td><input name='site_override_license' type='checkbox'" . checked( $license['site_override_license'], 'true', false ) . " id='site_override-license' value='true' />";
$html .= "</td>\n";
$html .= "</tr>\n";
$html .= "</tbody>\n";
$html .= "</table>\n";
echo $html;
}
// save license from network settings, user profile and post/page interface
function save_license( $post_id = false ) {
if( isset($_POST['license_wpnonce']) && wp_verify_nonce( $_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 {
// presume 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;
}
}
// wrapper so I can use the _verify_license_data function.
// @TODO refactor in the future
public function _wrapper_settings_api_verify( $data ) {
return $this->_verify_license_data( 'site', $data );
}
// check the data before saving it
// use $from ala get_license to determine where the data is coming from
// and what should be mandatory return an array or WP_Error?
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 = $_POST['license'];
}
$license['version'] = self::VERSION; // always save the current version
$license['deed'] = esc_url( $data['deed'] );
$license['image'] = esc_url( $data['image'] );
$license['name'] = esc_attr( $data['name'] );
$license['attribute_to'] = esc_attr( $data['attribute_to'] );
$license['attribute_other'] = esc_html( $data['attribute_other' ] );
$license['attribute_other_url'] = esc_html( $data['attribute_other_url' ] );
switch( $from ) {
case 'network': // @TODO need to check this!
if( isset( $_POST['site_override_license'] ) ) {
$license['site_override_license'] = esc_attr( $_POST['site_override_license'] );
}
break;
case 'site':
$license['user_override_license'] = esc_attr( $data['user_override_license'] );
$license['content_override_license'] = esc_attr( $data['content_override_license'] );
$license['warning_txt'] = esc_html( $data['warning_txt'] );
break;
}
return $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);
}
// 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 );
}
// 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
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() {
/* Register our script. */
wp_register_script('license', WP_PLUGIN_URL . '/wplicense/js/admin.js');
wp_enqueue_script("thickbox");
wp_enqueue_style("thickbox");
wp_enqueue_script('license');
// if a siteadmin may change her site's license, show the settings
// otherwise don't bother
if( is_multisite() ) {
if( $this->allow_site_override_network_license() ) {
$this->register_site_settings();
}
} else {
$this->register_site_settings();
}
}
// render the html settings for the user profile
// TODO: add global option: if allowed use this license across all my sites
// in this network.
function user_license_settings_html(){
$location = 'profile';
$html = wp_nonce_field('license-update', $name = 'license_wpnonce', $referer = true, $echo = false);
$html .= $this->_license_settings_html( $location);
echo $html;
}
function post_page_license_settings_html(){
$location = 'post-page';
$html = '<div id="license" class="misc-pub-section misc-pub-section-last ">';
$html .= wp_nonce_field('license-update', $name = 'license_wpnonce', $referer = true, $echo = false);
$html .= '<strong>' . __('Licensed:', $this->localization_domain) . '</strong>';
$html .= '<p>';
$html .= $this->select_license_html( $location, $echo = false );
$html .= '</p>';
$html .= '<p>';
$html .= $this->select_attribute_to_html( $location, $echo = false );
$html .= '</p>';
$html .= '</div>';
echo $html;
}
// DRY wrapper function used for profile settings and network settings
private function _license_settings_html( $location ){
$html ='';
$html .= "<tr valign='top'>\n";
$html .= "\t<th scope='row'><label for='license'>" . __('Select a default license', $this->localization_domain) . "</label></th>\n";
$html .= "\t<td>";
$html .= $this->select_license_html( $location, $echo = false );
$html .= "</td>\n";
$html .= "</tr>\n";
$html .= "<tr valign='top'>\n";
$html .= "\t<th scope='row'><label for='attribute_to'>" . __("Set attribution to", $this->localization_domain) . "</label></th>\n";
$html .= "\t<td>";
$html .= $this->select_attribute_to_html( $location, $echo = false );
$html .= "</td>\n";
$html .= "</tr>\n";
return $html;
}
// add filters to this function so themers can easily change the html
// output
public function print_license_html( $location = 'frontend', $echo = true ) {
// TODO if the license is shown on a multiple items page (except the author
// archive page?) display the site (or network default license)
// default license including a warning that individual items may be
// licensed differently.
// add a filter to include the license with the excerpt & content filter
// allow an option to switch this off
$license = $this->get_license( $location );
$html = '';
if( is_array($license) && sizeof($license) > 0 ) {
$deed_url = esc_url( $license['deed'] );
$image_url = esc_url( $license['image'] );
$license_name = esc_html( $license['name'] );
$warning = (array_key_exists( 'warning_txt', $license ) ) ? esc_html( $license['warning_txt'] ) : ''; // needs check
if( is_array($attribution = $this->_get_attribution( $license ) ) ) {
$attribute_text = isset( $attribution['text'] ) ? $attribution['text'] : '';
$attribute_url = isset( $attribution['url'] ) ? $attribution['url'] : '';
}
// it's a single entity so use the post->title otherwise use the site's
// title and add a warning that multiple items might have different
// licenses.
if( is_singular() ) {
global $post;
if( is_object( $post ) ) {
$title_work = esc_html( $post->post_title );
$warning_text = '';
}
} else {
$title_work = get_bloginfo( 'name' );
$attribute_url = esc_html(site_url());
$warning_text = "<p class='license-warning'>" . esc_html( $warning ) . "</p>";
}
$html .= "<div class='license-wrap'>";
$html .= "<a rel='license' href='$deed_url'>";
$html .= "<img alt='" . __('Creative Commons License', $this->localization_domain) . "' style='border-width:0' src='$image_url' />";
$html .= "</a><br />";
$html .= "<span xmlns:dct='http://purl.org/dc/terms/' property='dct:title'>$title_work</span> ";
if (is_singular()) {
$html .= __('by', $this->localization_domain);
$html .= " <a xmlns:cc='http://creativecommons.org/ns#' href='$attribute_url' property='cc:attributionName' rel='cc:attributionURL'>$attribute_text</a> ";
}
$html .= sprintf( __('is licensed under a <a rel="license" href="%s">%s</a>.', $this->localization_domain), $deed_url, $license_name );
//$html .= '<br />';
//$html .= __('Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="http://source.url" rel="dct:source">http://source.url</a>.', $this->localization_domain);
//$html .='<br />';
//$html .= __('Permissions beyond the scope of this license may be available at <a xmlns:cc="http://creativecommons.org/ns#" href="http://morepermissions.url" rel="cc:morePermissions">http://morepermissions.url</a>.', $this->localization_domain);
$html .= $warning_text;
$html .= '</div>';
}
if( $echo ) {
echo $html;
} else {
return $html;
}
}
// log all errors if wp_debug is active
private function _logger( $string ) {
if( defined('WP_DEBUG') && (WP_DEBUG == true) ) {
error_log( $string );
} else {
return;
}
}
private function _get_attribution( $license ) {
if( is_array($license) && sizeof( $license ) > 0 ){
$attribution_option = isset( $license['attribute_to'] ) ? $license['attribute_to'] : null;
}
$attribution = array();
switch($attribution_option) {
case 'network_name':
$attribution['text'] = esc_html( get_site_option('site_name') );
$attribution['url'] = esc_url( get_permalink() );
break;
case 'site_name':
$attribution['text'] = esc_html( get_bloginfo('site') );
$attribution['url'] = esc_url( get_permalink() );
break;
case 'display_name':
// If displaying multiple posts, the display_name (author) will be reverted to site name later.
$attribution['text'] = esc_html(get_the_author_meta('display_name'));
$attribution['url'] = esc_url(get_permalink());
break;
case 'other':
$other = isset( $license['attribute_other'] ) ? $license['attribute_other'] : '';
$other_url = isset( $license['attribute_other_url'] ) ? $license['attribute_other_url'] : '';
$attribution['text'] = esc_html( $other );
$attribution['url'] = esc_url( $other_url );
break;
}
return $attribution;
}
function license_as_widget() {
require_once('widgets/wplicense_widget.php');
register_widget( 'WPLicense_widget' );
}
}
$license = new WPLicense();
} else {
$this->_logger('Could not instantiate class WPLicense due to already existing class WPLicense.');
}