-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimberAttachment_UnitTestCase.php
More file actions
91 lines (77 loc) · 2.56 KB
/
Copy pathTimberAttachment_UnitTestCase.php
File metadata and controls
91 lines (77 loc) · 2.56 KB
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
<?php
class TimberAttachment_UnitTestCase extends Timber_UnitTestCase {
var $_files;
protected function addFile( $file ) {
$this->_files[] = $file;
}
function set_up(){
parent::set_up();
$this->_files = array();
}
function tear_down() {
parent::tear_down();
if (isset($this->_files) && is_array($this->_files)) {
foreach($this->_files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
$this->_files = array();
}
}
/* ----------------
* Helper functions
---------------- */
static function replace_attachment( $old_id, $new_id ) {
$uploadDir = wp_get_upload_dir();
$newFile = $uploadDir['basedir'].'/'.get_post_meta($new_id, '_wp_attached_file', true);
$oldFile = $uploadDir['basedir'].'/'.get_post_meta($old_id, '_wp_attached_file', true);
if (!file_exists(dirname($oldFile)))
mkdir(dirname($oldFile), 0777, true);
copy($newFile, $oldFile);
$meta = wp_generate_attachment_metadata($old_id, $oldFile);
wp_update_attachment_metadata($old_id, $meta);
wp_delete_post($new_id, true);
}
static function copyTestAttachment( $img = 'arch.jpg', $dest_name = null ) {
$upload_dir = wp_get_upload_dir();
if ( is_null($dest_name) ) {
$dest_name = $img;
}
$destination = $upload_dir['path'].'/'.$dest_name;
copy( __DIR__.'/assets/'.$img, $destination );
return $destination;
}
static function getTestAttachmentURL( $img = 'arch.jpg', $relative = false) {
$upload_dir = wp_get_upload_dir();
$result = $upload_dir['url'].'/'.$img;
if ( $relative ) {
$result = str_replace(home_url(), '', $result);
}
return $result;
}
public static function is_connected() {
$connected = @fsockopen( "www.google.com", 80, $errno, $errstr, 3 );
if ( $connected ) {
$is_conn = true; //action when connected
fclose( $connected );
} else {
$is_conn = false; //action in connection failure
}
return $is_conn;
}
function add_lang_to_home( $url, $path, $orig_scheme, $blog_id ){
return "$url?lang=en";
}
public static function get_attachment( $pid = 0, $file = 'arch.jpg' ) {
$filename = self::copyTestAttachment( $file );
$filetype = wp_check_filetype( basename( $filename ), null );
$attachment = array('post_title' => 'The Arch', 'post_content' => '', 'post_mime_type' => $filetype['type']);
$iid = wp_insert_attachment( $attachment, $filename, $pid );
return $iid;
}
public static function get_timber_attachment_object($file = 'cropper.png') {
$iid = self::get_attachment(0, $file);
return Timber::get_post( $iid );
}
}