This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
216 lines (181 loc) · 6.39 KB
/
plugin.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
<?php
/**
* Plugin Name: IMDb Profile Widget
* Description: This is a plugin that shows your IMDd profile with a simple widget.
* Version: 1.1.0
* Author: Henrique Dias and Luís Soares (imdb)
* Author URI: https://github.com/imdb
* Network: true
* License: GPL2 or later
*
* IMDb Widget for WordPress
*
* Copyright (C) 2015 Henrique Dias <[email protected]>
* Copyright (C) 2015 Luís Soares <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// third-party libraries
require_once( 'lib/htmlcompressor.php' );
require_once( 'vendor/autoload.php' );
use SmartScraper\Parser;
// prevent direct file access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// check the current PHP version
if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
exit( sprintf( 'Foo requires PHP 5.6.0 or higher. You’re still on %s.', PHP_VERSION ) );
}
class IMDb_Widget extends WP_Widget {
protected $widget_slug = 'imdb-widget';
protected $options = array(
"title",
"userId"
);
protected $config;
protected $optionsShow = array(
'bio',
'member since',
'picture',
'badges',
'watchlist',
'lists',
'ratings',
'reviews',
'boards'
);
public function __construct() {
parent::__construct(
$this->get_widget_slug(), __( 'IMDb Profile ', $this->get_widget_slug() ), array(
'classname' => $this->get_widget_slug() . '-class',
'description' => __( 'A widget to show a small version of your IMDb profile.', $this->get_widget_slug() )
)
);
add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts' ) );
}
public function get_widget_slug() {
return $this->widget_slug;
}
public function form( $config ) {
$config = ! empty( $config ) ? unserialize( $config ) : array();
foreach ( $config as $key => $value ) { // recover options
${$key} = esc_attr( $value );
}
ob_start( "imdb_HTMLCompressor" );
require 'views/options.php';
ob_end_flush();
}
public function update( $new_instance, $old_instance ) {
return serialize( $new_instance );
}
public function widget( $args, $config ) {
extract( $args, EXTR_SKIP );
$config = ! empty( $config ) ? unserialize( $config ) : array();
ob_start( "imdb_HTMLCompressor" );
if ( ! isset( $config['userId'] ) ) {
echo 'You need to first configure the plugin :)';
} else {
$info = $this->get_info( $config['userId'] );
require 'views/widget.php';
}
ob_end_flush();
}
private function get_info( $userId ) {
$info = new Parser( 'http://www.imdb.com/' . 'user/' . $userId . '/' );
$info->baseUrl = 'http://www.imdb.com';
foreach (
array(
'ratings',
'boards',
'lists',
'watchlist',
'checkins',
'boards/sendpm',
'comments-index',
'#pollResponses'
) as $relativeUrl
) {
$cleanId = preg_replace( '/[^A-Za-z]/', '', $relativeUrl );
$info->{$cleanId . 'Url'} = $info->url . $relativeUrl;
}
$info->saveText( 'nick', '.header h1' );
$info->saveText( 'avatar', '#avatar-frame img', 'src' );
$info->saveText( 'memberSince', '.header .timestamp' );
$info->saveText( 'bio', '.header .biography' );
$info->saveText( 'ratingsCount', '.see-more a' );
$info->saveHtml( 'ratingsDistribution', '.overall .histogram-horizontal' );
$info->saveHtml( 'ratingsByYear', '.byYear .histogram-horizontal' );
$info->saveHtml( 'ratingsByYearLegend', '.byYear .legend' );
$info->saveHtml( 'ratingsTopRatedGenres', '.histogram-vertical', 0 );
$info->saveHtml( 'ratingsTopRatedYears', '.histogram-vertical', 1 );
$info->at( '.ratings .item' )
->with( 'link', 'a', 'href' )
->with( 'logo', 'a img', 'src' )
->with( 'title', '.title a' )
->with( 'rating', '.sub-item .only-rating' )
->saveList( 'ratings' );
$info->at( '.badges .badge-frame' )
->with( 'title', '.name' )
->with( 'value', '.value' )
->saveList( 'badges' );
$info->at( '.watchlist .item' )
->with( 'title', '.sub-item a' )
->with( 'link', 'a', 'href' )
->with( 'logo', 'a img', 'src' )
->saveList( 'watchlist' );
$info->at( '.lists .user-list' )
->with( 'title', '.list-name' )
->with( 'link', '.list-meta', 'href' )
->with( 'meta', '.list-meta' )
->saveList( 'lists' );
$info->at( '.user-lists .user-list' )
->with( 'logo', 'img', 'src' )
->with( 'title', '.list-name' )
->with( 'link', 'a', 'href' )
->with( 'description', '.list-meta' )
->saveList( 'userLists' );
$info->at( '.reviews .item' )
->with( 'movieLogo', '.image img', 'src' )
->with( 'movieLink', '.image a', 'href' )
->with( 'movieTitle', 'h3 a' )
->with( 'movieYear', 'h3 span' )
->with( 'title', 'h4' )
->with( 'meta', '.details' )
->with( 'text', 'p' )
->saveList( 'reviews' );
$info->at( '.boards-comments .row' )
->with( 'boardLink', '.board a', 'href' )
->with( 'boardTitle', '.board a' )
->with( 'commentTitle', '.title a' )
->with( 'commentLink', '.title a', 'href' )
->with( 'when', '.when' )
->saveList( 'boards' );
return $info;
}
public function isChecked( $conf, $name ) {
return isset( $conf[ $name ] ) && $conf[ $name ] == 'on';
}
public function register_widget_styles() {
wp_enqueue_style( $this->get_widget_slug() . '-widget-styles', plugins_url( 'css/widget.css', __FILE__ ) );
}
public function register_widget_scripts() {
wp_enqueue_script( $this->get_widget_slug() . '-script', plugins_url( 'js/widget.js', __FILE__ ), array( 'jquery' ), null, true );
}
private function serveImage( $imageUrl ) {
return plugins_url( 'pic.php', __FILE__ ) . '?url=' . $imageUrl;
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget("IMDb_Widget");' ) );