forked from hypothesis/wp-hypothesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypothesis.php
122 lines (105 loc) · 4.49 KB
/
hypothesis.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
<?php
/**
* Plugin Name: Hypothesis
* Plugin URI: https://hypothes.is/
* Description: Hypothesis is an open platform for the collaborative evaluation of knowledge. This plugin embeds the necessary scripts in your WordPress site to enable any user to use Hypothesis without installing any extensions.
* Version: 0.6.0
* Requires at least: 3.7
* Requires PHP: 5.6
* Author: The Hypothesis Project and contributors
* Author URI: https://hypothes.is/
* License: BSD
* License URI: https://opensource.org/licenses/BSD-2-Clause
* Text Domain: hypothesis
* Domain Path: /languages
**/
// Exit if called directly.
defined( 'ABSPATH' ) || die( 'Cannot access pages directly.' );
// Load textdomain
function hypothesis_load_plugin_textdomain() {
load_plugin_textdomain( 'hypothesis', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'hypothesis_load_plugin_textdomain' );
define( 'HYPOTHESIS_PLUGIN_VERSION', '0.6.0' );
require_once __DIR__ . '/class-hypothesissettingspage.php';
if ( is_admin() ) {
$hypothesis_settings_page = new HypothesisSettingsPage();
}
/**
* Add Hypothesis based on conditions set in the plugin settings.
*/
add_action( 'wp', 'add_hypothesis' );
/**
* Wrapper for the primary Hypothesis wp_enqueue call.
*/
function enqueue_hypothesis() {
wp_enqueue_script( 'hypothesis', 'https://hypothes.is/embed.js', array(), HYPOTHESIS_PLUGIN_VERSION, true );
}
/**
* Add Hypothesis script(s) to front end.
*/
function add_hypothesis() {
$options = get_option( 'wp_hypothesis_options' );
$posttypes = HypothesisSettingsPage::get_posttypes();
// Set defaults if we $options is not set yet.
if ( empty( $options ) ) :
$defaults = array(
'highlights-on-by-default' => 1,
);
add_option( 'wp_hypothesis_options', $defaults );
endif;
// Otherwise highlighting is on by default.
wp_enqueue_script( 'nohighlights', plugins_url( 'js/nohighlights.js', __FILE__ ), array(), HYPOTHESIS_PLUGIN_VERSION, true );
// Embed options.
if ( isset( $options['highlights-on-by-default'] ) ) :
wp_enqueue_script( 'showhighlights', plugins_url( 'js/showhighlights.js', __FILE__ ), array(), HYPOTHESIS_PLUGIN_VERSION, true );
endif;
if ( isset( $options['sidebar-open-by-default'] ) ) :
wp_enqueue_script( 'sidebaropen', plugins_url( 'js/sidebaropen.js', __FILE__ ), array(), HYPOTHESIS_PLUGIN_VERSION, true );
endif;
if ( isset( $options['serve-pdfs-with-via'] ) ) :
wp_enqueue_script( 'pdfs-with-via', plugins_url( 'js/via-pdf.js', __FILE__ ), array(), HYPOTHESIS_PLUGIN_VERSION, true );
$uploads = wp_upload_dir();
wp_localize_script(
'pdfs-with-via',
'HypothesisPDF',
array(
'uploadsBase' => trailingslashit( $uploads['baseurl'] ),
)
);
endif;
// Content settings.
$enqueue = false;
if ( is_front_page() && isset( $options['allow-on-front-page'] ) ) {
enqueue_hypothesis();
} elseif ( is_home() && isset( $options['allow-on-blog-page'] ) ) {
enqueue_hypothesis();
}
foreach ( $posttypes as $slug => $name ) {
if ( 'page' !== $slug ) {
$posttype = $slug;
if ( 'post' === $slug ) {
$slug = 'posts'; // Backwards compatibility.
}
if ( isset( $options[ "allow-on-$slug" ] ) && is_singular( $posttype ) ) { // Check if Hypothesis is allowed on this post type.
if ( isset( $options[ $posttype . '_ids_override' ] ) && ! is_single( $options[ $posttype . '_ids_override' ] ) ) { // Make sure this post isn't in the override list if it exists.
enqueue_hypothesis();
} elseif ( ! isset( $options[ $posttype . '_ids_override' ] ) ) {
enqueue_hypothesis();
}
} elseif ( ! isset( $options[ "allow-on-$slug" ] ) && isset( $options[ $posttype . '_ids_show_h' ] ) && is_single( $options[ $posttype . '_ids_show_h' ] ) ) { // Check if Hypothesis is allowed on this specific post.
enqueue_hypothesis();
}
} elseif ( 'page' === $slug ) {
if ( isset( $options['allow-on-pages'] ) && is_page() && ! is_front_page() && ! is_home() ) { // Check if Hypothesis is allowed on pages (and that we aren't on a special page).
if ( isset( $options['page_ids_override'] ) && ! is_page( $options['page_ids_override'] ) ) { // Make sure this page isn't in the override list if it exists.
enqueue_hypothesis();
} elseif ( ! isset( $options['page_ids_override'] ) ) {
enqueue_hypothesis();
}
} elseif ( ! isset( $options['allow-on-pages'] ) && isset( $options['page_ids_show_h'] ) && is_page( $options['page_ids_show_h'] ) ) { // Check if Hypothesis is allowed on this specific page.
enqueue_hypothesis();
}
}
}
}