From e3901df883d01abe9ea0cb97d0017b73f7c79655 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 4 Apr 2025 16:26:22 +0300 Subject: [PATCH 01/13] Add a filter that allows customizing where generated css is saved --- classes/models/FrmStyle.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index 8a30c62a6a..973befad27 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -391,11 +391,19 @@ public function save_settings() { $this->clear_cache(); - $css = $this->get_css_content( $filename ); + $css = $this->get_css_content( $filename ); + + /** + * @since x.x + * + * @param string $file_path + */ + $file_path = apply_filters( 'frm_css_file_path', FrmAppHelper::plugin_path() . '/css' ); + $create_file = new FrmCreateFile( array( 'file_name' => FrmStylesController::get_file_name(), - 'new_file_path' => FrmAppHelper::plugin_path() . '/css', + 'new_file_path' => $file_path, ) ); $create_file->create_file( $css ); From 051dfb5b4b0340c985cf9a09569a195ecd373a01 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 10:34:19 +0300 Subject: [PATCH 02/13] Change how filter works --- classes/models/FrmStyle.php | 49 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index 973befad27..8bff06f293 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -393,23 +393,50 @@ public function save_settings() { $css = $this->get_css_content( $filename ); + $create_file = new FrmCreateFile( self::get_create_style_file_args() ); + $create_file->create_file( $css ); + + update_option( 'frmpro_css', $css, 'no' ); + set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS ); + } + + /** + * @since x.x + * + * @return array + */ + private function get_create_style_file_args() { + $create_file_args = array( + 'file_name' => FrmStylesController::get_file_name(), + ); + /** * @since x.x * - * @param string $file_path + * @param bool $add_css_to_uploads_dir */ - $file_path = apply_filters( 'frm_css_file_path', FrmAppHelper::plugin_path() . '/css' ); + $add_css_to_uploads_dir = apply_filters( 'frm_add_css_to_uploads_dir', false ); + + if ( $add_css_to_uploads_dir ) { + $generated_css_location = self::target_css_uploads_dir(); + $path_args = array( + 'new_file_path' => $generated_css_location, + 'folder_name' => 'formidable/css', + ); + } else { + $path_args = array( + 'new_file_path' => self::target_css_plugin_dir(), + ); + } + return array_merge( $create_file_args, $path_args ); + } - $create_file = new FrmCreateFile( - array( - 'file_name' => FrmStylesController::get_file_name(), - 'new_file_path' => $file_path, - ) - ); - $create_file->create_file( $css ); + private static function target_css_uploads_dir() { + return wp_upload_dir()['basedir'] . '/formidable/css'; + } - update_option( 'frmpro_css', $css, 'no' ); - set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS ); + private static function target_css_plugin_dir() { + return FrmAppHelper::plugin_path() . '/css'; } /** From 410d0becd18e95d8ee31d31ffad8cad4dbcf107c Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:12:47 +0300 Subject: [PATCH 03/13] Make generated css file url work based on filter --- classes/controllers/FrmStylesController.php | 12 +++-- classes/models/FrmStyle.php | 50 ++++++++++++++------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index 5d59564816..1026dc7403 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -228,9 +228,15 @@ public static function custom_stylesheet() { * @return void */ private static function get_url_to_custom_style( &$stylesheet_urls ) { - $file_name = '/css/' . self::get_file_name(); - if ( is_readable( FrmAppHelper::plugin_path() . $file_name ) ) { - $url = FrmAppHelper::plugin_url() . $file_name; + $add_css_to_uploads_dir = FrmStyle::add_css_to_uploads_dir(); + $file_path = FrmStyle::get_generated_css_file_path( $add_css_to_uploads_dir ) . '/' . self::get_file_name(); + if ( is_readable( $file_path ) ) { + if ( $add_css_to_uploads_dir ) { + $base_url = wp_upload_dir()['baseurl'] . '/formidable/css/'; + } else { + $base_url = FrmAppHelper::plugin_url() . '/css/'; + } + $url = $base_url . self::get_file_name(); } else { $url = admin_url( 'admin-ajax.php?action=frmpro_css' ); } diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index 8bff06f293..ee809e6850 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -406,29 +406,47 @@ public function save_settings() { * @return array */ private function get_create_style_file_args() { - $create_file_args = array( - 'file_name' => FrmStylesController::get_file_name(), + $add_css_to_uploads_dir = self::add_css_to_uploads_dir(); + $create_file_args = array( + 'file_name' => FrmStylesController::get_file_name(), + 'new_file_path' => self::get_generated_css_file_path( $add_css_to_uploads_dir ), ); + if ( ! $add_css_to_uploads_dir ) { + return $create_file_args; + } + $path_args = array( + 'folder_name' => 'formidable/css', + ); + return array_merge( + $create_file_args, + array( + 'folder_name' => 'formidable/css', + ) + ); + } + + /** + * @since x.x + * + * @param bool $add_css_to_uploads_dir + * @return string + */ + public static function get_generated_css_file_path( $add_css_to_uploads_dir ) { + if ( self::add_css_to_uploads_dir() ) { + return self::target_css_uploads_dir(); + } + return self::target_css_plugin_dir(); + } + + public static function add_css_to_uploads_dir() { + /** * @since x.x * * @param bool $add_css_to_uploads_dir */ - $add_css_to_uploads_dir = apply_filters( 'frm_add_css_to_uploads_dir', false ); - - if ( $add_css_to_uploads_dir ) { - $generated_css_location = self::target_css_uploads_dir(); - $path_args = array( - 'new_file_path' => $generated_css_location, - 'folder_name' => 'formidable/css', - ); - } else { - $path_args = array( - 'new_file_path' => self::target_css_plugin_dir(), - ); - } - return array_merge( $create_file_args, $path_args ); + return apply_filters( 'frm_add_css_to_uploads_dir', false ); } private static function target_css_uploads_dir() { From 5c79be10661b713fc2b71fa0250705b74710cbc6 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:23:08 +0300 Subject: [PATCH 04/13] Add comment to functions --- classes/models/FrmStyle.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index ee809e6850..dde0fbea3b 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -405,7 +405,7 @@ public function save_settings() { * * @return array */ - private function get_create_style_file_args() { + private static function get_create_style_file_args() { $add_css_to_uploads_dir = self::add_css_to_uploads_dir(); $create_file_args = array( 'file_name' => FrmStylesController::get_file_name(), @@ -433,12 +433,17 @@ private function get_create_style_file_args() { * @return string */ public static function get_generated_css_file_path( $add_css_to_uploads_dir ) { - if ( self::add_css_to_uploads_dir() ) { + if ( $add_css_to_uploads_dir ) { return self::target_css_uploads_dir(); } return self::target_css_plugin_dir(); } + /** + * @since x.x + * + * @return bool + */ public static function add_css_to_uploads_dir() { /** @@ -449,10 +454,20 @@ public static function add_css_to_uploads_dir() { return apply_filters( 'frm_add_css_to_uploads_dir', false ); } + /** + * @since x.x + * + * @return string + */ private static function target_css_uploads_dir() { return wp_upload_dir()['basedir'] . '/formidable/css'; } + /** + * @since x.x + * + * @return string + */ private static function target_css_plugin_dir() { return FrmAppHelper::plugin_path() . '/css'; } From ccd49f7bdce0b35705bbd59ea07e927a4e5ad927 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:25:32 +0300 Subject: [PATCH 05/13] Undo some extra changes --- classes/models/FrmStyle.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index dde0fbea3b..bdd7e76e5e 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -391,8 +391,7 @@ public function save_settings() { $this->clear_cache(); - $css = $this->get_css_content( $filename ); - + $css = $this->get_css_content( $filename ); $create_file = new FrmCreateFile( self::get_create_style_file_args() ); $create_file->create_file( $css ); From 60515b131292818fdd9c7d21cd5b9fdc60e705ca Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:36:24 +0300 Subject: [PATCH 06/13] Refactor and improve function clarity --- classes/controllers/FrmStylesController.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index 1026dc7403..54234d2a8a 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -230,17 +230,13 @@ public static function custom_stylesheet() { private static function get_url_to_custom_style( &$stylesheet_urls ) { $add_css_to_uploads_dir = FrmStyle::add_css_to_uploads_dir(); $file_path = FrmStyle::get_generated_css_file_path( $add_css_to_uploads_dir ) . '/' . self::get_file_name(); - if ( is_readable( $file_path ) ) { - if ( $add_css_to_uploads_dir ) { - $base_url = wp_upload_dir()['baseurl'] . '/formidable/css/'; - } else { - $base_url = FrmAppHelper::plugin_url() . '/css/'; - } - $url = $base_url . self::get_file_name(); - } else { - $url = admin_url( 'admin-ajax.php?action=frmpro_css' ); + if ( ! is_readable( $file_path ) ) { + $stylesheet_urls['formidable'] = admin_url( 'admin-ajax.php?action=frmpro_css' ); + return; } - $stylesheet_urls['formidable'] = $url; + $base_url = $add_css_to_uploads_dir ? wp_upload_dir()['baseurl'] . '/formidable/css/' : FrmAppHelper::plugin_url() . '/css/'; + + $stylesheet_urls['formidable'] = $base_url . self::get_file_name(); } /** From 6a9eefb5063a3481ca2ae2bd3e0cf31bbe1e5a09 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:43:35 +0300 Subject: [PATCH 07/13] Add a check for plugin's writability to default to uploads dir for generated css location --- classes/models/FrmStyle.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index bdd7e76e5e..87e81380d2 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -444,13 +444,12 @@ public static function get_generated_css_file_path( $add_css_to_uploads_dir ) { * @return bool */ public static function add_css_to_uploads_dir() { - /** * @since x.x * * @param bool $add_css_to_uploads_dir */ - return apply_filters( 'frm_add_css_to_uploads_dir', false ); + return apply_filters( 'frm_add_css_to_uploads_dir', ! wp_is_file_mod_allowed() ); } /** From 29fe00e67cd768d5dfe34b83d01fce180cf2898f Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:44:59 +0300 Subject: [PATCH 08/13] Add function short description --- classes/models/FrmStyle.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index 87e81380d2..e9a4d83b58 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -439,6 +439,8 @@ public static function get_generated_css_file_path( $add_css_to_uploads_dir ) { } /** + * Returns true if generated css file should be saved in the uploads directory. + * * @since x.x * * @return bool From 56c08fa7d62b3204f1f4269af4c88ff0248b66bd Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 8 Apr 2025 10:23:04 +0300 Subject: [PATCH 09/13] Add context to wp_is_file_mod_allowed --- classes/models/FrmStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index e9a4d83b58..f024681b9c 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -451,7 +451,7 @@ public static function add_css_to_uploads_dir() { * * @param bool $add_css_to_uploads_dir */ - return apply_filters( 'frm_add_css_to_uploads_dir', ! wp_is_file_mod_allowed() ); + return apply_filters( 'frm_add_css_to_uploads_dir', ! wp_is_file_mod_allowed( 'frm_can_install_plugin' ) ); } /** From dbbe4f88d1d604d3e00a78a0b7cde5d6ffec79e8 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 9 Apr 2025 11:18:29 +0300 Subject: [PATCH 10/13] Remove useless variable assignment --- classes/models/FrmStyle.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index f024681b9c..5706c8cfef 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -414,9 +414,6 @@ private static function get_create_style_file_args() { if ( ! $add_css_to_uploads_dir ) { return $create_file_args; } - $path_args = array( - 'folder_name' => 'formidable/css', - ); return array_merge( $create_file_args, array( From ac454105f8a12e8b370d0d48c2efaec6a9436aad Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 9 Apr 2025 11:24:29 +0300 Subject: [PATCH 11/13] Remove extra space on a return statement line --- classes/models/FrmStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index 5706c8cfef..ea35336cc4 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -457,7 +457,7 @@ public static function add_css_to_uploads_dir() { * @return string */ private static function target_css_uploads_dir() { - return wp_upload_dir()['basedir'] . '/formidable/css'; + return wp_upload_dir()['basedir'] . '/formidable/css'; } /** From f3e831d921c26d00c1aa760f420bddaa2c4d13d5 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 6 May 2025 09:43:35 +0300 Subject: [PATCH 12/13] Simplify code a bit --- classes/models/FrmStyle.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index ea35336cc4..c1e3f79533 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -411,15 +411,10 @@ private static function get_create_style_file_args() { 'new_file_path' => self::get_generated_css_file_path( $add_css_to_uploads_dir ), ); - if ( ! $add_css_to_uploads_dir ) { - return $create_file_args; + if ( $add_css_to_uploads_dir ) { + $create_file_args['folder_name'] = 'formidable/css'; } - return array_merge( - $create_file_args, - array( - 'folder_name' => 'formidable/css', - ) - ); + return $create_file_args; } /** From ca4f27ffde1533370e7210c8ede97c218a716634 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 7 May 2025 09:38:08 +0300 Subject: [PATCH 13/13] Rename context used for wp_is_file_mod_allowed --- classes/models/FrmStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index c1e3f79533..e1a7add56c 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -443,7 +443,7 @@ public static function add_css_to_uploads_dir() { * * @param bool $add_css_to_uploads_dir */ - return apply_filters( 'frm_add_css_to_uploads_dir', ! wp_is_file_mod_allowed( 'frm_can_install_plugin' ) ); + return apply_filters( 'frm_add_css_to_uploads_dir', ! wp_is_file_mod_allowed( 'frm_save_css_to_plugin_folder' ) ); } /**