| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; |
| 3 |
|
| 4 |
if ( ! imagify_can_optimize_custom_folders() ) { |
| 5 |
return; |
| 6 |
} |
| 7 |
|
| 8 |
// Get folders, remove excluded ones, sort them, add labels. |
| 9 |
$custom_folders = Imagify_Folders_DB::get_instance()->get_active_folders_column( 'path' ); |
| 10 |
$themes = []; |
| 11 |
|
| 12 |
if ( $custom_folders ) { |
| 13 |
$custom_folders = array_combine( $custom_folders, $custom_folders ); |
| 14 |
$custom_folders = array_map( [ 'Imagify_Files_Scan', 'remove_placeholder' ], $custom_folders ); |
| 15 |
$custom_folders = array_map( 'trailingslashit', $custom_folders ); |
| 16 |
$custom_folders = array_filter( $custom_folders, [ 'Imagify_Files_Scan', 'is_path_autorized' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
if ( $custom_folders ) { |
| 20 |
$custom_folders = array_map( [ $this->filesystem, 'make_path_relative' ], $custom_folders ); |
| 21 |
$custom_folders = array_map( 'untrailingslashit', $custom_folders ); |
| 22 |
natcasesort( $custom_folders ); |
| 23 |
$custom_folders = array_map( 'trailingslashit', $custom_folders ); |
| 24 |
|
| 25 |
if ( isset( $custom_folders['{{ROOT}}/'] ) ) { |
| 26 |
$custom_folders['{{ROOT}}/'] = __( 'Site\'s root', 'imagify' ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// Current used theme(s). |
| 31 |
if ( ! is_network_admin() ) { |
| 32 |
$current_theme = wp_get_theme(); |
| 33 |
$themes_not_added = []; |
| 34 |
|
| 35 |
foreach ( [ $current_theme, $current_theme->parent() ] as $theme ) { |
| 36 |
if ( ! $theme || ! $theme->exists() ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$theme_path = trailingslashit( $theme->get_stylesheet_directory() ); |
| 41 |
|
| 42 |
if ( ! Imagify_Files_Scan::is_path_forbidden( $theme_path ) ) { |
| 43 |
$theme = [ |
| 44 |
'name' => $theme->display( 'Name' ), |
| 45 |
'path' => Imagify_Files_Scan::add_placeholder( $theme_path ), |
| 46 |
'label' => $this->filesystem->make_path_relative( $theme_path ), |
| 47 |
]; |
| 48 |
|
| 49 |
$themes[ $theme['path'] ] = $theme; |
| 50 |
$added = false; |
| 51 |
$rel_path = strtolower( $theme['label'] ); |
| 52 |
|
| 53 |
foreach ( $custom_folders as $path_name => $label ) { |
| 54 |
if ( strpos( $rel_path, strtolower( $label ) ) === 0 ) { |
| 55 |
$added = true; |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! $added ) { |
| 61 |
$themes_not_added[] = $theme['path']; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$themes_count = count( $themes ); |
| 67 |
} |
| 68 |
?> |
| 69 |
<div class="imagify-custom-folders-section"> |
| 70 |
<div class="imagify-col" id="custom-folders"> |
| 71 |
<h3 class="imagify-options-subtitle"><?php esc_html_e( 'Custom Folders', 'imagify' ); ?></h3> |
| 72 |
|
| 73 |
<div id="imagify-custom-folders" class="hide-if-no-js imagify-mt2"> |
| 74 |
|
| 75 |
<?php if ( $themes ) { ?> |
| 76 |
<div class="imagify-folder-themes-suggestion"> |
| 77 |
<div class="imagify-fts-header imagify-flex imagify-vcenter"> |
| 78 |
<span><i class="dashicons dashicons-info"></i></span> |
| 79 |
<p> |
| 80 |
<?php |
| 81 |
$theme = reset( $themes ); |
| 82 |
|
| 83 |
/* translators: %s is a theme name. */ |
| 84 |
printf( esc_html__( 'You’re using %s', 'imagify' ), '<strong>' . esc_html( $theme['name'] ) . '</strong>' ); |
| 85 |
|
| 86 |
if ( $themes_count > 1 ) { |
| 87 |
$theme = end( $themes ); |
| 88 |
echo '<br>'; |
| 89 |
/* translators: %s is a theme name. */ |
| 90 |
printf( esc_html__( 'child theme of %s', 'imagify' ), '<strong>' . esc_html( $theme['name'] ) . '</strong>' ); |
| 91 |
} |
| 92 |
?> |
| 93 |
</p> |
| 94 |
</div> |
| 95 |
|
| 96 |
<div class="imagify-fts-content"> |
| 97 |
<?php |
| 98 |
if ( ! $themes_not_added ) { |
| 99 |
?> |
| 100 |
<p class="imagify-mb0"><?php echo esc_html( _n( 'Your theme is already in the optimization process. All Good!', 'Your themes are already in the optimization process. All Good!', $themes_count, 'imagify' ) ); ?></p> |
| 101 |
<?php |
| 102 |
} elseif ( count( $themes_not_added ) !== $themes_count ) { |
| 103 |
$theme = reset( $themes_not_added ); |
| 104 |
$theme = $themes[ $theme ]; |
| 105 |
?> |
| 106 |
<p><?php esc_html_e( 'Only one of your current themes is in the optimization process, would you like to also optimize the other one?', 'imagify' ); ?></p> |
| 107 |
|
| 108 |
<button id="imagify-add-themes-to-custom-folder" |
| 109 |
class="button imagify-button-clean imagify-add-themes" type="button" |
| 110 |
data-theme="<?php echo esc_attr( $theme['path'] ) . '#///#' . esc_attr( $theme['label'] ); ?>"> |
| 111 |
<span class="dashicons dashicons-plus"></span> |
| 112 |
<span class="button-text"> |
| 113 |
<?php |
| 114 |
/* translators: %s is a theme name. */ |
| 115 |
printf( esc_html__( 'Add %s to optimization', 'imagify' ), '<strong>' . esc_html( $theme['name'] ) . '</strong>' ); |
| 116 |
?> |
| 117 |
</span> |
| 118 |
</button> |
| 119 |
<?php |
| 120 |
} else { |
| 121 |
foreach ( $themes as $path_name => $theme ) { |
| 122 |
$themes[ $path_name ] = esc_attr( $theme['path'] ) . '#///#' . esc_attr( $theme['label'] ); |
| 123 |
} |
| 124 |
?> |
| 125 |
<p><?php echo esc_html( _n( 'Would you like to optimize your theme?', 'Would you like to optimize your themes?', $themes_count, 'imagify' ) ); ?></p> |
| 126 |
|
| 127 |
<button id="imagify-add-themes-to-custom-folder" |
| 128 |
class="button imagify-button-clean imagify-add-themes" type="button" |
| 129 |
data-theme="<?php echo implode( '" data-theme-parent="', $themes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"> |
| 130 |
<span class="dashicons dashicons-plus"></span> |
| 131 |
<span |
| 132 |
class="button-text"><?php echo esc_html( _n( 'Add the theme to optimization', 'Add the themes to optimization', $themes_count, 'imagify' ) ); ?></span> |
| 133 |
</button> |
| 134 |
<?php |
| 135 |
} |
| 136 |
?> |
| 137 |
</div> |
| 138 |
</div> |
| 139 |
<?php } ?> |
| 140 |
|
| 141 |
<p class="imagify-kindof-title imagify-flex imagify-vcenter"> |
| 142 |
<span><?php esc_html_e( 'Select folders for optimization.', 'imagify' ); ?></span> |
| 143 |
<span> |
| 144 |
<button id="imagify-add-custom-folder" |
| 145 |
class="button imagify-button-mini imagify-button-primary imagify-add-custom-folder" |
| 146 |
type="button"> |
| 147 |
<span class="dashicons dashicons-plus"></span> |
| 148 |
<span class="button-text"><?php esc_html_e( 'Add folders', 'imagify' ); ?></span> |
| 149 |
</button> |
| 150 |
<img class="imagify-loader" aria-hidden="true" alt="<?php esc_attr_e( 'Loading...', 'imagify' ); ?>" src="<?php echo esc_url( IMAGIFY_ASSETS_IMG_URL . 'loader-balls.svg' ); ?>" width="38" height="24"/> |
| 151 |
</span> |
| 152 |
</p> |
| 153 |
|
| 154 |
<fieldset id="imagify-custom-folders-selected"> |
| 155 |
<?php |
| 156 |
if ( $custom_folders ) { |
| 157 |
foreach ( $custom_folders as $placeholder => $label ) { |
| 158 |
$this->print_template( |
| 159 |
'part-settings-row-custom-folder', |
| 160 |
[ |
| 161 |
'value' => $placeholder, |
| 162 |
'label' => $label, |
| 163 |
] |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
?> |
| 168 |
</fieldset> |
| 169 |
|
| 170 |
<p> |
| 171 |
<?php |
| 172 |
printf( |
| 173 |
/* translators: 1 and 2 are <strong> opening and closing tags. */ |
| 174 |
esc_html__( '%1$sSelecting a folder will also optimize images in sub-folders.%2$s The only exception is "Site’s root": when selected, only images that are directly at the site’s root will be optimized (sub-folders can be selected separately).', 'imagify' ), |
| 175 |
'<strong>', |
| 176 |
'</strong>' |
| 177 |
); |
| 178 |
?> |
| 179 |
<br/> |
| 180 |
<?php esc_html_e( 'Folders that are hidden in the folder selector window are excluded and will not be optimized even if a parent folder is selected.', 'imagify' ); ?> |
| 181 |
</p> |
| 182 |
|
| 183 |
<p class="imagify-success hidden"><?php esc_html_e( 'You changed your custom folder settings, don\'t forget to save your changes!', 'imagify' ); ?></p> |
| 184 |
|
| 185 |
<script type="text/html" id="tmpl-imagify-custom-folder"> |
| 186 |
<?php |
| 187 |
$this->print_template( |
| 188 |
'part-settings-row-custom-folder', |
| 189 |
[ |
| 190 |
'value' => '{{ data.value }}', |
| 191 |
'label' => '{{ data.label }}', |
| 192 |
] |
| 193 |
); |
| 194 |
?> |
| 195 |
</script> |
| 196 |
</div> |
| 197 |
</div> |
| 198 |
</div> |
| 199 |
|
| 200 |
|