| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | * Plugin URI: https://thepatternswp.com |
| 5 | 5 | * Description: A growing library of ready-made block patterns can help you build websites faster in no time. |
| 6 | 6 | * Author: PatternsWP |
| 7 | 7 | * Author URI: https://thepatternswp.com |
| 8 | - * Version: 1.0.4 | |
| 8 | + * Version: 1.0.10 | |
| 9 | 9 | * License: GPL-2.0+ |
| 10 | 10 | * License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 11 | 11 | * Text Domain: patternswp |
| 12 | 12 | * Domain Path: /languages |
| @@ -19,81 +19,156 @@ | ||
| 19 | 19 | |
| 20 | 20 | // Define plugin constants. |
| 21 | 21 | define('PWP_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
| 22 | 22 | define('PWP_PLUGIN_URL', plugin_dir_url(__FILE__)); |
| 23 | +define('PWP_P_VERSION', '1.0.10'); | |
| 23 | 24 | define('PWP_PLUGIN_FILE', __FILE__); |
| 24 | 25 | define('PWP_ABSPATH', dirname(__FILE__) . '/'); |
| 25 | 26 | define('PWP_VERSION', get_file_data(__FILE__, ['Version'])[0]); |
| 26 | 27 | |
| 28 | +define('PATTERSWP_PLUGIN_API_URL', 'https://api.lemonsqueezy.com/v1/licenses'); | |
| 29 | + | |
| 27 | 30 | // Include necessary files. |
| 31 | +require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-admin.php'; | |
| 28 | 32 | require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-api.php'; |
| 33 | +require_once PWP_PLUGIN_DIR . 'includes/lib/class-patterns-license.php'; | |
| 29 | 34 | |
| 30 | 35 | /** |
| 31 | 36 | * Enqueue assets for Block Editor. |
| 32 | 37 | */ |
| 33 | 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; | |
| 34 | 41 | |
| 35 | - // Scripts. | |
| 36 | 42 | $script_asset = patternswp_get_asset_file('build/patternswp-editor'); |
| 37 | 43 | 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'], | |
| 44 | + 'patternswp-editor-scripts', | |
| 45 | + PWP_PLUGIN_URL . 'build/patternswp-editor.js', | |
| 46 | + array_merge($script_asset['dependencies'], ['wp-api']), | |
| 47 | + $script_asset['version'], | |
| 42 | 48 | true |
| 43 | 49 | ); |
| 44 | 50 | |
| 45 | - // Styles. | |
| 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 | + | |
| 46 | 61 | $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 | - ); | |
| 62 | + wp_enqueue_style('patternswp-editor-styles', PWP_PLUGIN_URL . 'build/style-patternswp-editor-styles.css', [], $style_asset['version']); | |
| 53 | 63 | } |
| 54 | 64 | add_action('enqueue_block_editor_assets', 'patternswp_enqueue_editor_assets'); |
| 55 | 65 | |
| 56 | 66 | /** |
| 57 | 67 | * Get asset file data. |
| 58 | - * | |
| 59 | - * @param string $filepath The file path. | |
| 60 | - * @return array Asset data. | |
| 61 | 68 | */ |
| 62 | 69 | function patternswp_get_asset_file($filepath) { |
| 63 | 70 | $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 | - ]; | |
| 71 | + return file_exists($asset_path) ? require_once $asset_path : ['dependencies' => [], 'version' => PWP_VERSION]; | |
| 70 | 72 | } |
| 71 | 73 | |
| 72 | 74 | // 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'; | |
| 75 | +register_activation_hook(__FILE__, 'patternswp_schedule_hourly_cron'); | |
| 77 | 76 | |
| 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;"; | |
| 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 | +} | |
| 89 | 82 | |
| 90 | - require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
| 91 | - dbDelta($sql); | |
| 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' ); | |
| 92 | 90 | } |
| 93 | 91 | } |
| 94 | 92 | |
| 95 | -// Plugin deactivation hook. | |
| 96 | -register_deactivation_hook(__FILE__, 'patternswp_plugin_deactivate'); | |
| 97 | -function patternswp_plugin_deactivate() { | |
| 98 | - // Deactivation tasks if any. | |
| 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(); | |
| 99 | 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); | |