@@ -113,15 +113,26 @@ public function save() {
113113 * @param object Snippet object
114114 * @return object Cleaned snippet object
115115 */
116- public static function clean ($ dirty ) {
116+ public static function clean ($ dirty, $ target = " view " ) {
117117 if (is_array ($ dirty )) {
118- $ cleaned = array_map (array ("Snippet " , "clean " ), $ dirty );
118+ $ cleaned = array ();
119+ foreach ($ dirty as $ id => $ snippet ) {
120+ $ cleaned [$ id ] = self ::clean ($ snippet , $ target );
121+ }
119122
120123 } else {
124+ if ($ target == "view " ) {
125+ $ dirty ->name = string_display_line ($ dirty ->name );
126+ $ dirty ->value = string_display ($ dirty ->value );
127+ } elseif ($ target == "form " ) {
128+ $ dirty ->name = string_attribute ($ dirty ->name );
129+ $ dirty ->value = string_textarea ($ dirty ->value );
130+ }
131+
121132 $ cleaned = new Snippet (
122133 $ dirty ->type ,
123- string_display_line ( $ dirty ->name ) ,
124- string_attribute ( $ dirty ->value ) ,
134+ $ dirty ->name ,
135+ $ dirty ->value ,
125136 $ dirty ->user_id
126137 );
127138 $ cleaned ->id = $ dirty ->id ;
@@ -130,6 +141,34 @@ public static function clean($dirty) {
130141 return $ cleaned ;
131142 }
132143
144+ /**
145+ * Load snippets by ID.
146+ *
147+ * @param mixed Snippet ID (int or array)
148+ * @param int User ID
149+ * @return mixed Snippet(s)
150+ */
151+ public static function load_by_id ($ id , $ user_id ) {
152+ $ snippet_table = plugin_table ("snippet " );
153+
154+ if (is_array ($ id )) {
155+ $ ids = array_filter ($ id , "is_int " );
156+ $ ids = implode (", " , $ ids );
157+
158+ $ query = "SELECT * FROM {$ snippet_table } WHERE id IN ( {$ ids }) AND user_id= " .db_param ();
159+ $ result = db_query_bound ($ query , array ($ user_id ));
160+
161+ return self ::from_db_result ($ result );
162+
163+ } else {
164+ $ query = "SELECT * FROM {$ snippet_table } WHERE id= " .db_param ()." AND user_id= " .db_param ();
165+ $ result = db_query_bound ($ query , array ($ id , $ user_id ));
166+
167+ $ snippets = self ::from_db_result ($ result );
168+ return $ snippets [0 ];
169+ }
170+ }
171+
133172 /**
134173 * Load text objects for a given field type and user id.
135174 *
@@ -169,14 +208,24 @@ public static function load_by_user_id($user_id) {
169208 }
170209
171210 /**
172- * Delete a single text object with the given ID.
211+ * Delete snippets with the given ID.
173212 *
174- * @param int Text ID
213+ * @param mixed Snippet ID (int or array)
175214 */
176- public static function delete_by_id ($ id ) {
215+ public static function delete_by_id ($ id, $ user_id ) {
177216 $ snippet_table = plugin_table ("snippet " );
178- $ query = "DELETE FROM {$ snippet_table } WHERE id= " .db_param ();
179- db_query_bound ($ query , array ($ id ));
217+
218+ if (is_array ($ id )) {
219+ $ ids = array_filter ($ id , "is_int " );
220+ $ ids = implode (", " , $ ids );
221+
222+ $ query = "DELETE FROM {$ snippet_table } WHERE id IN ( {$ ids }) AND user_id= " .db_param ();
223+ db_query_bound ($ query , array ($ user_id ));
224+
225+ } else {
226+ $ query = "DELETE FROM {$ snippet_table } WHERE id= " .db_param ()." AND user_id= " .db_param ();
227+ db_query_bound ($ query , array ($ id , $ user_id ));
228+ }
180229 }
181230
182231 /**
0 commit comments