Plugin Directory

Changeset 3243448


Ignore:
Timestamp:
02/19/2025 06:55:30 PM (12 months ago)
Author:
MrViSiOn
Message:

1.0.45

  • Auto login improvements.
Location:
multi-connect
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • multi-connect/trunk/.editorconfig

    r3217196 r3243448  
    44trim_trailing_whitespace = true
    55indent_size = 4
    6 indent_style = space
     6indent_style = tab
    77insert_final_newline = true
  • multi-connect/trunk/includes/class-multi-connect-api.php

    r3243412 r3243448  
    2323     */
    2424    private string $namespace = 'wp/v2';
     25    private int $token_length = 32;
     26    private int $token_expiry = 300;
    2527
    2628    /**
     
    131133            )
    132134        );
     135
     136        register_rest_route(
     137            $this->namespace,
     138            '/generate-login-token',
     139            array(
     140                'methods'             => 'POST',
     141                'callback'            => [ $this, 'generate_login_token' ],
     142                'permission_callback' => [ $this, 'check_authentication' ],
     143            )
     144        );
     145
     146        register_rest_route(
     147            $this->namespace,
     148            '/auto-login',
     149            array(
     150                'methods'             => 'GET',
     151                'callback'            => [ $this, 'process_auto_login' ],
     152                'permission_callback' => '__return_true',
     153            )
     154        );
    133155    }
    134156
     
    141163        global $wpdb;
    142164
    143     // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
     165        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    144166        $tables      = $wpdb->get_results( 'SHOW TABLES', ARRAY_N );
    145167        $table_names = array_map( fn( $table ) => $table[0], $tables );
     
    157179     *
    158180     * @param WP_REST_Request $request The request object.
     181     *
    159182     * @return WP_REST_Response The response object.
    160183     *
     
    191214                            break;
    192215                        case 'core':
    193                             $selected_folders = array_merge( $selected_folders, glob( MULTICONNECT_PLUGIN_ABSPATH . 'wp-*.php' ) );
     216                            $selected_folders = array_merge( $selected_folders,
     217                                glob( MULTICONNECT_PLUGIN_ABSPATH . 'wp-*.php' ) );
    194218                            break;
    195219                    }
     
    267291     *
    268292     * @param string $zip_file The zip file path.
    269      * @param array  $folders The folders to backup.
     293     * @param array $folders The folders to backup.
    270294     * @param string $log_file The log file path.
     295     *
    271296     * @return int The number of files added.
    272297     *
     
    289314                $relative_path = substr( $path, $base_length );
    290315                if ( $zip->addFile( $path, $relative_path ) ) {
    291                     ++$files_added;
     316                    ++ $files_added;
    292317                } else {
    293318                    $this->log( $log_file, "Failed to add file: $path" );
     
    308333
    309334                    if ( $zip->addFile( $file_path, $relative_path ) ) {
    310                         ++$files_added;
     335                        ++ $files_added;
    311336                    } else {
    312337                        $this->log( $log_file, "Failed to add file: $file_path" );
     
    328353     *
    329354     * @param string $zip_file The zip file path.
    330      * @param array  $tables The tables to backup.
     355     * @param array $tables The tables to backup.
    331356     * @param string $log_file The log file path.
     357     *
    332358     * @return bool True if the database backup is successful, false otherwise.
    333359     *
     
    336362    private function create_database_backup( $zip_file, $tables, $log_file ) {
    337363        $db_backup = $this->backup_database( $tables );
    338         // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
    339         $this->log('', 'backup_database: ' . file_get_contents($db_backup));
     364        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
     365        $this->log( '', 'backup_database: ' . file_get_contents( $db_backup ) );
    340366        if ( ! $db_backup ) {
    341367            $this->log( $log_file, 'Failed to create database dump' );
     
    371397     * @param string $log_file The log file path.
    372398     * @param string $message The message to log.
     399     *
    373400     * @return void
    374401     */
     
    376403        if ( empty( $log_file ) ) {
    377404            $backup_dir = wp_upload_dir()['basedir'] . '/backups';
    378             $log_file = $backup_dir . '/backup.log';
    379         }
    380         $enable_logging = get_option('multi_connect_log');
    381         if ($enable_logging !== '1') {
    382             return;
     405            $log_file   = $backup_dir . '/backup.log';
     406        }
     407        $enable_logging = get_option( 'multi_connect_log' );
     408        if ( $enable_logging !== '1' ) {
     409            return;
    383410        }
    384411        $timestamp   = gmdate( 'Y-m-d H:i:s' );
     
    389416            WP_Filesystem();
    390417        }
    391         $existing_content = $wp_filesystem->get_contents($log_file) ?: '';
     418        $existing_content = $wp_filesystem->get_contents( $log_file ) ?: '';
    392419        $wp_filesystem->put_contents( $log_file, $existing_content . $log_message, FS_CHMOD_FILE );
    393420    }
     
    397424     *
    398425     * @param array $tables_name The tables to backup.
     426     *
    399427     * @return string The backup file path.
    400428     */
     
    429457            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    430458            $create_table = $wpdb->get_row(
    431                 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange
    432                 sprintf('SHOW CREATE TABLE %s', "`{$table_name}`"),
     459            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange
     460                sprintf( 'SHOW CREATE TABLE %s', "`{$table_name}`" ),
    433461                ARRAY_N
    434462            );
    435             $content .= "DROP TABLE IF EXISTS `{$table_name}`;\n";
    436             $content .= $create_table[1] . ";\n\n";
     463            $content      .= "DROP TABLE IF EXISTS `{$table_name}`;\n";
     464            $content      .= $create_table[1] . ";\n\n";
    437465
    438466            // Datos de la tabla.
    439467            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    440468            $rows = $wpdb->get_results(
    441                 sprintf('SELECT * FROM %s', "`{$table_name}`"),
     469                sprintf( 'SELECT * FROM %s', "`{$table_name}`" ),
    442470                ARRAY_A
    443471            );
     
    448476                    foreach ( $chunk as $row ) {
    449477                        $values[] = '(' . implode(
    450                             ',',
    451                             array_map(
    452                                 function ( $value ) use ( $wpdb ) {
    453                                     return is_null( $value ) ? 'NULL' : "'" . $wpdb->_real_escape( $value ) . "'";
    454                                 },
    455                                 $row
    456                             )
    457                         ) . ')';
     478                                ',',
     479                                array_map(
     480                                    function ( $value ) use ( $wpdb ) {
     481                                        return is_null( $value ) ? 'NULL' : "'" . $wpdb->_real_escape( $value ) . "'";
     482                                    },
     483                                    $row
     484                                )
     485                            ) . ')';
    458486                    }
    459487
     
    532560    public function check_update_permission() {
    533561        return current_user_can( 'update_core' ) &&
    534                 current_user_can( 'update_plugins' ) &&
    535                 current_user_can( 'update_themes' );
     562               current_user_can( 'update_plugins' ) &&
     563               current_user_can( 'update_themes' );
    536564    }
    537565
     
    545573
    546574        // Forzar la verificación de actualizaciones.
    547         delete_site_transient('update_core');
    548         delete_site_transient('update_plugins');
    549         delete_site_transient('update_themes');
    550         delete_site_transient('available_translations');
    551 
    552         // Limpiar la caché de actualizaciones
    553         wp_clean_update_cache();
    554 
    555         // Forzar la verificación de actualizaciones con tiempo actual
    556         wp_version_check(array(), true);
    557         wp_update_plugins();
    558         wp_update_themes();
     575        delete_site_transient( 'update_core' );
     576        delete_site_transient( 'update_plugins' );
     577        delete_site_transient( 'update_themes' );
     578        delete_site_transient( 'available_translations' );
     579
     580        // Limpiar la caché de actualizaciones
     581        wp_clean_update_cache();
     582
     583        // Forzar la verificación de actualizaciones con tiempo actual
     584        wp_version_check( array(), true );
     585        wp_update_plugins();
     586        wp_update_themes();
    559587
    560588
     
    577605
    578606        // Verificar actualizaciones de plugins.
    579         get_plugin_updates();
    580         try {
    581             $transient = (object)[
    582                 'last_checked' => time() - (13 * 3600),
    583                 'checked' => ['default' => 'none'],
    584                 'response' => ['default' => 'none'],
    585             ];
    586             apply_filters("pre_set_site_transient_update_plugins", $transient, "update_plugins");
    587         } catch (\Throwable $e) {
    588             $transient;
    589         }
    590         $plugins = get_site_transient( 'update_plugins' );
    591         if ( $plugins && ! empty( $plugins->response ) ) {
     607        get_plugin_updates();
     608        try {
     609            $transient = (object) [
     610                'last_checked' => time() - ( 13 * 3600 ),
     611                'checked'      => [ 'default' => 'none' ],
     612                'response'     => [ 'default' => 'none' ],
     613            ];
     614            apply_filters( "pre_set_site_transient_update_plugins", $transient, "update_plugins" );
     615        } catch ( Throwable $e ) {
     616            $transient;
     617        }
     618        $plugins = get_site_transient( 'update_plugins' );
     619        if ( $plugins && ! empty( $plugins->response ) ) {
    592620            foreach ( $plugins->response as $plugin_file => $plugin_data ) {
    593621                $plugin_data          = (array) $plugin_data;
     
    604632        // Verificar actualizaciones de temas.
    605633        get_theme_updates();
    606         try {
    607             $transient = (object)[
    608                 'last_checked' => time() - (13 * 3600),
    609                 'checked' => ['default' => 'none'],
    610                 'response' => ['default' => 'none'],
    611             ];
    612             $updatesFromHook = apply_filters("pre_set_site_transient_update_themes", $transient, "update_themes");
    613         } catch (\Throwable $e) {
    614             $updatesFromHook = $transient;
    615         }
    616         $themes = get_site_transient( 'update_themes' );
    617         if ( $themes && ! empty( $themes->response ) ) {
     634        try {
     635            $transient       = (object) [
     636                'last_checked' => time() - ( 13 * 3600 ),
     637                'checked'      => [ 'default' => 'none' ],
     638                'response'     => [ 'default' => 'none' ],
     639            ];
     640            $updatesFromHook = apply_filters( "pre_set_site_transient_update_themes", $transient, "update_themes" );
     641        } catch ( Throwable $e ) {
     642            $updatesFromHook = $transient;
     643        }
     644        $themes = get_site_transient( 'update_themes' );
     645        if ( $themes && ! empty( $themes->response ) ) {
    618646            foreach ( $themes->response as $theme_slug => $theme_data ) {
    619647                $theme               = wp_get_theme( $theme_slug );
     
    725753     *
    726754     * @param WP_REST_Request $request The request object.
    727      * @param string          $type The type of translations to update.
    728      * @param string          $slug The slug of the translations to update.
     755     * @param string $type The type of translations to update.
     756     * @param string $slug The slug of the translations to update.
     757     *
    729758     * @return WP_REST_Response The response object.
    730759     */
     
    802831        ob_end_clean();
    803832
    804         $failed_translations = $skin->getErrors();
     833        $failed_translations  = $skin->getErrors();
    805834        $updated_translations = $skin->getSuccess();
    806835
     
    834863     *
    835864     * @param WP_REST_Request $request The request object.
     865     *
    836866     * @return WP_REST_Response The response object.
    837867     */
     
    916946     *
    917947     * @param WP_REST_Request $request The request object.
     948     *
    918949     * @return WP_REST_Response The response object.
    919950     */
     
    10251056        WP_Filesystem();
    10261057    }
     1058
     1059    public function check_authentication($request) {
     1060        // Verificar la autenticación por Application Password
     1061        return current_user_can('administrator');
     1062    }
     1063
     1064    public function generate_login_token($request) {
     1065        // Generar token único
     1066        $token = wp_generate_password($this->token_length, false);
     1067
     1068        // Guardar el token en las opciones de WordPress con tiempo de expiración
     1069        $token_data = [
     1070            'token' => $token,
     1071            'expires' => time() + $this->token_expiry,
     1072            'user_id' => get_current_user_id()
     1073        ];
     1074
     1075        set_transient('multi_wp_login_token_' . $token, $token_data, $this->token_expiry);
     1076
     1077        return rest_ensure_response(['token' => $token]);
     1078    }
     1079
     1080    public function process_auto_login($request) {
     1081        $token = $request->get_param('token');
     1082
     1083        if (!$token) {
     1084            wp_redirect(wp_login_url());
     1085            exit;
     1086        }
     1087
     1088        $token_data = get_transient('multi_wp_login_token_' . $token);
     1089
     1090        if (!$token_data || time() > $token_data['expires']) {
     1091            wp_redirect(wp_login_url());
     1092            exit;
     1093        }
     1094
     1095        // Eliminar el token usado
     1096        delete_transient('multi_wp_login_token_' . $token);
     1097
     1098        // Realizar el login
     1099        $user = get_user_by('id', $token_data['user_id']);
     1100        if ($user) {
     1101            wp_set_current_user($user->ID);
     1102            wp_set_auth_cookie($user->ID);
     1103
     1104            // Redirigir al admin
     1105            wp_redirect(admin_url());
     1106            exit;
     1107        }
     1108
     1109        wp_redirect(wp_login_url());
     1110        exit;
     1111    }
    10271112}
  • multi-connect/trunk/multi-connect.php

    r3243412 r3243448  
    44 * Plugin URI: https://multi-wp.com
    55 * Description: Connect your WordPress site with Multi-WP for centralized management and updates.
    6  * Version: 1.0.44
     6 * Version: 1.0.45
    77 * Requires at least: 5.8
    88 * Requires PHP: 7.4
  • multi-connect/trunk/readme.txt

    r3243412 r3243448  
    55Tested up to: 6.7
    66Requires PHP: 7.4
    7 Stable tag: 1.0.44
     7Stable tag: 1.0.45
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    4444
    4545== Changelog ==
     46
     47= 1.0.45 =
     48* Auto login improvements.
    4649
    4750= 1.0.44 =
Note: See TracChangeset for help on using the changeset viewer.