Skip to content

Commit 8c81562

Browse files
committed
Add hashtag search support.
And check for empty queries so we don't waste time with them.
1 parent e3fe74e commit 8c81562

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

includes/class-mastodon-api.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,9 @@ public function api_tag_timelines( $request ) {
15221522
$args['tag'] = $request->get_param( 'hashtag' );
15231523

15241524
$ppp_param = $request->get_param( 'limit' );
1525-
if( $ppp_param != null ) { $args['posts_per_page'] = $ppp_param; }
1525+
if ( null !== $ppp_param ) {
1526+
$args['posts_per_page'] = $ppp_param;
1527+
}
15261528

15271529
$args = apply_filters( 'mastodon_api_timelines_args', $args, $request );
15281530

@@ -1540,14 +1542,20 @@ public function api_search( $request ) {
15401542
$ret = array(
15411543
'accounts' => array(),
15421544
'statuses' => array(),
1545+
'hashtags' => array(),
15431546
);
1547+
$search_query = trim( $request->get_param( 'q' ) );
1548+
// Don't allow empty search queries.
1549+
if ( '' === $search_query ) {
1550+
return $ret;
1551+
}
15441552
if ( ! $type || 'accounts' === $type ) {
1545-
if ( preg_match( '/^@?' . self::ACTIVITYPUB_USERNAME_REGEXP . '$/i', $request->get_param( 'q' ) ) && ! $request->get_param( 'offset' ) ) {
1546-
$ret['accounts'][] = $this->get_friend_account_data( $request->get_param( 'q' ), array(), true );
1553+
if ( preg_match( '/^@?' . self::ACTIVITYPUB_USERNAME_REGEXP . '$/i', $search_query ) && ! $request->get_param( 'offset' ) ) {
1554+
$ret['accounts'][] = $this->get_friend_account_data( $search_query, array(), true );
15471555
}
15481556
$query = new \WP_User_Query(
15491557
array(
1550-
'search' => '*' . $request->get_param( 'q' ) . '*',
1558+
'search' => '*' . $search_query . '*',
15511559
'search_columns' => array(
15521560
'user_login',
15531561
'user_nicename',
@@ -1570,10 +1578,10 @@ public function api_search( $request ) {
15701578
return array();
15711579
}
15721580
$args = apply_filters( 'mastodon_api_timelines_args', $args, $request );
1573-
$valid_url = wp_parse_url( $request->get_param( 'q' ) );
1581+
$valid_url = wp_parse_url( $search_query );
15741582
if ( $valid_url && isset( $valid_url['host'] ) ) {
15751583
if ( ! $request->get_param( 'offset' ) ) {
1576-
$url = $request->get_param( 'q' );
1584+
$url = $search_query;
15771585
$json = $this->get_json( $url, crc32( $url ) );
15781586
if ( ! is_wp_error( $json ) && isset( $json['id'], $json['attributedTo'] ) ) {
15791587
$user_id = $this->get_acct( $json['attributedTo'] );
@@ -1588,12 +1596,43 @@ public function api_search( $request ) {
15881596
}
15891597
}
15901598
} elseif ( is_user_logged_in() ) {
1591-
$args['s'] = $request->get_param( 'q' );
1599+
$args['s'] = $search_query;
15921600
$args['offset'] = $request->get_param( 'offset' );
15931601
$args['posts_per_page'] = $request->get_param( 'limit' );
15941602
$ret['statuses'] = array_merge( $ret['statuses'], $this->get_posts( $args ) );
15951603
}
15961604
}
1605+
if ( ! $type || 'hashtags' === $type ) {
1606+
$q_param = $request->get_param( 'q' );
1607+
$categories = get_categories(
1608+
array(
1609+
'orderby' => 'name',
1610+
'hide_empty' => false,
1611+
'search' => $q_param,
1612+
)
1613+
);
1614+
foreach ( $categories as $category ) {
1615+
$ret['hashtags'][] = array(
1616+
'name' => $category->name,
1617+
'url' => get_category_link( $category ),
1618+
'history' => array(),
1619+
);
1620+
}
1621+
$tags = get_tags(
1622+
array(
1623+
'orderby' => 'name',
1624+
'hide_empty' => false,
1625+
'search' => $q_param,
1626+
)
1627+
);
1628+
foreach ( $tags as $tag ) {
1629+
$ret['hashtags'][] = array(
1630+
'name' => $tag->name,
1631+
'url' => get_tag_link( $tag ),
1632+
'history' => array(),
1633+
);
1634+
}
1635+
}
15971636
return $ret;
15981637
}
15991638

0 commit comments

Comments
 (0)