Skip to content

Commit 547b7ab

Browse files
paulgibbsrodrigoprimojrfnl
committed
Add documentation for WordPress.DB.RestrictedClasses
Co-authored-by: Paul Wong-Gibbs <paulgibbs@users.noreply.github.com> Co-authored-by: Rodrigo Primo <rodrigoprimo@users.noreply.github.com> Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com>
1 parent a3f1eed commit 547b7ab

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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"
5+
>
6+
<standard>
7+
<![CDATA[
8+
Avoid touching the database directly. Use the $wpdb object and associated functions instead of using classes from PHP database extensions.
9+
]]>
10+
</standard>
11+
<code_comparison>
12+
<code title="Valid: Using a WordPress function to fetch posts.">
13+
<![CDATA[
14+
$results = <em>get_posts()</em>;
15+
]]>
16+
</code>
17+
<code title="Invalid: Using the mysqli class to fetch posts.">
18+
<![CDATA[
19+
$mysqli = <em>new mysqli</em>(
20+
'localhost',
21+
$user,
22+
$pass,
23+
$db
24+
);
25+
26+
$results = $mysqli->query(
27+
"SELECT * FROM wp_posts LIMIT 5"
28+
);
29+
]]>
30+
</code>
31+
</code_comparison>
32+
<code_comparison>
33+
<code title="Valid: Using WordPress functions to insert a post.">
34+
<![CDATA[
35+
<em>wp_insert_post</em>(
36+
array( 'post_title' => 'Title' )
37+
);
38+
39+
// or...
40+
41+
global $wpdb;
42+
<em>$wpdb->insert</em>(
43+
$wpdb->posts,
44+
array( 'post_title' => 'Title' ),
45+
array( '%s' )
46+
);
47+
]]>
48+
</code>
49+
<code title="Invalid: Using PDO class to insert a post.">
50+
<![CDATA[
51+
$pdo = <em>new PDO</em>(
52+
$dsn,
53+
$user,
54+
$pass
55+
);
56+
57+
$stmt = $pdo->prepare(
58+
"INSERT INTO wp_posts (post_title)
59+
VALUES (?)"
60+
);
61+
62+
$stmt->execute( array( 'Title' ) );
63+
]]>
64+
</code>
65+
</code_comparison>
66+
</documentation>

0 commit comments

Comments
 (0)