Skip to content

Commit 4b2fd2c

Browse files
paulgibbsrodrigoprimo
authored andcommitted
Add documentation for WordPress.DB.RestrictedClasses
See WordPress#1722
1 parent a3f1eed commit 4b2fd2c

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0"?>
2+
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4+
title="Restricted Database Classes and Methods"
5+
>
6+
<standard>
7+
<![CDATA[
8+
Avoid touching the database directly with PHP library classes and methods. Use the $wpdb object and associated functions instead.
9+
]]>
10+
</standard>
11+
<code_comparison>
12+
<code title="Invalid: Using the object-orientated mysqli interface">
13+
<![CDATA[
14+
$mysqli = new mysqli(
15+
"example.com",
16+
$db_user,
17+
$db_pass,
18+
$db_name
19+
);
20+
21+
$result = <em>$mysqli->query</em>(
22+
$mysqli,
23+
"SELECT * FROM wp_posts LIMIT 5"
24+
);
25+
]]>
26+
</code>
27+
<code title="Valid: Use WordPress functions">
28+
<![CDATA[
29+
$result = <em>get_posts()</em>;
30+
]]>
31+
</code>
32+
</code_comparison>
33+
<code_comparison>
34+
<code title="Invalid: Using the object-orientated PDO and PDOStatement interfaces to insert a post">
35+
<![CDATA[
36+
$pdo = new PDO(
37+
$dsn,
38+
$db_user,
39+
$db_pass
40+
);
41+
42+
$stmnt = $pdo->prepare(
43+
"INSERT INTO wp_posts (post_title)
44+
VALUES (?)"
45+
);
46+
47+
$stmnt->execute( ['Title'] );
48+
]]>
49+
</code>
50+
<code title="Valid: Use WordPress functions">
51+
<![CDATA[
52+
<em>wp_insert_post</em>(
53+
array( 'post_title' => 'Title' )
54+
);
55+
56+
// or...
57+
58+
global $wpdb;
59+
<em>$wpdb->insert</em>(
60+
"{$wpdb->prefix}_posts",
61+
array( 'post_title' => 'Title' ),
62+
array( '%s' )
63+
);
64+
]]>
65+
</code>
66+
</code_comparison>
67+
</documentation>

0 commit comments

Comments
 (0)