PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.0.1
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.0.1
0.5.7 0.5.6 0.5.5 0.5.4 0.5.3 0.5.2 0.5.1 0.5.0 0.4.9 0.4.8 0.4.7 0.4.6 trunk 0.0.1 0.0.2 0.2.8 0.2.9 0.3.0 0.3.1 0.3.2 0.3.3 0.3.4 0.3.5 0.3.6 0.3.7 All 33 releases
code-engine / classes / snippets / code_snippets.php

code_snippets.php in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.0.1, at classes/snippets/code_snippets.php

134 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class for handling snippets of Code Snippets.
4 */
5 class Meow_CDEGN_Snippets_Code_Snippets {
6
7 /** @var string */
8 public static $src = 'Code Snippets';
9
10 /** @var object */
11 private $wpdb;
12
13 /** @var string */
14 private $table_name = 'snippets';
15
16 /** @var string */
17 private $table_name_with_prefix = 'snippets';
18
19 /** @var array<string> */
20 private $scope = ['single-use'];
21
22 public function __construct(){
23 global $wpdb;
24 $this->wpdb = $wpdb;
25 $this->table_name_with_prefix = $this->wpdb->prefix . $this->table_name;
26 }
27
28 /**
29 * Get the specific snippet
30 *
31 * @param int $id
32 * @return Meow_CDEGN_Models_Snippet|null
33 */
34 public function getById( int $id ) {
35 $placeholder = implode( ',', array_fill( 0, count( $this->scope ), '%s' ) );
36
37 $sql = $this->wpdb->prepare( "
38 SELECT *
39 FROM $this->table_name_with_prefix
40 WHERE id = %d AND scope IN ( $placeholder )
41 ", array_merge( [ $id ], $this->scope ) );
42
43 $result = $this->wpdb->get_row( $sql );
44
45 return $result
46 ? new Meow_CDEGN_Models_Snippet(
47 $result->id,
48 Meow_CDEGN_Snippets_Code_Snippets::$src,
49 $result->name,
50 $result->description,
51 $result->code,
52 $this->get_edit_url( $result->id )
53 )
54 : null;
55 }
56
57 /**
58 * Get a snippets
59 *
60 * @param string $cdegn_order
61 * @param string $order_by
62 * @param int $limit
63 * @param int $offset
64 * @return array<Meow_CDEGN_Models_Snippet>
65 */
66 public function get( string $cdegn_order, string $order_by, int $limit, int $offset ) {
67 $placeholder = implode( ',', array_fill( 0, count( $this->scope ), '%s' ) );
68
69 $sql = $this->wpdb->prepare( "
70 SELECT id, name, description, code
71 FROM $this->table_name_with_prefix
72 WHERE scope IN ( $placeholder )
73 ORDER BY %s %s
74 LIMIT %d OFFSET %d
75 ", array_merge( $this->scope, [ $order_by, $cdegn_order, $limit, $offset ] ) );
76
77 $results = $this->wpdb->get_results( $sql );
78
79 return array_map( function ($item) {
80 return new Meow_CDEGN_Models_Snippet(
81 $item->id,
82 Meow_CDEGN_Snippets_Code_Snippets::$src,
83 $item->name,
84 $item->description,
85 $item->code,
86 $this->get_edit_url( $item->id )
87 );
88 }, $results );
89 }
90
91 /**
92 * Get the total number of snippets
93 *
94 * @return int
95 */
96 public function get_total() {
97 $placeholder = implode( ',', array_fill( 0, count( $this->scope ), '%s' ) );
98
99 $sql = $this->wpdb->prepare( "
100 SELECT COUNT(*)
101 FROM $this->table_name_with_prefix
102 WHERE scope IN ( $placeholder )
103 ", $this->scope );
104
105 $results = $this->wpdb->get_var( $sql );
106
107 return (int)$results;
108 }
109
110 /**
111 * Get the edit URL for a snippet
112 *
113 * @param int $id
114 * @return string
115 */
116 protected function get_edit_url( int $id ): string {
117 $base_url = 'admin.php?page=edit-snippet';
118
119 // Check if the snippet is shared on the network
120 $is_shared_network_snippet = false;
121 $network = function_exists( 'is_network_admin' ) ? is_network_admin() : false;
122 if ( $network ) {
123 $shared_network_snippets = get_site_option( 'shared_network_snippets', [] );
124 $is_shared_network_snippet = in_array( $id, $shared_network_snippets, true );
125 }
126
127 // Build the URL depends on the context
128 $url = $is_shared_network_snippet ? network_admin_url( $base_url ) : self_admin_url( $base_url );
129
130 // Add the ID
131 return add_query_arg( 'id', absint( $id ), $url );
132 }
133 }
134