Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add actions to LangInterface #97

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Actions added to `LangInterface`:
- ubb_post_language_set
- ubb_post_language_change
- ubb_post_source_set
- ubb_post_source_delete
- ubb_term_language_set
- ubb_term_language_change
- ubb_term_source_set
- ubb_term_source_delete
- Check for empty post ubb_source and delete it to prevent weird cases.

### Fixed

- Change null check to empty check in `LangInterface::get_post_translations` for $source_id to prevent errors from empty ubb_source's.

## [0.4.5] - 2024-07-18

### Added
Expand Down
153 changes: 141 additions & 12 deletions lib/LangInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public static function is_post_type_translatable( string $post_type ) : bool {
* If the language is already set, nothing will happen and it will return `false`. Use the $force
* argument to force the language change.
*
* @since Unreleased Added `ubb_post_language_set` action.
* @since 0.0.1
*
* @param int $post_id The post that language is being set for.
* @param string $language Language being set for post.
* @param bool $force Whether to force the language set. If false (default) and the
Expand All @@ -198,12 +201,13 @@ public static function set_post_language( int $post_id, string $language, bool $
return false;
}

$old_language = self::get_post_language( $post_id );

if ( ! $force ) {
$existing_language = self::get_post_language( $post_id );
if ( $existing_language === $language ) {
if ( $old_language === $language ) {
return true;
}
if ( $existing_language !== null ) {
if ( $old_language !== null ) {
return false;
}
}
Expand All @@ -218,6 +222,19 @@ public static function set_post_language( int $post_id, string $language, bool $

if ( is_int( $inserted ) ) {
\delete_transient( sprintf( 'ubb_%s_post_language', $post_id ) );

/**
* Fires after a post's language is set.
*
* @since Unreleased
*
* @param int $post_id ID of the post.
* @param string $language Language code of the post.
* @param ?string $old_language Old language of the post.
* @param bool $force Whether the language set was forced.
*/
do_action( 'ubb_post_language_set', $post_id, $language, $force, $old_language );

return true;
}

Expand Down Expand Up @@ -267,6 +284,7 @@ public static function get_post_language( int $post_id ) : ?string {
*
* A post's source, via a meta entry, is what links it to other posts (translations).
*
* @since Unreleased Added `ubb_post_source_set` action.
* @since 0.0.1
*
* @param int $post_id Post to set source for.
Expand All @@ -276,8 +294,10 @@ public static function get_post_language( int $post_id ) : ?string {
* @return bool True if source was set, false otherwise or if meta insert/update failed.
*/
public static function set_post_source( int $post_id, string $source_id, bool $force = false ) : bool {
$previous_source = self::get_post_source( $post_id );

if ( $force ) {
if ( $source_id === self::get_post_source( $post_id ) ) {
if ( $source_id === $previous_source ) {
return true;
}
$meta_id = update_post_meta( $post_id, 'ubb_source', $source_id );
Expand All @@ -292,6 +312,18 @@ public static function set_post_source( int $post_id, string $source_id, bool $f

// Update transient for post source.
\set_transient( sprintf( 'ubb_%s_post_source', $post_id ), $source_id, 30 );

/**
* Fires after a post's source is set.
*
* @since Unreleased
*
* @param int $post_id ID of the post.
* @param string $source_id Source ID of the post.
* @param ?string $previous_source Previous source ID of the post.
* @param bool $force Whether the source set was forced.
*/
do_action( 'ubb_post_source_set', $post_id, $source_id, $previous_source, $force );
}

return (bool) $meta_id;
Expand All @@ -300,6 +332,7 @@ public static function set_post_source( int $post_id, string $source_id, bool $f
/**
* Returns the post's source ID.
*
* @since Unreleased Delete empty string `ubb_source`'s from the DB.
* @since 0.0.1
*
* @param int $post_id ID of the post to get source for.
Expand All @@ -309,11 +342,25 @@ public static function get_post_source( int $post_id ) : ?string {
$transient_key = sprintf( 'ubb_%s_post_source', $post_id );
$source_id = \get_transient( $transient_key );
if ( $source_id !== false ) {
return is_string( $source_id ) ? $source_id: null;
return ( is_string( $source_id ) && ! empty( $source_id ) ) ? $source_id : null;
}

$source_id = get_post_meta( $post_id, 'ubb_source', true );
$source_id = empty( $source_id ) ? null : $source_id;

/**
* If the source_id is an empty string, delete it and return null.
*
* `get_post_meta` returns empty string when post id doesn't exist, so we are unable to
* know if there is an actual empty string in the DB or not, so we try to delete it
* either way.
*/
if ( is_string( $source_id ) && empty( $source_id ) ) {
\delete_post_meta( $post_id, 'ubb_source' );
$source_id = null;

} else {
$source_id = empty( $source_id ) ? null : $source_id;
}

\set_transient( $transient_key, $source_id, 30 );

Expand Down Expand Up @@ -342,6 +389,7 @@ public static function get_post_translation( int $post_id, string $lang ) : ?int
/**
* Returns a post's translations.
*
* @since Unreleased Change null check to empty check for $source_id to prevent errors from empty ubb_source's.
* @since 0.0.1
*
* @param int $post_id ID of post to get translatiosn for.
Expand All @@ -354,7 +402,7 @@ public static function get_post_translation( int $post_id, string $lang ) : ?int
*/
public static function get_post_translations( int $post_id ) : array {
$source_id = self::get_post_source( $post_id );
if ( $source_id === null ) {
if ( empty( $source_id ) ) {
return [];
}

Expand All @@ -379,6 +427,7 @@ public static function get_post_translations( int $post_id ) : array {
* `ubb_change_language_post_meta_translate_keys` filter, more in-depth description on the
* filter documentation.
*
* @since Unreleased Add `ubb_post_language_change` action.
* @since 0.0.1
*
* @param int $post_id ID of the post being changed.
Expand Down Expand Up @@ -420,6 +469,19 @@ public static function change_post_language( int $post_id, string $lang ) : bool

\delete_transient( sprintf( 'ubb_%s_post_language', $post_id ) );

/**
* Fires after a post's language is changed.
*
* TODO: should this be the same action as the one in `set_post_language`?
*
* @since Unreleased
*
* @param int $post_id ID of the post.
* @param string $lang New language of the post.
* @param ?string $old_lang Old language of the post.
*/
do_action( 'ubb_post_language_change', $post_id, $lang, $old_lang );

// Update Terms.

// Filter needed since system still thinks its in the previous language.
Expand Down Expand Up @@ -468,6 +530,7 @@ public static function change_post_language( int $post_id, string $lang ) : bool
$meta_keys_to_translate = apply_filters( 'ubb_change_language_post_meta_translate_keys', $default_meta, $post_id, $lang, $old_lang );
if ( ! self::translate_post_meta( $post_id, $lang, $meta_keys_to_translate ) ) {
// TODO: Failure state
// FIXME: language is changed but meta is not, what to do and return?
return false;
}

Expand Down Expand Up @@ -545,6 +608,7 @@ public static function get_new_post_source_id() : string {
*
* Unlinks post from its translations.
*
* @since Unreleased Added `ubb_post_source_delete` action.
* @since 0.0.1
*
* @param string $post_id ID of the post to delete source.
Expand All @@ -561,6 +625,15 @@ public static function delete_post_source( string $post_id ) : bool {
// Delete transient for source.
\delete_transient( sprintf( 'ubb_%s_post_source', $post_id ) );

/**
* Fires before a post's source is deleted.
*
* @since Unreleased
*
* @param int $post_id ID of the post.
* @param string $post_source Source ID of the post.
*/
do_action( 'ubb_post_source_delete', $post_id, $post_source );

return delete_post_meta( $post_id, 'ubb_source' );
}
Expand Down Expand Up @@ -595,6 +668,9 @@ public static function is_taxonomy_translatable( string $taxonomy ) : bool {
* If the language is already set, nothing will happen and it will return `false`. Use the $force
* argument to force the language change.
*
* @since Unreleased Added `ubb_term_language_set` action.
* @since 0.0.1
*
* @param int $term_id The term that language is being set for.
* @param string $language Language being set for term.
* @param bool $force Whether to force the language set. If false (default) and the
Expand All @@ -609,9 +685,9 @@ public static function set_term_language( int $term_id, string $language, bool $
return false;
}

$old_language = self::get_term_language( $term_id );
if ( ! $force ) {
$existing_language = self::get_term_language( $term_id );
if ( $existing_language !== null ) {
if ( $old_language !== null ) {
return false;
}
}
Expand All @@ -626,6 +702,19 @@ public static function set_term_language( int $term_id, string $language, bool $

if ( is_int( $inserted ) ) {
\delete_transient( sprintf( 'ubb_%s_term_language', $term_id ) );

/**
* Fires after a term's language is set.
*
* @since Unreleased
*
* @param int $term_id ID of the term.
* @param string $language Language code of the term.
* @param ?string $old_language Old language of the term.
* @param bool $force Whether the language set was forced.
*/
do_action( 'ubb_term_language_set', $term_id, $language, $force, $old_language );

return true;
}

Expand Down Expand Up @@ -664,6 +753,7 @@ public static function get_term_language( int $term_id ) : ?string {
*
* A term's source, via a meta entry, is what links it to other terms (translations).
*
* @since Unreleased Added `ubb_term_source_set` action.
* @since 0.0.1
*
* @param int $term_id Post to set source for.
Expand All @@ -673,8 +763,9 @@ public static function get_term_language( int $term_id ) : ?string {
* @return bool True if source was set, false otherwise or if meta insert/update failed.
*/
public static function set_term_source( int $term_id, string $source_id, bool $force = false ) : bool {
$previous_source = self::get_term_source( $term_id );
if ( $force ) {
if ( $source_id === self::get_term_source( $term_id ) ) {
if ( $source_id === $previous_source ) {
return true;
}
$meta_id = update_term_meta( $term_id, 'ubb_source', $source_id );
Expand All @@ -689,6 +780,18 @@ public static function set_term_source( int $term_id, string $source_id, bool $f

// Update transient for term source.
\set_transient( sprintf( 'ubb_%s_term_source', $term_id ), $source_id, 30 );

/**
* Fires after a term's source is set.
*
* @since Unreleased
*
* @param int $term_id ID of the term.
* @param string $source_id Source ID of the term.
* @param ?string $previous_source Previous source ID of the term.
* @param bool $force Whether the source set was forced.
*/
do_action( 'ubb_term_source_set', $term_id, $source_id, $previous_source, $force );
}

return (bool) $meta_id;
Expand Down Expand Up @@ -782,6 +885,7 @@ public static function get_term_translations( int $term_id ) : array {
*
* TODO: What to do when term language changes? How to handle post relationships?
*
* @since Unreleased Added `ubb_term_language_change` action.
* @since 0.0.1
*
* @param int $term_id ID of the term being changed.
Expand All @@ -791,10 +895,11 @@ public static function get_term_translations( int $term_id ) : array {
public static function change_term_language( int $term_id, string $lang ) : bool {
global $wpdb;

$old_lang = self::get_term_language( $term_id );
if (
empty( $lang )
|| ! self::is_language_allowed( $lang )
|| $lang === self::get_term_language( $term_id )
|| $lang === $old_lang
) {
return false;
}
Expand All @@ -816,6 +921,19 @@ public static function change_term_language( int $term_id, string $lang ) : bool

\delete_transient( sprintf( 'ubb_%s_term_language', $term_id ) );

/**
* Fires after a term's language is changed.
*
* TODO: should this be the same action as the one in `set_term_language`?
*
* @since Unreleased
*
* @param int $term_id ID of the term.
* @param string $lang New language of the term.
* @param ?string $old_lang Old language of the term.
*/
do_action( 'ubb_term_language_change', $term_id, $lang, $old_lang );

// TODO: Update posts?
return true;
}
Expand Down Expand Up @@ -898,6 +1016,7 @@ public static function get_new_term_source_id() : string {
*
* Unlinks term from its translations.
*
* @since Unreleased Added `ubb_term_source_delete` action.
* @since 0.0.1
*
* @param string $term_id ID of the term to delete source.
Expand All @@ -906,14 +1025,24 @@ public static function get_new_term_source_id() : string {
public static function delete_term_source( string $term_id ) : bool {

// Delete transient for translations.
$term_source = self::get_term_source( $term_id );
$term_source = self::get_term_source( $term_id );
if ( ! $term_source ) {
\delete_transient( sprintf( 'ubb_%s_source_terms', $term_source ) );
}

// Delete transient for source.
\delete_transient( sprintf( 'ubb_%s_term_source', $term_id ) );

/**
* Fires before a term's source is deleted.
*
* @since Unreleased
*
* @param int $term_id ID of the term.
* @param string $term_source Source ID of the term.
*/
do_action( 'ubb_term_source_delete', $term_id, $term_source );

return delete_term_meta( $term_id, 'ubb_source' );
}

Expand Down