Skip to content

Commit

Permalink
Add description of post types and post statuses on the Converter page.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Feb 10, 2024
1 parent 7be47d6 commit a8afd34
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 105 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Yes, you can!
= 6.0.7 (10.02.2024) =
* Tested with WooCommerce 8.5.
* Added redirect from the cyrillic post title when creating a new post.
* Added description of post types and post statuses on the Converter page.
* Fixed displaying all file descriptions in the Theme Editor in the current locale.

= 6.0.6 (14.01.2024) =
Expand Down
16 changes: 16 additions & 0 deletions src/php/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,28 @@ public function init() {
* @return void
*/
public function init_all() {
$this->load_textdomain();

$this->init_multilingual();
$this->init_classes();
$this->init_cli();
$this->init_hooks();
}

/**
* Load plugin text domain.
*
* @return void
*/
public function load_textdomain() {
load_default_textdomain();
load_plugin_textdomain(
'cyr2lat',
false,
dirname( plugin_basename( constant( 'CYR_TO_LAT_FILE' ) ) ) . '/languages/'
);
}

/**
* Init multilingual features.
* It must be first in the init sequence, as we use defined filters internally in our classes.
Expand Down
3 changes: 0 additions & 3 deletions src/php/Requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public function __construct( Settings $settings, AdminNotices $admin_notices, WP
*/
global $wp_file_descriptions;

$admin_locale = get_locale();
load_default_textdomain( $admin_locale );

require_once ABSPATH . 'wp-admin/includes/file.php';
}
// @codeCoverageIgnoreEnd
Expand Down
97 changes: 51 additions & 46 deletions src/php/Settings/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,6 @@ protected function init_hooks() {
public function init_form_fields() {
$this->form_fields = [];

$default_post_types = [ 'post', 'page', 'nav_menu_item' ];

$post_types = $default_post_types;

$filtered_post_types = array_filter( (array) apply_filters( 'ctl_post_types', $post_types ) );

$this->form_fields['background_post_types'] = [
'label' => __( 'Post Types', 'cyr2lat' ),
'section' => self::SECTION_TYPES_STATUSES,
'type' => 'checkbox',
'placeholder' => '',
'helper' => __( 'Post types included in the conversion.', 'cyr2lat' ),
'supplemental' => '',
'options' => [],
];

foreach ( $post_types as $post_type ) {
$label = $post_type;

$this->form_fields['background_post_types']['options'][ $post_type ] = $label;
}

$this->form_fields['background_post_types']['default'] = $default_post_types;
$this->form_fields['background_post_types']['disabled'] = array_diff( $default_post_types, $filtered_post_types );

$default_post_statuses = [ 'publish', 'future', 'private' ];
$post_statuses = [ 'publish', 'future', 'private', 'draft', 'pending' ];

$this->form_fields['background_post_statuses'] = [
'label' => __( 'Post Statuses', 'cyr2lat' ),
'section' => self::SECTION_TYPES_STATUSES,
Expand All @@ -103,46 +75,79 @@ public function init_form_fields() {
'options' => [],
];

foreach ( $post_statuses as $post_status ) {
$label = $post_status;

$this->form_fields['background_post_statuses']['options'][ $post_status ] = $label;
$post_status_objects = get_post_stati( [ 'internal' => false ], 'objects' );
$post_stati = array_keys( $post_status_objects );
$default_post_statuses = array_intersect( $post_stati, [ 'publish', 'future', 'private' ] );
$array_flip = array_flip( $default_post_statuses );
$post_status_objects =
array_intersect_key( $post_status_objects, $array_flip ) +
array_diff_key( $post_status_objects, $array_flip );

foreach ( $post_status_objects as $post_status => $post_status_object ) {
$this->form_fields['background_post_statuses']['options'][ $post_status ] =
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
__( $post_status_object->label ) . ' (' . $post_status . ')';
}

$this->form_fields['background_post_statuses']['default'] = $default_post_statuses;
}

/**
* Get convertible post type objects.
*
* @return array
*/
private static function get_get_convertible_post_type_objects(): array {
return get_post_types(
[
'public' => true,
'name' => 'nav_menu_item',
],
'objects',
'or'
);
}

/**
* Get convertible post types.
*
* @return array
*/
public static function get_convertible_post_types(): array {
$post_types = get_post_types( [ 'public' => true ] );

return array_merge( $post_types, [ 'nav_menu_item' => 'nav_menu_item' ] );
return array_keys( self::get_get_convertible_post_type_objects() );
}

/**
* Init form fields.
*/
public function delayed_init_form_fields() {
$post_types = self::get_convertible_post_types();
$this->form_fields['background_post_types'] = [
'label' => __( 'Post Types', 'cyr2lat' ),
'section' => self::SECTION_TYPES_STATUSES,
'type' => 'checkbox',
'placeholder' => '',
'helper' => __( 'Post types included in the conversion.', 'cyr2lat' ),
'supplemental' => '',
'options' => [],
];

$post_type_objects = self::get_get_convertible_post_type_objects();
$post_types = array_keys( $post_type_objects );
$default_post_types = array_intersect( $post_types, [ 'post', 'page', 'nav_menu_item' ] );
$array_flip = array_flip( $default_post_types );
$post_type_objects =
array_intersect_key( $post_type_objects, $array_flip ) +
array_diff_key( $post_type_objects, $array_flip );
$filtered_post_types = array_filter( (array) apply_filters( 'ctl_post_types', $post_types ) );

$this->form_fields['background_post_types']['options'] = [];

foreach ( $post_types as $post_type ) {
$label = $post_type;

$this->form_fields['background_post_types']['options'][ $post_type ] = $label;
foreach ( $post_type_objects as $post_type => $post_type_object ) {
$this->form_fields['background_post_types']['options'][ $post_type ] =
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
__( $post_type_object->label ) . ' (' . $post_type . ')';
}

$this->form_fields['background_post_types']['disabled'] = array_diff(
$this->form_fields['background_post_types']['default'],
$filtered_post_types
);
$this->form_fields['background_post_types']['default'] = $default_post_types;
$this->form_fields['background_post_types']['disabled'] = array_diff( $default_post_types, $filtered_post_types );
}

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function test_init() {
* @return void
*/
public function test_init_all() {
$load_textdomain = 'load_textdomain';
$init_multilingual = 'init_multilingual';
$init_classes = 'init_classes';
$init_cli = 'init_cli';
Expand All @@ -85,6 +86,7 @@ public function test_init_all() {
$subject = Mockery::mock( Main::class )->makePartial();

$subject->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( $load_textdomain )->once();
$subject->shouldReceive( $init_multilingual )->once();
$subject->shouldReceive( $init_classes )->once();
$subject->shouldReceive( $init_cli )->once();
Expand All @@ -93,6 +95,32 @@ public function test_init_all() {
$subject->init_all();
}

/**
* Test load_textdomain().
*
* @return void
*/
public function test_load_textdomain() {
$plugin_file = '/var/www/wp-content/plugins/cyr2lat/cyr-to-lat.php';
$plugin_base_name = 'cyr2lat/cyr-to-lat.php';

FunctionMocker::replace(
'constant',
static function ( $name ) use ( $plugin_file ) {
return $name === 'CYR_TO_LAT_FILE' ? $plugin_file : '';
}
);

WP_Mock::userFunction( 'plugin_basename' )->with( $plugin_file )->once()->andReturn( $plugin_base_name );
WP_Mock::userFunction( 'load_default_textdomain' )->with()->once();
WP_Mock::userFunction( 'load_plugin_textdomain' )
->with( 'cyr2lat', false, 'cyr2lat/languages/' )
->once();
$subject = Mockery::mock( Main::class )->makePartial();

$subject->load_textdomain();
}

/**
* Test init_multilingual.
*
Expand Down Expand Up @@ -416,6 +444,21 @@ public function test_init_hooks_when_not_allowed() {
$subject->$method();
}

/**
* Test settings().
*
* @throws ReflectionException ReflectionException.
*/
public function test_settings() {
$settings = Mockery::mock( Settings::class );

$subject = Mockery::mock( Main::class )->makePartial();

$this->set_protected_property( $subject, 'settings', $settings );

self::assertSame( $settings, $subject->settings() );
}

/**
* Test that sanitize_title() does nothing when title is empty.
*/
Expand Down
Loading

0 comments on commit a8afd34

Please sign in to comment.