| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: PatternsWP |
| 4 |
* Plugin URI: https://thepatternswp.com |
| 5 |
* Description: A growing library of ready-made block patterns can help you build websites faster in no time. |
| 6 |
* Author: PatternsWP |
| 7 |
* Author URI: https://thepatternswp.com |
| 8 |
* Version: 1.0.4 |
| 9 |
* License: GPL-2.0+ |
| 10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 11 |
* Text Domain: patternswp |
| 12 |
* Domain Path: /languages |
| 13 |
*/ |
| 14 |
|
| 15 |
// If this file is called directly, abort. |
| 16 |
if (!defined('WPINC')) { |
| 17 |
die; |
| 18 |
} |
| 19 |
|
| 20 |
// Define plugin constants. |
| 21 |
define('PWP_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
| 22 |
define('PWP_PLUGIN_URL', plugin_dir_url(__FILE__)); |
| 23 |
define('PWP_PLUGIN_FILE', __FILE__); |
| 24 |
define('PWP_ABSPATH', dirname(__FILE__) . '/'); |
| 25 |
define('PWP_VERSION', get_file_data(__FILE__, ['Version'])[0]); |
| 26 |
|
| 27 |
// Include necessary files. |
| 28 |
require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-api.php'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Enqueue assets for Block Editor. |
| 32 |
*/ |
| 33 |
function patternswp_enqueue_editor_assets() { |
| 34 |
|
| 35 |
// Scripts. |
| 36 |
$script_asset = patternswp_get_asset_file('build/patternswp-editor'); |
| 37 |
wp_enqueue_script( |
| 38 |
'patternswp-editor-scripts', |
| 39 |
PWP_PLUGIN_URL . 'build/patternswp-editor.js', |
| 40 |
array_merge($script_asset['dependencies'], ['wp-api']), |
| 41 |
$script_asset['version'], |
| 42 |
true |
| 43 |
); |
| 44 |
|
| 45 |
// Styles. |
| 46 |
$style_asset = patternswp_get_asset_file('build/block-pattern-inserter-editor-styles'); |
| 47 |
wp_enqueue_style( |
| 48 |
'patternswp-editor-styles', |
| 49 |
PWP_PLUGIN_URL . 'build/style-patternswp-editor-styles.css', |
| 50 |
array(), |
| 51 |
$style_asset['version'] |
| 52 |
); |
| 53 |
} |
| 54 |
add_action('enqueue_block_editor_assets', 'patternswp_enqueue_editor_assets'); |
| 55 |
|
| 56 |
/** |
| 57 |
* Get asset file data. |
| 58 |
* |
| 59 |
* @param string $filepath The file path. |
| 60 |
* @return array Asset data. |
| 61 |
*/ |
| 62 |
function patternswp_get_asset_file($filepath) { |
| 63 |
$asset_path = PWP_ABSPATH . $filepath . '.asset.php'; |
| 64 |
return file_exists($asset_path) |
| 65 |
? require_once $asset_path |
| 66 |
: [ |
| 67 |
'dependencies' => [], |
| 68 |
'version' => PWP_VERSION, |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
// Plugin activation hook. |
| 73 |
register_activation_hook(__FILE__, 'patternswp_plugin_activate'); |
| 74 |
function patternswp_plugin_activate() { |
| 75 |
global $wpdb; |
| 76 |
$table_name = $wpdb->prefix . 'patternswp_patterns'; |
| 77 |
|
| 78 |
// Check if the table already exists. |
| 79 |
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { |
| 80 |
// Table does not exist, so create it. |
| 81 |
$charset_collate = $wpdb->get_charset_collate(); |
| 82 |
$sql = "CREATE TABLE $table_name ( |
| 83 |
id INT NOT NULL AUTO_INCREMENT, |
| 84 |
title VARCHAR(255), |
| 85 |
categories VARCHAR(100), |
| 86 |
content LONGTEXT, |
| 87 |
PRIMARY KEY (id) |
| 88 |
) $charset_collate;"; |
| 89 |
|
| 90 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 91 |
dbDelta($sql); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Plugin deactivation hook. |
| 96 |
register_deactivation_hook(__FILE__, 'patternswp_plugin_deactivate'); |
| 97 |
function patternswp_plugin_deactivate() { |
| 98 |
// Deactivation tasks if any. |
| 99 |
} |
| 100 |
|