| 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.9 |
| 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_P_VERSION', '1.0.9'); |
| 24 |
define('PWP_PLUGIN_FILE', __FILE__); |
| 25 |
define('PWP_ABSPATH', dirname(__FILE__) . '/'); |
| 26 |
define('PWP_VERSION', get_file_data(__FILE__, ['Version'])[0]); |
| 27 |
|
| 28 |
define('PATTERSWP_PLUGIN_API_URL', 'https://api.lemonsqueezy.com/v1/licenses'); |
| 29 |
|
| 30 |
// Include necessary files. |
| 31 |
require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-admin.php'; |
| 32 |
require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-api.php'; |
| 33 |
require_once PWP_PLUGIN_DIR . 'includes/lib/class-patterns-license.php'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue assets for Block Editor. |
| 37 |
*/ |
| 38 |
function patternswp_enqueue_editor_assets() { |
| 39 |
$get_license_data = get_option('patternswp_plugin_license_data'); |
| 40 |
$is_active = isset($get_license_data['activated']) ? $get_license_data['activated'] : false; |
| 41 |
|
| 42 |
$script_asset = patternswp_get_asset_file('build/patternswp-editor'); |
| 43 |
wp_enqueue_script( |
| 44 |
'patternswp-editor-scripts', |
| 45 |
PWP_PLUGIN_URL . 'build/patternswp-editor.js', |
| 46 |
array_merge($script_asset['dependencies'], ['wp-api']), |
| 47 |
$script_asset['version'], |
| 48 |
true |
| 49 |
); |
| 50 |
|
| 51 |
$patternswp_api_section = new PatternsWP_API_Section(); |
| 52 |
$localize_data = [ |
| 53 |
'isLicenseActive' => $is_active, |
| 54 |
'externalPatterns' => [], |
| 55 |
'patternCategories' => $patternswp_api_section->get_patternswp_category_type(), |
| 56 |
'patternsNonce' => wp_create_nonce('patternswp_nonce'), |
| 57 |
]; |
| 58 |
|
| 59 |
wp_localize_script('patternswp-editor-scripts', 'patternsWpData', $localize_data); |
| 60 |
|
| 61 |
$style_asset = patternswp_get_asset_file('build/block-pattern-inserter-editor-styles'); |
| 62 |
wp_enqueue_style('patternswp-editor-styles', PWP_PLUGIN_URL . 'build/style-patternswp-editor-styles.css', [], $style_asset['version']); |
| 63 |
} |
| 64 |
add_action('enqueue_block_editor_assets', 'patternswp_enqueue_editor_assets'); |
| 65 |
|
| 66 |
/** |
| 67 |
* Get asset file data. |
| 68 |
*/ |
| 69 |
function patternswp_get_asset_file($filepath) { |
| 70 |
$asset_path = PWP_ABSPATH . $filepath . '.asset.php'; |
| 71 |
return file_exists($asset_path) ? require_once $asset_path : ['dependencies' => [], 'version' => PWP_VERSION]; |
| 72 |
} |
| 73 |
|
| 74 |
// Plugin activation hook. |
| 75 |
register_activation_hook(__FILE__, 'patternswp_schedule_hourly_cron'); |
| 76 |
|
| 77 |
function patternswp_schedule_hourly_cron() { |
| 78 |
if ( !wp_next_scheduled( 'patternswp_hourly_transient_load' ) ) { |
| 79 |
wp_schedule_event( time(), 'hourly', 'patternswp_hourly_transient_load' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// Plugin deactivation hook. |
| 84 |
register_deactivation_hook(__FILE__, 'patternswp_remove_hourly_cron'); |
| 85 |
|
| 86 |
function patternswp_remove_hourly_cron() { |
| 87 |
$timestamp = wp_next_scheduled( 'patternswp_hourly_transient_load' ); |
| 88 |
if ( $timestamp ) { |
| 89 |
wp_unschedule_event( $timestamp, 'patternswp_hourly_transient_load' ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* AJAX handler to fetch patterns. |
| 95 |
*/ |
| 96 |
function fetch_patterns_handler() { |
| 97 |
|
| 98 |
// Verify nonce |
| 99 |
if (!isset($_POST['nonce'])) { |
| 100 |
wp_send_json_error('Missing nonce'); |
| 101 |
wp_die(); |
| 102 |
} |
| 103 |
|
| 104 |
$nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); |
| 105 |
|
| 106 |
if (!wp_verify_nonce($nonce, 'patternswp_nonce')) { |
| 107 |
wp_send_json_error('Invalid nonce'); |
| 108 |
wp_die(); |
| 109 |
} |
| 110 |
|
| 111 |
if (!isset($_POST['page']) || !isset($_POST['patternsPerPage'])) { |
| 112 |
wp_send_json_error('Missing parameters'); |
| 113 |
wp_die(); |
| 114 |
} |
| 115 |
|
| 116 |
$page = intval($_POST['page']); |
| 117 |
$patterns_per_page = intval($_POST['patternsPerPage']); |
| 118 |
$search = sanitize_text_field(wp_unslash($_POST['search'] ?? '')); |
| 119 |
$category = sanitize_text_field(wp_unslash($_POST['category'] ?? '')); |
| 120 |
|
| 121 |
$cache_key = 'patternswp_' . md5("$page-$patterns_per_page-$search-$category"); |
| 122 |
$cached_data = get_transient($cache_key); |
| 123 |
|
| 124 |
if ($cached_data !== false) { |
| 125 |
wp_send_json_success($cached_data); |
| 126 |
wp_die(); |
| 127 |
} |
| 128 |
|
| 129 |
$patternswp_api_section = new PatternsWP_API_Section(); |
| 130 |
$localize_data_ajax = $patternswp_api_section->get_patternswp_pattern($page, $patterns_per_page, $search, $category); |
| 131 |
|
| 132 |
if (is_array($localize_data_ajax)) { |
| 133 |
set_transient($cache_key, $localize_data_ajax, 3600); |
| 134 |
wp_send_json_success($localize_data_ajax); |
| 135 |
} else { |
| 136 |
wp_send_json_error('Failed to fetch patterns'); |
| 137 |
} |
| 138 |
|
| 139 |
wp_die(); |
| 140 |
} |
| 141 |
add_action('wp_ajax_fetch_patterns', 'fetch_patterns_handler'); |
| 142 |
add_action('wp_ajax_nopriv_fetch_patterns', 'fetch_patterns_handler'); |
| 143 |
|
| 144 |
/** |
| 145 |
* Add plugin action links. |
| 146 |
*/ |
| 147 |
function patternswp_plugin_action_links($links, $file) { |
| 148 |
if ($file === plugin_basename(__FILE__)) { |
| 149 |
$get_license_data = get_option('patternswp_plugin_license_data'); |
| 150 |
$is_active = isset($get_license_data['activated']) ? $get_license_data['activated'] : false; |
| 151 |
|
| 152 |
$support_link = '<a href="https://thepatternswp.com/contact/" target="_blank">Support</a>'; |
| 153 |
array_unshift($links, $support_link); |
| 154 |
|
| 155 |
if (!$is_active) { |
| 156 |
$upgrade_link = '<a href="https://thepatternswp.com/pricing/" target="_blank">Upgrade to Pro</a>'; |
| 157 |
array_unshift($links, $upgrade_link); |
| 158 |
} |
| 159 |
} |
| 160 |
return $links; |
| 161 |
} |
| 162 |
add_filter('plugin_action_links', 'patternswp_plugin_action_links', 10, 2); |
| 163 |
|
| 164 |
/** |
| 165 |
* Add custom plugin meta links. |
| 166 |
*/ |
| 167 |
function patternswp_add_plugin_meta_links($links, $file) { |
| 168 |
if ($file === plugin_basename(__FILE__)) { |
| 169 |
$links[] = '<a href="https://thepatternswp.com/suggest-feature" target="_blank">Suggest a Feature</a>'; |
| 170 |
$links[] = '<a href="https://wordpress.org/support/plugin/patternswp/reviews/?filter=5" target="_blank">Rate Us</a>'; |
| 171 |
} |
| 172 |
return $links; |
| 173 |
} |
| 174 |
add_filter('plugin_row_meta', 'patternswp_add_plugin_meta_links', 10, 2); |