get_filepath( $filetype, $location ); if ( ! isset( $file['folder_path'], $file['file_name'] ) ) { return false; } if ( ! file_exists( $file['folder_path'] ) ) { return wp_mkdir_p( $file['folder_path'] ); } $filesystem = $this->get_wp_filesystem(); if ( ! $filesystem ) { return false; } $file_path = $this->build_full_path( $file ); if ( ! $filesystem->is_writable( $file['folder_path'] ) && ( ! file_exists( $file_path ) || ! $filesystem->is_writable( $file_path ) ) ) { return false; } if ( $filesystem->is_writable( $file['folder_path'] ) && file_exists( $file_path ) && ! $filesystem->is_writable( $file_path ) ) { return false; } return true; } /** * Write code to an external file and return its versioned URI. * * @param string $code * @param string $filetype * @param string $location * @return string|false URI on success, false on failure. */ public function create_external_file( string $code, string $filetype, string $location ) { if ( ! $code || ! $this->can_write( $filetype, $location ) ) { return false; } $filesystem = $this->get_wp_filesystem(); if ( ! $filesystem ) { return false; } $file = $this->get_filepath( $filetype, $location ); $file_path = $this->build_full_path( $file ); if ( $filesystem->is_writable( $file_path ) || ( ! file_exists( $file_path ) && $filesystem->is_writable( dirname( $file_path ) ) ) ) { $chmod = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : 0644; if ( ! $filesystem->put_contents( $file_path, $code, $chmod ) ) { return false; } if ( ! file_exists( $file_path ) ) { return false; } return $this->get_file_url( $filetype, $location ) . '?ver=' . filemtime( $file_path ); } return false; } /** * Return folder path and file name for a given filetype/location pair. * * @param string $filetype * @param string $location * @return array|false */ public function get_filepath( string $filetype, string $location ) { if ( ! in_array( $filetype, [ 'js', 'css' ], true ) ) { return false; } if ( ! array_key_exists( $location, Custom_Code::ALL_LOCATIONS ) ) { return false; } $upload_dir = wp_get_upload_dir(); $basename = ( 'js' === $filetype ) ? 'script' : 'style'; $folder_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'ao-custom-code'; if ( is_multisite() ) { $folder_path .= DIRECTORY_SEPARATOR . get_current_blog_id(); } return [ 'folder_path' => $folder_path, 'file_name' => $basename . '-' . $location . '.' . $filetype, ]; } /** * Return the public URL to a generated file. * * @param string $filetype * @param string $location * @return string */ public function get_file_url( string $filetype, string $location ): string { $upload_dir = wp_get_upload_dir(); $basename = ( 'js' === $filetype ) ? 'script' : 'style'; $file_name = $basename . '-' . $location . '.' . $filetype; return set_url_scheme( trailingslashit( $upload_dir['baseurl'] ) . 'ao-custom-code/' . $file_name ); } /** * Initialize and return the global WP_Filesystem object. * * @return \WP_Filesystem_Base|null */ public function get_wp_filesystem() { global $wp_filesystem; if ( ! $wp_filesystem ) { require_once ABSPATH . '/wp-admin/includes/file.php'; $credentials = []; $constants = [ 'hostname' => 'FTP_HOST', 'username' => 'FTP_USER', 'password' => 'FTP_PASS', 'public_key' => 'FTP_PUBKEY', 'private_key' => 'FTP_PRIKEY', ]; foreach ( $constants as $key => $constant ) { if ( defined( $constant ) ) { $credentials[ $key ] = constant( $constant ); } } $success = WP_Filesystem( $credentials ); if ( ! $success || $wp_filesystem->errors->has_errors() ) { return null; } } return $wp_filesystem; } /** * Delete a generated external file. * * @param string $filetype * @param string $location * @return bool */ public function delete_file( string $filetype, string $location ): bool { $file = $this->get_filepath( $filetype, $location ); if ( isset( $file['folder_path'], $file['file_name'] ) ) { $file_path = $this->build_full_path( $file ); if ( file_exists( $file_path ) ) { return (bool) wp_delete_file( $file_path ); } } return true; } }