Skip to content

Refactoring version 2 8 #306

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

Merged
merged 2 commits into from
May 24, 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
44 changes: 18 additions & 26 deletions includes/MslsCustomFilter.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php
/**
* MslsCustomFilter
* @author Maciej Czerpiński <[email protected]>
* @contributor Dennis Ploetner <[email protected]>
* @since 0.9.9
*/

namespace lloc\Msls;

use lloc\Msls\Query\TranslatedPostsQuery;

/**
* Adding custom filter to posts/pages table.
*
* @package Msls
*/
class MslsCustomFilter extends MslsMain {
Expand All @@ -22,15 +19,15 @@ class MslsCustomFilter extends MslsMain {
* @return MslsCustomFilter
*/
public static function init() {
$options = msls_options();
$options = msls_options();
$collection = msls_blog_collection();
$obj = new static( $options, $collection );
$obj = new static( $options, $collection );

if ( ! $options->is_excluded() ) {
$post_type = MslsPostType::instance()->get_request();
if ( ! empty( $post_type ) ) {
add_action( 'restrict_manage_posts', [ $obj, 'add_filter' ] );
add_filter( 'parse_query', [ $obj, 'execute_filter' ] );
add_action( 'restrict_manage_posts', array( $obj, 'add_filter' ) );
add_filter( 'parse_query', array( $obj, 'execute_filter' ) );
}
}

Expand All @@ -39,11 +36,12 @@ public static function init() {

/**
* Echo's select tag with list of blogs
*
* @uses selected
*/
public function add_filter(): void {
$id = (
filter_has_var( INPUT_GET, 'msls_filter' ) ?
filter_has_var( INPUT_GET, 'msls_filter' ) ?
filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) :
''
);
Expand All @@ -57,8 +55,10 @@ public function add_filter(): void {
'<option value="%d" %s>%s</option>',
$blog->userblog_id,
selected( $id, $blog->userblog_id, false ),
sprintf( __( 'Not translated in the %s-blog', 'multisite-language-switcher' ),
$blog->get_description() )
sprintf(
__( 'Not translated in the %s-blog', 'multisite-language-switcher' ),
$blog->get_description()
)
);
}
echo '</select>';
Expand All @@ -80,21 +80,14 @@ public function execute_filter( \WP_Query $query ) {
}

$id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT );

if ( isset( $blogs[ $id ] ) ) {
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );

// load post we need to exclude (already have translation) from search query
$posts = $cache->get_results(
$cache->prepare(
"SELECT option_id, option_name FROM {$cache->options} WHERE option_name LIKE %s AND option_value LIKE %s",
'msls_%',
'%"' . $blogs[ $id ]->get_language() . '"%'
)
);
// load post we need to exclude (they already have a translation) from search query
$translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blogs[ $id ]->get_language() );

$exclude_ids = [];
foreach ( $posts as $post ) {
$exclude_ids = array();
foreach ( $translated_posts as $post ) {
$exclude_ids[] = substr( $post->option_name, 5 );
}
$query->query_vars['post__not_in'] = $exclude_ids;
Expand All @@ -104,5 +97,4 @@ public function execute_filter( \WP_Query $query ) {

return false;
}

}
11 changes: 6 additions & 5 deletions includes/MslsGetSet.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* MslsGetSet
*
* @author Dennis Ploetner <[email protected]>
* @since 0.9.8
*/
Expand All @@ -18,15 +19,16 @@ class MslsGetSet extends MslsRegistryInstance {

/**
* Generic container for all properties of an instance
*
* @var array $arr
*/
protected $arr = [];
protected $arr = array();

/**
* Overloads the set method.
*
* @param string $key
* @param mixed $value
* @param mixed $value
*/
public function __set( $key, $value ) {
$this->arr[ $key ] = $value;
Expand Down Expand Up @@ -75,7 +77,7 @@ public function __unset( $key ) {
* @return MslsGetSet
*/
public function reset() {
$this->arr = [];
$this->arr = array();

return $this;
}
Expand All @@ -98,7 +100,7 @@ public function reset() {
*
* @return bool
*/
public function has_value( $key ) {
public function has_value( string $key ): bool {
return ! empty( $this->arr[ $key ] );
}

Expand All @@ -119,5 +121,4 @@ public function is_empty() {
public function get_arr() {
return $this->arr;
}

}
54 changes: 30 additions & 24 deletions includes/MslsOptionsQuery.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<?php
/**
* MslsOptionsQuery
*
* @author Dennis Ploetner <[email protected]>
* @since 0.9.8
*/

namespace lloc\Msls;

/**
* OptionsQuery
* MslsOptionsQuery
*
* @package Msls
*/
Expand All @@ -22,36 +16,48 @@ class MslsOptionsQuery extends MslsOptions {
*/
public $with_front = true;

protected MslsSqlCacher $sql_cache;

public function __construct( MslsSqlCacher $sql_cache ) {
parent::__construct();

$this->sql_cache = $sql_cache;
}

/**
* @return array<string, mixed>
*/
public static function get_params(): array {
return array();
}

/**
* Factory method
*
* @param int $id This parameter is unused here
*
* @return MslsOptionsQuery|null
* @return ?MslsOptionsQuery
*/
public static function create( $id = 0 ) {
$query = null;

public static function create( $id = 0 ): ?MslsOptionsQuery {
if ( is_day() ) {
$query = new MslsOptionsQueryDay(
get_query_var( 'year' ),
get_query_var( 'monthnum' ),
get_query_var( 'day' )
);
$query_class = MslsOptionsQueryDay::class;
} elseif ( is_month() ) {
$query = new MslsOptionsQueryMonth(
get_query_var( 'year' ),
get_query_var( 'monthnum' )
);
$query_class = MslsOptionsQueryMonth::class;
} elseif ( is_year() ) {
$query = new MslsOptionsQueryYear( get_query_var( 'year' ) );
$query_class = MslsOptionsQueryYear::class;
} elseif ( is_author() ) {
$query = new MslsOptionsQueryAuthor( get_queried_object_id() );
$query_class = MslsOptionsQueryAuthor::class;
} elseif ( is_post_type_archive() ) {
$query = new MslsOptionsQueryPostType( get_query_var( 'post_type' ) );
$query_class = MslsOptionsQueryPostType::class;
}

if ( ! isset( $query_class ) ) {
return null;
}

return $query;
$sql_cache = MslsSqlCacher::create( $query_class, $query_class::get_params() );

return new $query_class( $sql_cache );
}

/**
Expand Down
40 changes: 23 additions & 17 deletions includes/MslsOptionsQueryAuthor.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
<?php
/**
* MslsOptionsQueryAuthor
*
* @author Dennis Ploetner <[email protected]>
* @since 0.9.8
*/

namespace lloc\Msls;

use lloc\Msls\Query\AuthorPostsCounterQuery;

/**
* OptionsQueryAuthor
* MslsOptionsQueryAuthor
*
* @package Msls
*/
class MslsOptionsQueryAuthor extends MslsOptionsQuery {

protected int $author_id;

public function __construct( MslsSqlCacher $sql_cache ) {
parent::__construct( $sql_cache );

$this->author_id = self::get_params()['author_id'];
}

/**
* @return array<string, mixed>
*/
public static function get_params(): array {
return array(
'author_id' => get_queried_object_id(),
);
}

/**
* Check if the array has a non-empty item which has $language as a key
*
* @param string $language
*
* @return bool
*/
public function has_value( $language ) {
public function has_value( string $language ): bool {
if ( ! isset( $this->arr[ $language ] ) ) {
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args );

$this->arr[ $language ] = $cache->get_var(
$cache->prepare(
"SELECT count(ID) FROM {$cache->posts} WHERE post_author = %d AND post_status = 'publish'",
$this->get_arg( 0, 0 )
)
);
$this->arr[ $language ] = ( new AuthorPostsCounterQuery( $this->sql_cache ) )( $this->author_id );
}

return (bool) $this->arr[ $language ];
Expand All @@ -43,6 +49,6 @@ public function has_value( $language ) {
* @return string
*/
public function get_current_link() {
return get_author_posts_url( $this->get_arg( 0, 0 ) );
return get_author_posts_url( $this->author_id );
}
}
53 changes: 32 additions & 21 deletions includes/MslsOptionsQueryDay.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
<?php
/**
* MslsOptionsQueryDay
* @author Dennis Ploetner <[email protected]>
* @since 0.9.8
*/

namespace lloc\Msls;

use lloc\Msls\Query\DatePostsCounterQuery;

/**
* OptionsQueryDay
* MslsOptionsQueryDay
*
* @package Msls
*/
class MslsOptionsQueryDay extends MslsOptionsQuery {

protected int $year;

protected int $monthnum;
protected int $day;

public function __construct( MslsSqlCacher $sql_cache ) {
parent::__construct( $sql_cache );

$params = self::get_params();

$this->year = $params['year'];
$this->monthnum = $params['monthnum'];
$this->day = $params['day'];
}

public static function get_params(): array {
return array(
'year' => get_query_var( 'year' ),
'monthnum' => get_query_var( 'monthnum' ),
'day' => get_query_var( 'day' ),
);
}

/**
* Check if the array has an non empty item which has $language as a key
* Check if the array has a non-empty item which has $language as a key
*
* @param string $language
*
* @return bool
*/
public function has_value( $language ) {
public function has_value( string $language ): bool {
if ( ! isset( $this->arr[ $language ] ) ) {
$date = new \DateTime();
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args );

$this->arr[ $language ] = $cache->get_var(
$cache->prepare(
"SELECT count(ID) FROM {$cache->posts} WHERE DATE(post_date) = %s AND post_status = 'publish'",
$date->setDate( $this->get_arg( 0, 0 ),
$this->get_arg( 1, 0 ),
$this->get_arg( 2, 0 ) )->format( 'Y-m-d' )
)
);
$query_callable = new DatePostsCounterQuery( $this->sql_cache );

$this->arr[ $language ] = $query_callable( $this->year, $this->monthnum, $this->day );
}

return (bool) $this->arr[ $language ];
Expand All @@ -45,7 +57,6 @@ public function has_value( $language ) {
* @return string
*/
public function get_current_link() {
return get_day_link( $this->get_arg( 0, 0 ), $this->get_arg( 1, 0 ), $this->get_arg( 2, 0 ) );
return get_day_link( $this->year, $this->monthnum, $this->day );
}

}
Loading
Loading