Skip to content

Commit

Permalink
Switch to REST API for project suggest.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Feb 21, 2024
1 parent 06f6f3d commit 1629adb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 108 deletions.
136 changes: 28 additions & 108 deletions classes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Pronamic\Orbis\Projects;

use stdClass;
use WP_Post;

class Plugin {
public function __construct( $file ) {
Expand All @@ -20,8 +21,6 @@ public function __construct( $file ) {

add_action( 'p2p_init', [ $this, 'p2p_init' ] );

add_action( 'wp_ajax_project_id_suggest', [ $this, 'ajax_projects_suggest_project_id' ] );

$this->content_types = new ContentTypes();
$this->query_processor = new QueryProcessor();
$this->shortcodes = new Shortcodes( $this );
Expand All @@ -32,6 +31,33 @@ public function __construct( $file ) {
} else {
$this->theme = new Theme( $this );
}

\add_action(
'rest_api_init',
function () {
\register_rest_field(
'orbis_project',
'select2_text',
[
'get_callback' => function() {
$project_post = \get_post();

if ( ! $project_post instanceof WP_Post ) {
return null;
}

return \sprintf(
'%s. %s - %s ( %s )',
$project_post->project_id,
$project_post->principal_name,
\get_the_title( $project_post ),
isset( $project->project_logged_time ) ? \orbis_time( $project_post->project_logged_time ) . ' / ' . \orbis_time( $project_post->project_number_seconds ) : \orbis_time( $project_post->project_number_seconds )
);
},
]
);
}
);
}

public function init() {
Expand Down Expand Up @@ -147,110 +173,4 @@ public function p2p_init() {
]
);
}

/**
* AJAX projects suggest project ID.
*/
public function ajax_projects_suggest_project_id() {
global $wpdb;

$term = filter_input( INPUT_GET, 'term', FILTER_SANITIZE_STRING );

$extra_select = '';
$extra_join = '';
$extra_where = '';

if ( isset( $wpdb->orbis_timesheets ) ) {
$extra_select .= ',
SUM( entry.number_seconds ) AS project_logged_time
';

$extra_join = "
LEFT JOIN
$wpdb->orbis_timesheets AS entry
ON entry.project_id = project.id
";
}

if ( isset( $wpdb->orbis_companies ) ) {
$extra_select .= ',
principal.name AS principal_name
';

$extra_join .= "
LEFT JOIN
$wpdb->orbis_companies AS principal
ON project.principal_id = principal.id
";

$extra_where .= '
OR
principal.name LIKE %s
';
}

$query = "
SELECT
project.id AS project_id,
project.name AS project_name,
project.number_seconds AS project_time
$extra_select
FROM
$wpdb->orbis_projects AS project
$extra_join
WHERE
project.finished = 0
AND
(
project.name LIKE %s
$extra_where
)
GROUP BY
project.id
ORDER BY
project.id
";

$like = '%' . $wpdb->esc_like( $term ) . '%';

$query = $wpdb->prepare( $query, $like, $like ); // unprepared SQL

$projects = $wpdb->get_results( $query ); // unprepared SQL

$data = [];

foreach ( $projects as $project ) {
$result = new stdClass();
$result->id = $project->project_id;

$principal_name = ( isset( $project->principal_name ) ) ? $project->principal_name . ' -' : '';

$text = sprintf(
'%s. %s %s ( %s )',
$project->project_id,
$principal_name,
$project->project_name,
orbis_time( $project->project_time )
);

if ( isset( $project->project_logged_time ) ) {
$text = sprintf(
'%s. %s %s ( %s / %s )',
$project->project_id,
$principal_name,
$project->project_name,
orbis_time( $project->project_logged_time ),
orbis_time( $project->project_time )
);
}

$result->text = $text;

$data[] = $result;
}

echo wp_json_encode( $data );

die();
}
}
1 change: 1 addition & 0 deletions classes/QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function posts_clauses( $pieces, $query ) {
if ( 'orbis_project' === $post_type ) {
// Fields
$fields = ',
project.id AS project_id,
project.number_seconds AS project_number_seconds,
project.finished AS project_is_finished,
project.invoiced AS project_is_invoiced
Expand Down

0 comments on commit 1629adb

Please sign in to comment.