Skip to content

Commit

Permalink
Merge pull request #15 from twimbit/siddhant-develop
Browse files Browse the repository at this point in the history
Siddhant develop
  • Loading branch information
amanintech authored Sep 16, 2020
2 parents 333935e + fac77d8 commit 4bd91fe
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 94 deletions.
11 changes: 7 additions & 4 deletions Wordpress Plugin/monday-twimbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ function monday_install() {
"CREATE TABLE $table_name[2] (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
subscription_id bigint(20) NOT NULL,
subscription_id bigint(20),
mondayId int(11) NOT NULL,
wpId int(11) NOT NULL,
boardId int(11) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[3] (
Expand Down Expand Up @@ -415,6 +416,8 @@ function update_or_create( $post_id ) {
$post_update = strtotime( $post->post_modified );


error_log( print_r( $post, true ) );

//for post create
if ( $post_date == $post_update && $post->post_status != 'auto-draft' ) {
if ( ! empty( get_post_meta( $post->ID, 'post_status' ) ) ) {
Expand Down Expand Up @@ -452,7 +455,7 @@ function monday_create_user_item( $userId ) {
function monday_create_comment_item( $commentId, $status, $data ) {
global $monday_mutation;
foreach ( get_board_ids( 'create_comment' ) as $board_id ) {
$itemId = $monday_mutation->create_item( $board_id->boardId, array(), $data['comment_content'] );
add_comment_meta( $commentId, 'comment_item_id', $itemId['data']['create_item']['id'] );
$itemId = $monday_mutation->create_item( $board_id->boardId, array(), $data['comment_content'] )['data']['create_item']['id'];
create_monday_comment( '', $itemId, $commentId, $board_id->boardId );
}
}
}
33 changes: 26 additions & 7 deletions Wordpress Plugin/utils/db_table-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function get_board_ids( $action = '' ) {
if ( empty( $action ) ) {

$results = $wpdb->get_results( "SELECT boardId FROM $wp_table_name" );
$results = array_unique( $results, SORT_REGULAR );
} else {

$results = $wpdb->get_results( "SELECT boardId FROM $wp_table_name WHERE action='$action'" );
Expand Down Expand Up @@ -53,11 +54,16 @@ function get_subscription_ids() {
}

//get post item id from post id
function get_post_item_id( $postId ) {
function get_post_item_id( $postId, $boardId = '' ) {
global $wpdb;
$wp_table_name = $wpdb->prefix . 'monday_post';

$results = $wpdb->get_results( "SELECT itemId FROM $wp_table_name WHERE postId=$postId" );
if ( empty( $boardId ) ) {
$results = $wpdb->get_results( "SELECT itemId FROM $wp_table_name WHERE postId=$postId" );
} else {
$results = $wpdb->get_results( "SELECT itemId FROM $wp_table_name WHERE postId=$postId AND boardId=$boardId" );
}


if ( empty( $results ) ) {
return '';
Expand Down Expand Up @@ -152,17 +158,16 @@ function get_item_post_id( $itemId ) {
//get board id from post item id
function get_post_item_board_id( $itemId ) {
global $wpdb;
$wp_table_name_post = $wpdb->prefix . 'monday_post';
$wp_table_name_action = $wpdb->prefix . 'monday_action';
$wp_table_name_post = $wpdb->prefix . 'monday_post';

$results = $wpdb->get_results( "SELECT boardId FROM $wp_table_name_post WHERE itemId=$itemId" )[0]->boardId;

$subscription_id = $wpdb->get_results( "SELECT subscription_id FROM $wp_table_name_post WHERE itemId=$itemId" )[0]->subscription_id;
$results = $wpdb->get_results( "SELECT boardId FROM $wp_table_name_action WHERE subscription_id=$subscription_id" );

if ( empty( $results ) ) {
return '';
}

return $results[0]->boardId;
return $results;
}

//get subscription authorization token
Expand Down Expand Up @@ -357,3 +362,17 @@ function monday_add_authorize_data( $clientId, $scopes, $expDate, $accessToken )

return array( 'error' => '' );
}

//create monday comment item id
function create_monday_comment( $subId, $mondayId, $wpId, $boardId ) {

global $wpdb;
$wp_table_name = $wpdb->prefix . 'monday_comments';
$wpdb->insert( $wp_table_name, array(
'time' => current_time( 'mysql' ),
"subscription_id" => $subId,
"mondayId" => $mondayId,
"wpId" => $wpId,
"boardId" => $boardId
) );
}
100 changes: 82 additions & 18 deletions Wordpress Plugin/utils/monday-functions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

//generate hash
function generate_hash( $data ) {

Expand Down Expand Up @@ -240,14 +241,14 @@ function sync_post_comments( $postId ) {
) );

$item_id = get_post_item_id( $postId );

foreach ( $comments as $comment ) {
if ( empty( get_comment_meta( $comment->comment_ID, 'update_id' ) ) ) {
$update_id = $monday_mutation->create_update( $item_id, $comment->comment_content );
add_comment_meta( $comment->comment_ID, 'update_id', $update_id['data']['create_update']['id'] );
foreach ( $item_id as $item ) {
if ( ! in_array( $item->itemId, get_comment_meta( $comment->comment_ID, 'update_id' ) ) ) {
$update_id = $monday_mutation->create_update( $item->itemId, $comment->comment_content );
add_comment_meta( $comment->comment_ID, 'update_id', $update_id['data']['create_update']['id'] );
}
}
}

}

//create monday post
Expand Down Expand Up @@ -293,7 +294,7 @@ function update_or_create_content( $board_id, $post_array, $post_status, $post )

$sub_id = get_subscription_id( $board_id->boardId );

sync_post_comments( $post->ID );
//sync_post_comments( $post->ID );

//monday board columns
$board_columns = get_option( 'monday_' . $board_id->boardId );
Expand All @@ -317,7 +318,7 @@ function update_or_create_content( $board_id, $post_array, $post_status, $post )
//update_post_meta( $post->ID, 'monday_item_id', $item_id );
} else if ( $post_status == 'update' ) {
//print_r( $post_post_array );
$item_id = get_post_item_id( $post->ID );
$item_id = get_post_item_id( $post->ID, $board_id->boardId );
foreach ( $item_id as $item ) {
$status = $monday_mutation->change_multiple_column_values( $board_id->boardId, $post_post_array, $item->itemId );
//error_log( print_r( $status, true ) );
Expand Down Expand Up @@ -351,28 +352,32 @@ function update_or_create_content_auto_synch( $board_id, $post_array, $post ) {
}
}

$item_id = get_post_item_id( $post->ID );
$item_id = get_post_item_id( $post->ID, $board_id->boardId );


if ( ! empty( $item_id ) ) {
foreach ( $item_id as $item ) {
if ( get_post_item_board_id( $item->itemId ) == $board_id->boardId ) {
if ( empty( get_item_post_id( $item->itemId ) ) ) {
create_monday_post_item_function( $board_id->boardId, $post_post_array, $post->post_title, $sub_id, $post->ID );
} else {

$status = $monday_mutation->change_multiple_column_values( $board_id->boardId, $post_post_array, $item->itemId );
if ( empty( get_item_post_id( $item->itemId ) ) ) {
create_monday_post_item_function( $board_id->boardId, $post_post_array, $post->post_title, $sub_id, $post->ID );
} else {
$status = $monday_mutation->change_multiple_column_values( $board_id->boardId, $post_post_array, $item->itemId );
if ( array_key_exists( 'state', $status['data']['change_multiple_column_values'] ) ) {
if ( $status['data']['change_multiple_column_values']['state'] == 'deleted' ) {
delete_post_item_id( $item->itemId );
if ( empty( get_item_post_id( $item->itemId ) ) ) {
create_monday_post_item_function( $board_id->boardId, $post_post_array, $post->post_title, $sub_id, $post->ID );
}
}
}
} else {
if ( empty( get_item_post_id( $item->itemId ) ) ) {
create_monday_post_item_function( $board_id->boardId, $post_post_array, $post->post_title, $sub_id, $post->ID );
} else if ( array_key_exists( 'status_code', $status ) ) {
if ( $status['status_code'] == '404' ) {
delete_post_item_id( $item->itemId );
if ( empty( get_item_post_id( $item->itemId ) ) ) {
create_monday_post_item_function( $board_id->boardId, $post_post_array, $post->post_title, $sub_id, $post->ID );
}
}
}
}

}
} else {
create_monday_post_item_function( $board_id->boardId, $post_post_array, $post->post_title, $sub_id, $post->ID );
Expand Down Expand Up @@ -401,4 +406,63 @@ function keys_refresh() {

add_action( 'wp_ajax_keys_refresh', 'keys_refresh' );

//create page or post
function process_post_or_page( $type, $board_id ) {

$args = array(
'numberposts' => - 1,
'post_type' => array( $type ),
'orderby' => 'title',
'order' => 'ASC',
'post_status' => array( 'pending', 'draft', 'future', 'publish' )
);

$posts_array = get_posts( $args );

foreach ( $posts_array as $post ) {

//sync post comments
//sync_post_comments( $post->ID );

// get post tags
$tags = get_post_tags_array( $post->ID );

// get post categories
$categories = get_post_categories_array( $post->ID );

if ( $post->post_status == 'publish' ) {
$status = 1;
} else {
$status = 0;
}

if ( empty( get_monday_user_id( $post->post_author ) ) ) {
$authors = array();
} else {
$authors = array(
array(
'id' => get_monday_user_id( $post->post_author ),
'kind' => 'person'
)
);
}

$post_array = array(
'name' => $post->post_title,
'author' => array(
'personsAndTeams' => $authors
),
'tags' => array( 'tag_ids' => $tags ),
'category' => array( 'tag_ids' => $categories ),
'status' => array( 'index' => $status ),
'date' => array(
'date' => get_the_date( 'Y-m-d', $post->ID ),
'time' => get_the_time( 'G:i:s', $post->ID )
),
'post_link' => array( 'url' => get_post_permalink( $post->ID ), 'text' => $post->post_title ),
);

update_or_create_content_auto_synch( $board_id, $post_array, $post );

}
}
70 changes: 8 additions & 62 deletions Wordpress Plugin/utils/post-synch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,24 @@ function sync_post() {
synch_monday_authors();


$args = array(
'numberposts' => - 1,
'post_type' => array( 'post', 'page' ),
'orderby' => 'title',
'order' => 'ASC',
'post_status' => array( 'pending', 'draft', 'future', 'publish' )
);
//for create post subscription
foreach ( get_board_ids( 'create_post' ) as $board_id ) {

$posts_array = get_posts( $args );
process_post_or_page( 'post', $board_id );

foreach ( $posts_array as $post ) {

//sync post comments
sync_post_comments( $post->ID );

// get post tags
$tags = get_post_tags_array( $post->ID );

// get post categories
$categories = get_post_categories_array( $post->ID );

if ( $post->post_status == 'publish' ) {
$status = 1;
} else {
$status = 0;
}

if ( empty( get_monday_user_id( $post->post_author ) ) ) {
$authors = array();
} else {
$authors = array(
array(
'id' => get_monday_user_id( $post->post_author ),
'kind' => 'person'
)
);
}
}

$post_array = array(
'name' => $post->post_title,
'author' => array(
'personsAndTeams' => $authors
),
'tags' => array( 'tag_ids' => $tags ),
'category' => array( 'tag_ids' => $categories ),
'status' => array( 'index' => $status ),
'date' => array(
'date' => get_the_date( 'Y-m-d', $post->ID ),
'time' => get_the_time( 'G:i:s', $post->ID )
),
'post_link' => array( 'url' => get_post_permalink( $post->ID ), 'text' => $post->post_title ),
);
//for create page subscription
foreach ( get_board_ids( 'create_page' ) as $board_id ) {

if ( $post->post_type == 'post' ) {
foreach ( get_board_ids( 'create_post' ) as $board_id ) {
update_or_create_content_auto_synch( $board_id, $post_array, $post );
}
}
process_post_or_page( 'page', $board_id );

if ( $post->post_type == 'page' ) {
foreach ( get_board_ids( 'create_page' ) as $board_id ) {
update_or_create_content_auto_synch( $board_id, $post_array, $post );
}
}
}

die();
}

//add_action( 'wp_ajax_nopriv_synch_post', 'synch_post' );
add_action( 'wp_ajax_sync_post', 'sync_post' );





6 changes: 3 additions & 3 deletions Wordpress Plugin/view/monday-settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function add_plugin_settings_menu() {
$this,
'create_plugin_settings_page'
) );
add_filter( 'admin_footer_text', 'remove_footer_admin' );
add_filter( 'admin_footer_text', 'remove_footer_admin_monday' );

function remove_footer_admin() { ?>
function remove_footer_admin_monday() { ?>
<span id="footer-thankyou">Twimbit wordpress monday integration <a
href="https://github.com/twimbit/monday-wordpress-integration">plugin</a>.</span>
<?php
Expand Down Expand Up @@ -95,7 +95,7 @@ function create_plugin_settings_page() {
</style>
<div class="wrap">
<div class="monday-cover">
<img src="<?php echo plugins_url() ?>/monday-twimbit/assets/monday-cover.png"
<img src="<?php echo plugin_dir_url( __DIR__ ) ?>assets/monday-cover.png"
alt="">
</div>

Expand Down

0 comments on commit 4bd91fe

Please sign in to comment.