/
onepanelexternals.php
67 lines (46 loc) · 1.8 KB
/
onepanelexternals.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
<?php
class OnePanelExternals {
public static function AddActions() {
add_action( 'save_post', array( 'OnePanelExternals', 'AutoGenThumbs' ) );
}
/**
*
* @param $post
* @return unknown_type
* @todo anything in the html that has dodgy characters in it will make DOM unhappy.
* Probably best to hold back errors.
*/
public static function AutoGenThumbs( $post ) {
$tracker = OnePanelDebug::Track( 'Trying to autogenerate thumbnails.' );
// Wordpress is crazy
$post_id = wp_is_post_revision($post);
OnePanelDebug::Info( 'Post id: ' . $post_id );
// Create thumbnail module object?
OnePanelLib::RequireFileOnce( ONE_PANEL_DIR . '/panel/module.php' );
OnePanelLib::RequireFileOnce( ONE_PANEL_DIR . '/panel/feature.php' );
OnePanelLib::RequireFileOnce( ONE_PANEL_DIR . '/panel/thumbnails.php' );
$thumbnail_feature_object = new Thumbnails();
// Scan the post for images
$dom_doc = new DOMDocument();
@$dom_doc->loadHTML( $_POST['content'] );
// Grab the first image
$first_image = $dom_doc->getElementsByTagName( 'img' )->item(0);
if (is_null( $first_image )) {
OnePanelDebug::Info( 'No images found in post' );
return true;
}
// Get the location of the image
$src = str_replace( '"', '' ,$first_image->getAttribute( 'src' ) );
$src = str_replace( '\\', '', $src );
// Get the real path
$src = str_replace( get_option('siteurl'), '', $src) ;
$location = ABSPATH . $src;
$location = str_replace( '//' , '/', $location );
// Generate
OnePanelDebug::Info( 'Calling CreateThumbs with ' . $location . ' ' . $post_id );
$thumbnail_feature_object->CreateThumbs( $location, $post_id, 'All', false );
// All done
$tracker->Affirm();
return true;
}
}