|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Gestion des fichiers |
|
4
|
|
|
* |
|
5
|
|
|
* @package Evarisk\Plugin |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace eoxia; |
|
9
|
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
11
|
|
|
exit; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
if ( ! class_exists( '\eoxia\File_Util' ) ) { |
|
15
|
|
|
/** |
|
16
|
|
|
* Gestion des fichiers |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jimmy Latour <[email protected]> |
|
19
|
|
|
* @version 1.1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class File_Util extends \eoxia\Singleton_Util { |
|
22
|
|
|
/** |
|
23
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
|
24
|
|
|
* |
|
25
|
|
|
* @return void nothing |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function construct() {} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Upload le fichier $file et créer les méta données de ce fichier. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $file Les données du fichier. |
|
33
|
|
|
* @param int $element_id L'ID de l'élément pour l'attachement du fichier. |
|
34
|
|
|
* @return int L'id de l'attachement |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function move_file_and_attach( $file, $element_id ) { |
|
37
|
|
|
if ( ! is_string( $file ) || ! is_int( $element_id ) || ! is_file( $file ) ) { |
|
38
|
|
|
return false; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$wp_upload_dir = wp_upload_dir(); |
|
42
|
|
|
|
|
43
|
|
|
// Transfère le thumbnail. |
|
44
|
|
|
$upload_result = wp_upload_bits( basename( $file ), null, file_get_contents( $file ) ); |
|
45
|
|
|
|
|
46
|
|
|
$filetype = wp_check_filetype( basename( $upload_result['file'] ), null ); |
|
47
|
|
|
/** Set the default values for the current attachement */ |
|
48
|
|
|
$attachment_default_args = array( |
|
49
|
|
|
'guid' => $wp_upload_dir['url'] . '/' . basename( $upload_result['file'] ), |
|
50
|
|
|
'post_mime_type' => $filetype['type'], |
|
51
|
|
|
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $upload_result['file'] ) ), |
|
52
|
|
|
'post_content' => '', |
|
53
|
|
|
'post_status' => 'inherit', |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
/** Save new picture into database */ |
|
57
|
|
|
$attach_id = wp_insert_attachment( $attachment_default_args, $upload_result['file'], $element_id ); |
|
58
|
|
|
|
|
59
|
|
|
/** Create the different size for the given picture and get metadatas for this picture */ |
|
60
|
|
|
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload_result['file'] ); |
|
61
|
|
|
/** Finaly save pictures metadata */ |
|
62
|
|
|
wp_update_attachment_metadata( $attach_id, $attach_data ); |
|
63
|
|
|
|
|
64
|
|
|
return $attach_id; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} // End if(). |
|
|
|
|
|
|
68
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.