1
|
|
|
<?php |
2
|
|
|
namespace App\Service; |
3
|
|
|
|
4
|
|
|
class WpPostService |
5
|
|
|
{ |
6
|
|
|
public function __construct($pdo) |
7
|
|
|
{ |
8
|
|
|
$this->pdo = $pdo; |
|
|
|
|
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function findOneAlbumByName($albumName) |
12
|
|
|
{ |
13
|
|
|
$sql = 'SELECT * FROM muu_posts WHERE post_title = "%'.$albumName.'%" AND post_type="album";'; |
14
|
|
|
foreach ($this->pdo->query($sql) as $res) { |
15
|
|
|
return $res; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function findOneLyricByTitle($title) |
20
|
|
|
{ |
21
|
|
|
$sql = 'SELECT * FROM muu_posts WHERE post_title = "'.$title.'" AND post_type="lyrics";'; |
22
|
|
|
foreach ($this->pdo->query($sql) as $res) { |
23
|
|
|
return $res; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function findOneArtistByName($artistName) |
28
|
|
|
{ |
29
|
|
|
$sql = 'SELECT * FROM muu_posts WHERE post_title = "'.$artistName.'" AND post_type="artist";'; |
30
|
|
|
foreach ($this->pdo->query($sql) as $res) { |
31
|
|
|
return $res; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function insertPost($params) |
36
|
|
|
{ |
37
|
|
|
$sql = $this->pdo->prepare(' |
38
|
|
|
INSERT INTO muu_posts |
39
|
|
|
(post_name, post_author, post_date, post_date_gmt, post_title, post_content, post_status, comment_status, ping_status, post_type) |
40
|
|
|
VALUES (:post_name, 1, :post_date, :post_gmt_date, :post_title, :post_content, :post_status, :comment_status, :ping_status, :post_type) |
41
|
|
|
'); |
42
|
|
|
foreach ($params as $key => $param) { |
43
|
|
|
$sql->bindValue($key, $param); |
44
|
|
|
} |
45
|
|
|
if (!$sql->execute()) { |
46
|
|
|
var_dump($this->pdo->errorInfo());die; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
$id = $this->pdo->lastInsertId(); |
49
|
|
|
$sql = 'SELECT * FROM muu_posts WHERE ID='.$id; |
50
|
|
|
foreach ($this->pdo->query($sql) as $res) { |
51
|
|
|
return $res; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function insertMeta($params) |
56
|
|
|
{ |
57
|
|
|
$sql = $this->pdo->prepare(' |
58
|
|
|
INSERT INTO muu_postmeta |
59
|
|
|
(post_id, meta_key, meta_value) |
60
|
|
|
VALUES (:post_id, :key, :value) |
61
|
|
|
'); |
62
|
|
|
foreach ($params as $key => $param) { |
63
|
|
|
$sql->bindValue($key, $param); |
64
|
|
|
} |
65
|
|
|
$sql->execute(); |
66
|
|
|
return $this->pdo->lastInsertId(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: