blogarc_plugin_api_status( $plugin_slug ); if ( is_wp_error( $api ) ) { $status['message'] = $api->get_error_message(); wp_send_json_error( $status ); } $status['pluginName'] = $api->name; $skin = new WP_Ajax_Upgrader_Skin(); $upgrader = new Plugin_Upgrader( $skin ); $result = $upgrader->install( $api->download_link ); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $status['debug'] = $skin->get_upgrade_messages(); } if ( is_wp_error( $result ) ) { $status['errorCode'] = $result->get_error_code(); $status['message'] = $result->get_error_message(); wp_send_json_error( $status ); } elseif ( is_wp_error( $skin->result ) ) { $status['errorCode'] = $skin->result->get_error_code(); $status['message'] = $skin->result->get_error_message(); wp_send_json_error( $status ); } elseif ( $skin->get_errors()->get_error_code() ) { $status['message'] = $skin->get_error_messages(); wp_send_json_error( $status ); } elseif ( is_null( $result ) ) { global $wp_filesystem; $status['errorCode'] = 'unable_to_connect_to_filesystem'; $status['message'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'blogarc' ); // Pass through the error from WP_Filesystem if one was raised. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { $status['message'] = esc_html( $wp_filesystem->errors->get_error_message() ); } wp_send_json_error( $status ); } if ( current_user_can( 'activate_plugin' ) ) { $plugin_path = '/'.esc_attr( $plugin_slug ).'/'.esc_attr( $plugin_slug ).'.php'; $result = activate_plugin( $plugin_path ); if ( is_wp_error( $result ) ) { $status['errorCode'] = $result->get_error_code(); $status['message'] = $result->get_error_message(); wp_send_json_error( $status ); } } $status['message'] = esc_html__( 'Plugin installed successfully', 'blogarc' ); wp_send_json_success( $status ); } function blogarc_activate_free_plugin() { $plugin_slug = $_POST['plugin_slug']; if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'blogarc_plugin_install_nonce' ) ) { die( 'This action was stopped for security purposes.' ); } if ( ! current_user_can( 'activate_plugin' ) ) { $status['message'] = __( 'Sorry, you are not allowed to activate plugins on this site.', 'blogarc' ); wp_send_json_error( $status ); } $plugin_path = '/'.esc_attr( $plugin_slug ).'/'.esc_attr( $plugin_slug ).'.php'; $result = activate_plugin( $plugin_path ); if ( is_wp_error( $result ) ) { $status['errorCode'] = $result->get_error_code(); $status['message'] = $result->get_error_message(); wp_send_json_error( $status ); } $status['message'] = esc_html__( 'Plugin activated successfully', 'blogarc' ); wp_send_json_success( $status ); } /** * Complete info about lists of free plugins */ public function blogarc_free_plugins_lists() { // Use a transient to cache plugin info for 12 hours // $cached_plugins_info = get_transient('blogarc_plugins_info'); // if ( $cached_plugins_info ) { // return $cached_plugins_info; // } $free_plugins = array( 'contact-form-7' => array( 'slug' => 'contact-form-7', 'filename' => 'contact-form-7.php', ), ); $plugins_info = array(); foreach ( $free_plugins as $plugin ) { // Check transient for each plugin data to avoid repetitive API calls $cache_key = 'blogarc_plugin_' . $plugin['slug']; $plugin_api_data = get_transient( $cache_key ); if ( ! $plugin_api_data ) { // Fetch plugin information from the API $api_status = $this->blogarc_plugin_api_status( $plugin['slug'] ); if ( is_wp_error( $api_status ) ) { continue; } $plugin_api_data = array( 'name' => $api_status->name, 'slug' => $plugin['slug'], 'version' => $api_status->version, 'author' => $api_status->author, 'icon_url' => $this->blogarc_get_plugin_icon_url( $api_status->icons ), 'description' => $api_status->short_description, ); // Cache individual plugin data for 24 hours set_transient( $cache_key, $plugin_api_data, DAY_IN_SECONDS ); } // Determine action status and button URL $action_status = $this->blogarc_check_plugin_status( $plugin ); $button_url = $this->blogarc_generate_plugin_action_url( $action_status, $plugin ); $plugin_api_data['action'] = $action_status; $plugin_api_data['button_url'] = $button_url; $plugins_info[] = $plugin_api_data; } // Cache the final plugins info for 12 hours // set_transient('blogarc_plugins_info', $plugins_info, 12 * HOUR_IN_SECONDS); return $plugins_info; } /** * get icon for requested plugin */ public function blogarc_get_plugin_icon_url( $arr ) { $plugin_icon_url = ''; if ( ! empty( $arr['svg'] ) ) { $plugin_icon_url = $arr['svg']; } elseif ( ! empty( $arr['2x'] ) ) { $plugin_icon_url = $arr['2x']; } elseif ( ! empty( $arr['1x'] ) ) { $plugin_icon_url = $arr['1x']; } else { $plugin_icon_url = $arr['default']; } return $plugin_icon_url; } /** * Get requested plugin API */ public function blogarc_plugin_api_status( $plugin ) { include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $plugin_info_api = plugins_api( 'plugin_information', array( 'slug' => $plugin, 'fields' => array( 'sections' => false, 'icons' => true, 'short_description' => true, 'banners' => true, ) ) ); return $plugin_info_api; } /** * Check if Plugin is active or not */ public function blogarc_check_plugin_status( $plugin ) { include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); $folder_name = $plugin['slug']; $file_name = $plugin['filename']; $status = 'install'; $path = WP_PLUGIN_DIR.'/'.esc_attr( $folder_name ).'/'.esc_attr( $file_name ); if ( file_exists( $path ) ) { $status = is_plugin_active( esc_attr( $folder_name ).'/'.esc_attr( $file_name ) ) ? 'inactive' : 'active'; } return $status; } /** * Get plugin path based on plugin slug. * * @param string $slug - plugin slug. * * @return string */ public function blogarc_get_plugin_path( $slug ) { switch ( $slug ) { case 'contact-form-7': return $slug . '/contact-form-7.php'; default: return $slug . '/' . $slug . '.php'; } } /** * Generate Url for the Plugin Button */ public function blogarc_generate_plugin_action_url( $status, $plugin ) { $plugin_slug = $plugin['slug']; switch ( $status ) { case 'install': return wp_nonce_url( add_query_arg( array( 'action' => 'install-plugin', 'plugin' => $plugin_slug, ), esc_url( network_admin_url( 'update.php' ) ) ), 'install-plugin_' . $plugin_slug ); break; case 'inactive': return add_query_arg( array( 'action' => 'deactivate', 'plugin' => rawurlencode( $this->blogarc_get_plugin_path( $plugin_slug ) ), 'plugin_status' => 'all', 'paged' => '1', '_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $this->blogarc_get_plugin_path( $plugin_slug ) ), ), esc_url( network_admin_url( 'plugins.php' ) ) ); break; case 'active': return add_query_arg( array( 'action' => 'activate', 'plugin' => rawurlencode( $this->blogarc_get_plugin_path( $plugin_slug ) ), 'plugin_status' => 'all', 'paged' => '1', '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $this->blogarc_get_plugin_path( $plugin_slug ) ), ), esc_url( network_admin_url( 'plugins.php' ) ) ); break; } } /** * Get the parsed changelog. * * @param string $changelog_path the changelog path. * * @return array */ public function get_changelog( $changelog_path ) { if ( ! is_file( $changelog_path ) ) { return []; } if ( ! WP_Filesystem() ) { return []; } return $this->parse_changelog( $changelog_path ); } /** * Return the releases changes array. * * @param string $changelog_path the changelog path. * * @return array $releases - changelog. */ private function parse_changelog( $changelog_path ) { WP_Filesystem(); global $wp_filesystem; $changelog = $wp_filesystem->get_contents( $changelog_path ); if ( is_wp_error( $changelog ) ) { $changelog = ''; } $changelog = explode( PHP_EOL, $changelog ); $releases = []; $release_count = 0; foreach ( $changelog as $changelog_line ) { if ( empty( $changelog_line ) ) { continue; } if (substr(ltrim($changelog_line), 0, 2) === '==') { $release_count++; preg_match('/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $changelog_line, $found_v); preg_match('/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $changelog_line, $found_d); $version = $found_v[0] ?? null; $date = $found_d[0] ?? null; if ($version && $date) { $releases[$release_count] = array( 'version' => $version, 'date' => $date, ); } else { error_log("Invalid changelog line: $changelog_line"); } continue; } if ( preg_match( '/[*|-]?\s?(\NEW|\New|new)[:]?\s?(\b|(?=\[))/', $changelog_line ) ) { $changelog_line = preg_replace( '/[*|-]?\s?(\NEW|\New|new)[:]?\s?(\b|(?=\[))/', '', $changelog_line ); $releases[ $release_count ]['new'][] = $this->blogarc_parse_md_and_clean( $changelog_line ); continue; } if ( preg_match( '/[*|-]?\s?(IMP|Imp|imp)[:]?\s?(\b|(?=\[))/', $changelog_line ) ) { $changelog_line = preg_replace( '/[*|-]?\s?(IMP|Imp|imp)[:]?\s?(\b|(?=\[))/', '', $changelog_line ); $releases[ $release_count ]['imp'][] = $this->blogarc_parse_md_and_clean( $changelog_line ); continue; } if ( preg_match( '/[*|-]?\s?(FIX|Fix|fix)[:]?\s?(\b|(?=\[))/', $changelog_line ) ) { $changelog_line = preg_replace( '/[*|-]?\s?(FIX|Fix|fix)[:]?\s?(\b|(?=\[))/', '', $changelog_line ); $releases[ $release_count ]['fix'][] = $this->blogarc_parse_md_and_clean( $changelog_line ); continue; } $changelog_line = $this->blogarc_parse_md_and_clean( $changelog_line ); if ( empty( $changelog_line ) ) { continue; } $releases[ $release_count ]['tweak'][] = $changelog_line; } return array_values( $releases ); } /** * Parse markdown links and cleanup string. * * @param string $string changelog line. * * @return string */ private function blogarc_parse_md_and_clean( $string ) { // Drop spaces, starting lines | asterisks. $string = trim( $string ); $string = ltrim( $string, '*' ); $string = ltrim( $string, '-' ); // Replace markdown links with tags. $string = preg_replace_callback( '/\[(.*?)]\((.*?)\)/', function ( $matches ) { return '' . $matches[1] . ''; }, htmlspecialchars( $string ) ); return $string; } } $admin_main_class =new Blogarc_Admin_Main(); endif;