/** * Deletes a custom code snippet when it is deleted from WordPress. * * @since 1.0.0 * * @param int $post_id The current post ID */ public function delete($post_id) { /** Next we check to see if the snippet has been applied before - if so, overwrite it */ $delimiter = '|'; $expression = '/**##** Snippet ID ' . $post_id . ' **##**/'; $regex = $delimiter . preg_quote($expression, $delimiter) . '(.*?)' . preg_quote($expression, $delimiter) . $delimiter . 's'; /** Grab our contents - we need to check and see if we already have a snippet */ $handle = @fopen(Cwwp_Init_Snippets::get_snippets_file_path(), 'ab+'); $contents = @fread($handle, filesize(Cwwp_Init_Snippets::get_snippets_file_path())); /** If the snippet exists, overwrite it with our updated code */ if (preg_match($regex, $contents, $matches)) { $contents = str_replace($matches[0], '', $contents); @ftruncate($handle, 0); @fwrite($handle, stripslashes(trim($contents))); @fclose($handle); } }
/** * Returns the custom snippets directory path. * * @since 1.0.0 * * @return string The custom snippets directory path */ public function get_snippets_directory_path() { $uploads_dir = wp_upload_dir(); $snippets_dir = explode('/', untrailingslashit(dirname(plugin_basename(__FILE__)))); return self::$snippets_dir = trailingslashit($uploads_dir['basedir']) . array_pop($snippets_dir); }