Index: trunk/tools/wp-photocommons/inc/class-photocommons.php |
— | — | @@ -1,11 +1,11 @@ |
2 | 2 | <?php |
3 | | -class Photocommons { |
4 | | - // TODO: ugly |
5 | | - const PLUGIN_PATH = "/wp-content/plugins/wp-photocommons/"; |
6 | | - const RESIZE_URL = "http://commons.wikimedia.org/w/api.php?action=query&titles=%s&prop=imageinfo&iiprop=url&iiurlwidth=%s&format=php"; |
| 3 | +class PhotoCommons { |
| 4 | + const PLUGIN_PATH = '/wp-photocommons/'; |
| 5 | + const FILEPATH_PATTERN = 'http://commons.wikimedia.org/w/index.php?title=Special:FilePath&file=%s&width=%s'; |
| 6 | + const FILEPAGE_PATTERN = 'http://commons.wikimedia.org/w/index.php?title=File:%s'; |
7 | 7 | |
8 | 8 | function __construct() { |
9 | | - if (is_admin()) { |
| 9 | + if ( is_admin() ) { |
10 | 10 | $this->initAdmin(); |
11 | 11 | } else { |
12 | 12 | $this->initFrontend(); |
— | — | @@ -14,39 +14,26 @@ |
15 | 15 | |
16 | 16 | public function addShortcode($args) { |
17 | 17 | $filename = $args['file']; |
18 | | - $size = $args['size']; |
| 18 | + $width = $args['width']; |
19 | 19 | $align = empty($args['align']) ? 'alignright' : 'align' . $args['align']; |
20 | | - $d = $this->doApiThumbResizeRequest($filename, $size); |
| 20 | + $thumb = $this->getThumbUrl($filename, $width); |
| 21 | + $filepage = $this->getPageUrl($filename, $width); |
21 | 22 | |
22 | 23 | return sprintf( |
23 | | - '<a href="%s" title="%s">' . |
24 | | - '<img src="%s" title="%s" alt="%s" class="%s" width="%s" height="%s" />' . |
| 24 | + '<a href="%s" title="%s" class="wp-photocommons-thumb">' . |
| 25 | + '<img src="%s" title="%s via Wikimedia Commons" alt="%s" class="%s" width="%s" />' . |
25 | 26 | '</a>', |
26 | | - $d['url'], $d['title'], $d['src'], $d['title'], $d['title'], |
27 | | - $align, |
28 | | - $d['width'], $d['height'] |
| 27 | + $filepage, $filename, |
| 28 | + $thumb, $filename, $filename, $align, $width |
29 | 29 | ); |
30 | 30 | } |
31 | 31 | |
32 | | - private function doApiThumbResizeRequest($filename, $size) { |
33 | | - ini_set('user_agent', 'Photocommons/1.0'); |
34 | | - $url = $this->getResizeUrl($filename, $size); |
35 | | - $result = unserialize(file_get_contents($url)); |
36 | | - $data = array_pop($result['query']['pages']); |
37 | | - |
38 | | - $image = $data['imageinfo'][0]; |
39 | | - |
40 | | - return array( |
41 | | - "url" => $image['descriptionurl'], |
42 | | - "src" => $image['thumburl'], |
43 | | - "width" => $image['thumbwidth'], |
44 | | - "height" => $image['thumbheight'], |
45 | | - "title" => $data['title'] |
46 | | - ); |
| 32 | + private function getThumbUrl($file, $width) { |
| 33 | + return sprintf(self::FILEPATH_PATTERN, rawurlencode($file), rawurlencode($width)); |
47 | 34 | } |
48 | 35 | |
49 | | - private function getResizeUrl($file, $size) { |
50 | | - return sprintf(self::RESIZE_URL, rawurlencode($file), rawurlencode($size)); |
| 36 | + private function getPageUrl($file, $width) { |
| 37 | + return sprintf(self::FILEPAGE_PATTERN, rawurlencode($file)); |
51 | 38 | } |
52 | 39 | |
53 | 40 | private function initAdmin() { |
— | — | @@ -56,9 +43,9 @@ |
57 | 44 | |
58 | 45 | private function enqueueScripts() { |
59 | 46 | // Register some of our own scripts |
60 | | - wp_register_script('admin', self::PLUGIN_PATH . 'js/admin.js'); |
61 | | - wp_register_script('search', self::PLUGIN_PATH . 'js/search.js'); |
62 | | - wp_register_script('suggestions', self::PLUGIN_PATH . 'js/jquery.suggestions.js'); |
| 47 | + wp_register_script('admin', plugins_url() . self::PLUGIN_PATH . 'js/admin.js'); |
| 48 | + wp_register_script('search', plugins_url() . self::PLUGIN_PATH . 'js/search.js'); |
| 49 | + wp_register_script('suggestions', plugins_url() . self::PLUGIN_PATH . 'js/jquery.suggestions.js'); |
63 | 50 | |
64 | 51 | // Enqueue external libraries |
65 | 52 | wp_enqueue_script('jquery'); |
— | — | @@ -74,8 +61,8 @@ |
75 | 62 | private function enqueueStyles() { |
76 | 63 | // Register our own styles and enqueue |
77 | 64 | wp_register_style('jquid_jquery_blog_stylesheet', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/redmond/jquery-ui.css'); |
78 | | - wp_register_style('suggestions', self::PLUGIN_PATH . "css/jquery.suggestions.css"); |
79 | | - wp_register_style('search', self::PLUGIN_PATH . "css/search.css"); |
| 65 | + wp_register_style('suggestions', plugins_url() . self::PLUGIN_PATH . 'css/jquery.suggestions.css'); |
| 66 | + wp_register_style('search', plugins_url() . self::PLUGIN_PATH . 'css/search.css'); |
80 | 67 | |
81 | 68 | wp_enqueue_style('jquid_jquery_blog_stylesheet'); |
82 | 69 | wp_enqueue_style('suggestions'); |
— | — | @@ -84,6 +71,6 @@ |
85 | 72 | } |
86 | 73 | |
87 | 74 | private function initFrontend() { |
88 | | - add_shortcode("photocommons", array($this, "addShortcode")); |
| 75 | + add_shortcode('photocommons', array($this, 'addShortcode')); |
89 | 76 | } |
90 | 77 | } |
\ No newline at end of file |
Index: trunk/tools/wp-photocommons/js/search.js |
— | — | @@ -1,20 +1,15 @@ |
2 | | -// usage: log('inside coolFunc',this,arguments); |
3 | | -// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/ |
4 | | -window.log = function(){ |
5 | | - log.history = log.history || []; // store logs to an array for reference |
6 | | - log.history.push(arguments); |
7 | | - if(this.console){ |
8 | | - console.log( Array.prototype.slice.call(arguments) ); |
9 | | - } |
| 2 | +// Debug |
| 3 | +window.log = function( a, b ) { |
| 4 | + //console.log( a, b ); |
10 | 5 | }; |
11 | 6 | |
12 | | -if ( !window.Photocommons ) { |
13 | | - window.Photocommons = {}; |
| 7 | +if ( !window.PhotoCommons ) { |
| 8 | + window.PhotoCommons = {}; |
14 | 9 | } |
15 | 10 | |
16 | 11 | (function($){ |
17 | 12 | |
18 | | - $.extend( Photocommons, { |
| 13 | + $.extend( PhotoCommons, { |
19 | 14 | |
20 | 15 | getQueryUrl: function( type, args ) { |
21 | 16 | var queries = { |
— | — | @@ -54,7 +49,7 @@ |
55 | 50 | throw new Error( 'Unknown query type' ); |
56 | 51 | } |
57 | 52 | |
58 | | - return Photocommons.makeUrl(queries[type](args)); |
| 53 | + return PhotoCommons.makeUrl(queries[type](args)); |
59 | 54 | }, |
60 | 55 | |
61 | 56 | makeUrl: function( args ) { |
— | — | @@ -90,7 +85,7 @@ |
91 | 86 | /* jQuery suggestions */ |
92 | 87 | $( '#wp-photocommons-search' ).suggestions( { |
93 | 88 | fetch: function( query ) { |
94 | | - var url = Photocommons.getQueryUrl( 'pagesearch', { |
| 89 | + var url = PhotoCommons.getQueryUrl( 'pagesearch', { |
95 | 90 | 'search' : query |
96 | 91 | }); |
97 | 92 | $.getJSON( url, function( data ) { |
— | — | @@ -104,7 +99,7 @@ |
105 | 100 | result: { |
106 | 101 | select: function( $result ) { |
107 | 102 | var value = $result.val(), |
108 | | - url = Photocommons.getQueryUrl( 'pageimages', { |
| 103 | + url = PhotoCommons.getQueryUrl( 'pageimages', { |
109 | 104 | 'title' : value, |
110 | 105 | 'width' : '200' |
111 | 106 | }); |
— | — | @@ -117,11 +112,15 @@ |
118 | 113 | $( '#wp-photocommons-images' ).html( 'No images found :(' ); |
119 | 114 | } else { |
120 | 115 | $.each( data.query.pageids, function( key, pageid ) { |
121 | | - var img = data.query.pages[pageid]; |
| 116 | + var img = data.query.pages[pageid], |
| 117 | + pagetitle; |
122 | 118 | if ( img.imageinfo && img.imageinfo[0] ) { |
| 119 | + pagetitle = img.title.split(':'); |
| 120 | + pagetitle.shift(); |
| 121 | + pagetitle = pagetitle.join(':'); |
123 | 122 | $('<div class="image">').attr({ |
124 | 123 | 'style': "background-image:url('" + img.imageinfo[0].thumburl + "');", |
125 | | - 'data-filename': img.title |
| 124 | + 'data-filename': pagetitle |
126 | 125 | }).appendTo('#wp-photocommons-images'); |
127 | 126 | |
128 | 127 | } |
— | — | @@ -140,6 +139,6 @@ |
141 | 140 | |
142 | 141 | // Init |
143 | 142 | // FIXME |
144 | | - $( document ).ready( Photocommons.init ); |
| 143 | + $( document ).ready( PhotoCommons.init ); |
145 | 144 | |
146 | 145 | })(jQuery); |
\ No newline at end of file |
Index: trunk/tools/wp-photocommons/js/admin.js |
— | — | @@ -1,38 +1,39 @@ |
2 | 2 | (function($) { |
3 | | - var PATH = "../wp-content/plugins/wp-photocommons"; |
| 3 | + var PATH = '../wp-content/plugins/wp-photocommons'; |
4 | 4 | |
5 | 5 | function addButtons() { |
6 | | - $("#media-buttons").append(''.concat( |
| 6 | + $('#media-buttons').append(''.concat( |
7 | 7 | '<a id="photocommons-add" title="Afbeeldingen invoegen van Wikimedia Commons" style="padding-left:4px;">', |
8 | 8 | '<img src="' + PATH + '/img/button.png"/>', |
9 | 9 | '</a>' |
10 | 10 | )); |
11 | 11 | |
12 | | - $("#photocommons-add").live('click', function(e) { |
| 12 | + $('#photocommons-add').live('click', function(e) { |
13 | 13 | e.preventDefault(); |
14 | 14 | |
15 | | - $("body").prepend('<div id="photocommons-dialog"></div>'); |
16 | | - $("#photocommons-dialog").load(PATH + "/search.php?standalone=1", function() { |
17 | | - Photocommons.init(); |
| 15 | + $('body').prepend('<div id="photocommons-dialog"></div>'); |
| 16 | + $('#photocommons-dialog').load(PATH + '/search.php?standalone=1', function() { |
| 17 | + PhotoCommons.init(); |
18 | 18 | |
19 | | - var $self = $("#photocommons-dialog"); |
| 19 | + var $self = $('#photocommons-dialog'); |
20 | 20 | |
21 | 21 | $self.dialog({ |
22 | | - title : 'Photocommons - Afbeelingen invoegen van Wikimedia Commons', |
| 22 | + title : 'PhotoCommons - Afbeelingen invoegen van Wikimedia Commons', |
23 | 23 | width : 800, |
24 | 24 | height : 500 |
25 | 25 | }); |
26 | 26 | |
27 | | - $("#wp-photocommons-images .image").live('click', function() { |
| 27 | + $('#wp-photocommons-images .image').live('click', function() { |
28 | 28 | var file = $(this).attr('data-filename'), |
29 | | - shortcode = '[photocommons file="' + file + '" size="300"]' + "\n"; |
| 29 | + shortcode = '[photocommons file="' + file + '" width="300"] '; |
30 | 30 | |
31 | 31 | // Depending on whether we are in Wysiwyg or HTML mode we |
32 | 32 | // do a different insert |
33 | | - if ($("#edButtonHTML").hasClass('active')) { |
| 33 | + if ($('#edButtonHTML').hasClass('active')) { |
34 | 34 | // HTML editor |
35 | | - cnt = $("#content").val(); |
36 | | - $("#content").val( + cnt); |
| 35 | + $('#content').val( function(i,val){ |
| 36 | + return shortcode + val; |
| 37 | + }); |
37 | 38 | } else { |
38 | 39 | // Wysiwyg |
39 | 40 | tinyMCE.execCommand('mceInsertContent', false, shortcode); |