WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
класс не описан

DataStore() WC 1.0

WC Admin Note Data Store (Custom Tables)

Возвращает

Null. Ничего.

Использование

$DataStore = new DataStore();
// use class methods

Методы

  1. ERROR: no method name found on line ``
  2. set_name( $note_row->name )
  3. set_image( $note_row->image )
  4. ERROR: no method name found on line ``
  5. ERROR: no method name found on line ``
  6. ERROR: no method name found on line ``
  7. ERROR: no method name found on line ``
  8. ERROR: no method name found on line ``
  9. Exception( __( 'Invalid admin note', 'woocommerce' ) )
  10. ERROR: no method name found on line ``
  11. ERROR: no method name found on line ``
  12. ERROR: no method name found on line ``
  13. get_id()
  14. ERROR: no method name found on line ` }`
  15. set_layout( $note_row->layout )
  16. elseif ( $note_row )
  17. ERROR: no method name found on line ``
  18. gmdate( 'Y-m-d H:i:s', $date_reminder_timestamp )
  19. ERROR: no method name found on line ` * Fires when an admin note is loaded.`
  20. ERROR: no method name found on line ` * Fires when an admin note is loaded.`
  21. ERROR: no method name found on line ``
  22. ERROR: no method name found on line ` } else {`
  23. ERROR: no method name found on line ` }`
  24. ERROR: no method name found on line ``
  25. ERROR: no method name found on line ` /**`
  26. save_meta_data()
  27. ERROR: no method name found on line ` * @param WC_Admin_Note $note Admin note.`
  28. save_meta_data()
  29. ERROR: no method name found on line ` */`
  30. ERROR: no method name found on line ``
  31. ERROR: no method name found on line ` }`
  32. ERROR: no method name found on line ``
  33. ERROR: no method name found on line ` * @throws \Exception Throws exception when invalid data is found.`
  34. ERROR: no method name found on line ` "SELECT * FROM {$wpdb->prefix}wc_admin_notes WHERE note_id = %d LIMIT 1",`
  35. ERROR: no method name found on line ` */`
  36. save_meta_data()
  37. if ( 0 !== $note_id || '0' !== $note_id )
  38. if ( 0 === $note->get_id() || '0' === $note->get_id() )
  39. ERROR: no method name found on line ``
  40. create( &$note )
  41. ERROR: no method name found on line ``
  42. delete( &$note, $args = array() )
  43. IN ($escaped_where_names)
  44. ERROR: no method name found on line ``
  45. ERROR: no method name found on line ``
  46. get_notes_where_clauses(
  47. ERROR: no method name found on line ` * @return bool|void`
  48. ERROR: no method name found on line ``
  49. ERROR: no method name found on line ` * @param string $where_clauses The generated WHERE clause.`
  50. if ( is_null( $date_reminder ) )
  51. get_source()
  52. ERROR: no method name found on line ` }`
  53. get_notes_with_name( $name )
  54. implode( ',', $where_status_array )
  55. ERROR: no method name found on line ``
  56. ERROR: no method name found on line ``
  57. if ( in_array( $args_type, $allowed_types, true ) )
  58. ERROR: no method name found on line ` *`
  59. ERROR: no method name found on line ` * @param WC_Admin_Note $note Admin note.`
  60. query( $clear_actions_query )
  61. ERROR: no method name found on line ` /**`
  62. getTimestamp()
  63. ERROR: no method name found on line ` }`
  64. ERROR: no method name found on line ` *`
  65. read_actions( $note )
  66. apply_changes()
  67. ERROR: no method name found on line ` FROM {$wpdb->prefix}wc_admin_note_actions`
  68. get_actions( 'edit' )
  69. elseif ( is_array( $content_data ) )
  70. ERROR: no method name found on line ` *`
  71. ERROR: no method name found on line ` 'label' => $action->label,`
  72. ERROR: no method name found on line ``
  73. set_content_data( $content_data )
  74. ERROR: no method name found on line ``
  75. if ( $db_actions )
  76. ERROR: no method name found on line ` */`
  77. ERROR: no method name found on line ``
  78. ERROR: no method name found on line ``
  79. ERROR: no method name found on line ``
  80. ERROR: no method name found on line ``
  81. ERROR: no method name found on line ``
  82. ERROR: no method name found on line ``
  83. ERROR: no method name found on line ``
  84. ERROR: no method name found on line ``
  85. ERROR: no method name found on line ``
  86. ERROR: no method name found on line ``
  87. ERROR: no method name found on line ` ),`
  88. ERROR: no method name found on line ``
  89. ERROR: no method name found on line ``
  90. prepare(
  91. delete( &$note, $args = array() )
  92. ERROR: no method name found on line ``
  93. ERROR: no method name found on line ``
  94. ERROR: no method name found on line ``
  95. ERROR: no method name found on line ``
  96. ERROR: no method name found on line ``
  97. ERROR: no method name found on line ``
  98. ERROR: no method name found on line ``
  99. ERROR: no method name found on line ` * @param WC_Admin_Note $note Admin note.`
  100. ERROR: no method name found on line ``
  101. ERROR: no method name found on line ``
  102. get_notes( $args = array() )
  103. get_notes_count( $type = array(), $status = array() )
  104. get_notes_where_clauses( $args = array() )
  105. get_notes_with_name( $name )
  106. ERROR: no method name found on line ``
  107. ERROR: no method name found on line ``
  108. ERROR: no method name found on line ``
  109. ERROR: no method name found on line ` /**`
  110. Exception( __( 'Invalid admin note', 'woocommerce' ) )
  111. ERROR: no method name found on line ``
  112. ERROR: no method name found on line ` 'status' => $status,`
  113. ERROR: no method name found on line ``
  114. ERROR: no method name found on line ``
  115. elseif ( is_array( $content_data ) )
  116. ERROR: no method name found on line ``
  117. ERROR: no method name found on line ` }`
  118. get_status()
  119. ERROR: no method name found on line ` '%s',`
  120. ERROR: no method name found on line ` * Fires when an admin note is deleted.`
  121. ERROR: no method name found on line ` global $wpdb;`
  122. apply_changes()
  123. ERROR: no method name found on line ` /**`
  124. read_meta_data()
  125. ERROR: no method name found on line ` $note_row = false;`
  126. set_source( $note_row->source )
  127. elseif ( is_array( $content_data ) )
  128. ERROR: no method name found on line ``
  129. array()
  130. ERROR: no method name found on line ` *`
  131. ERROR: no method name found on line ` $offset,`
  132. array_keys( $note->get_changes() )
  133. ERROR: no method name found on line ``
  134. set_actions( $note_actions )
  135. ERROR: no method name found on line `}`
  136. ERROR: no method name found on line ``
  137. ERROR: no method name found on line ``
  138. ERROR: no method name found on line ``
  139. ERROR: no method name found on line ` $action_data['action_id'] = $action->id;`
  140. get_results( $query )
  141. delete( &$note, $args = array() )
  142. if ( isset( $args['type'] ) )
  143. ERROR: no method name found on line ``
  144. ERROR: no method name found on line ``
  145. ERROR: no method name found on line ``
  146. ERROR: no method name found on line ``
  147. set_date_reminder( $note_row->date_reminder )
  148. ERROR: no method name found on line ``
  149. ERROR: no method name found on line ` ),`
  150. ERROR: no method name found on line ``
  151. if ( is_null( $date_reminder ) )
  152. ERROR: no method name found on line ``
  153. do_action( 'woocommerce_note_updated', $note->get_id() )
  154. ERROR: no method name found on line ` 'name' => $action->name,`
  155. ERROR: no method name found on line ` $wpdb->prefix . 'wc_admin_notes',`
  156. get_content()
  157. ERROR: no method name found on line ``
  158. ERROR: no method name found on line ` );`
  159. ERROR: no method name found on line ``
  160. ERROR: no method name found on line ` }`
  161. read( &$note )
  162. read_actions( &$note )
  163. ERROR: no method name found on line ` 'page' => 1,`
  164. save_actions( &$note )
  165. ERROR: no method name found on line ``
  166. ERROR: no method name found on line ``
  167. ERROR: no method name found on line ``
  168. set_image( $note_row->image )
  169. ERROR: no method name found on line ` }`
  170. ERROR: no method name found on line ` 'query' => $action->query,`
  171. ERROR: no method name found on line ` )`
  172. ERROR: no method name found on line ` * @return array An array of objects containing a note id.`
  173. if ( isset( $args['name'] ) )
  174. ERROR: no method name found on line ``
  175. ERROR: no method name found on line ` * Allows modification of the notes select criterial.`
  176. ERROR: no method name found on line ` * @return array An array of objects containing a note id.`
  177. trim( $args_type )
  178. ERROR: no method name found on line ``
  179. update( &$note )
  180. ERROR: no method name found on line ` $name`
  181. ERROR: no method name found on line ``
  182. sprintf( "'%s'", esc_sql( $args_name ) )
  183. ERROR: no method name found on line ``
  184. ERROR: no method name found on line ` // The default for 'content_value' used to be an array, so there might be rows with invalid data!`
  185. prepare(
  186. if ( 0 !== $note_id || '0' !== $note_id )
  187. ERROR: no method name found on line ` * @throws \Exception Throws exception when invalid data is found.`

Код DataStore: woocommerce/packages/woocommerce-admin/src/Notes/DataStore.php WC 4.3.1

<?php
class DataStore extends \WC_Data_Store_WP implements \WC_Object_Data_Store_Interface {
	/**
	 * Method to create a new note in the database.
	 *
	 * @param WC_Admin_Note $note Admin note.
	 */
	public function create( &$note ) {
		$date_created = time();
		$note->set_date_created( $date_created );

		global $wpdb;

		$note_to_be_inserted = array(
			'name'         => $note->get_name(),
			'type'         => $note->get_type(),
			'locale'       => $note->get_locale(),
			'title'        => $note->get_title(),
			'content'      => $note->get_content(),
			'status'       => $note->get_status(),
			'source'       => $note->get_source(),
			'is_snoozable' => (int) $note->get_is_snoozable(),
			'layout'       => $note->get_layout(),
			'image'        => $note->get_image(),
			'is_deleted'   => (int) $note->get_is_deleted(),
		);

		$note_to_be_inserted['content_data']  = wp_json_encode( $note->get_content_data() );
		$note_to_be_inserted['date_created']  = gmdate( 'Y-m-d H:i:s', $date_created );
		$note_to_be_inserted['date_reminder'] = null;

		$wpdb->insert( $wpdb->prefix . 'wc_admin_notes', $note_to_be_inserted );
		$note_id = $wpdb->insert_id;
		$note->set_id( $note_id );
		$note->save_meta_data();
		$this->save_actions( $note );
		$note->apply_changes();

		/**
		 * Fires when an admin note is created.
		 *
		 * @param int $note_id Note ID.
		 */
		do_action( 'woocommerce_note_created', $note_id );
	}

	/**
	 * Method to read a note.
	 *
	 * @param WC_Admin_Note $note Admin note.
	 * @throws \Exception Throws exception when invalid data is found.
	 */
	public function read( &$note ) {
		global $wpdb;

		$note->set_defaults();
		$note_row = false;

		$note_id = $note->get_id();
		if ( 0 !== $note_id || '0' !== $note_id ) {
			$note_row = $wpdb->get_row(
				$wpdb->prepare(
					"SELECT * FROM {$wpdb->prefix}wc_admin_notes WHERE note_id = %d LIMIT 1",
					$note->get_id()
				)
			);
		}

		if ( 0 === $note->get_id() || '0' === $note->get_id() ) {
			$this->read_actions( $note );
			$note->read_meta_data();
			$note->set_object_read( true );

			/**
			 * Fires when an admin note is loaded.
			 *
			 * @param int $note_id Note ID.
			 */
			do_action( 'woocommerce_note_loaded', $note );
		} elseif ( $note_row ) {
			$note->set_name( $note_row->name );
			$note->set_type( $note_row->type );
			$note->set_locale( $note_row->locale );
			$note->set_title( $note_row->title );
			$note->set_content( $note_row->content );

			// The default for 'content_value' used to be an array, so there might be rows with invalid data!
			$content_data = json_decode( $note_row->content_data );
			if ( ! $content_data ) {
				$content_data = new \stdClass();
			} elseif ( is_array( $content_data ) ) {
				$content_data = (object) $content_data;
			}
			$note->set_content_data( $content_data );

			$note->set_status( $note_row->status );
			$note->set_source( $note_row->source );
			$note->set_date_created( $note_row->date_created );
			$note->set_date_reminder( $note_row->date_reminder );
			$note->set_is_snoozable( $note_row->is_snoozable );
			$note->set_layout( $note_row->layout );
			$note->set_image( $note_row->image );
			$this->read_actions( $note );
			$note->read_meta_data();
			$note->set_object_read( true );

			/**
			 * Fires when an admin note is loaded.
			 *
			 * @param int $note_id Note ID.
			 */
			do_action( 'woocommerce_note_loaded', $note );
		} else {
			throw new \Exception( __( 'Invalid admin note', 'woocommerce' ) );
		}
	}

	/**
	 * Updates a note in the database.
	 *
	 * @param WC_Admin_Note $note Admin note.
	 */
	public function update( &$note ) {
		global $wpdb;

		if ( $note->get_id() ) {
			$date_created           = $note->get_date_created();
			$date_created_timestamp = $date_created->getTimestamp();
			$date_created_to_db     = gmdate( 'Y-m-d H:i:s', $date_created_timestamp );

			$date_reminder = $note->get_date_reminder();
			if ( is_null( $date_reminder ) ) {
				$date_reminder_to_db = null;
			} else {
				$date_reminder_timestamp = $date_reminder->getTimestamp();
				$date_reminder_to_db     = gmdate( 'Y-m-d H:i:s', $date_reminder_timestamp );
			}

			$wpdb->update(
				$wpdb->prefix . 'wc_admin_notes',
				array(
					'name'          => $note->get_name(),
					'type'          => $note->get_type(),
					'locale'        => $note->get_locale(),
					'title'         => $note->get_title(),
					'content'       => $note->get_content(),
					'content_data'  => wp_json_encode( $note->get_content_data() ),
					'status'        => $note->get_status(),
					'source'        => $note->get_source(),
					'date_created'  => $date_created_to_db,
					'date_reminder' => $date_reminder_to_db,
					'is_snoozable'  => $note->get_is_snoozable(),
					'layout'        => $note->get_layout(),
					'image'         => $note->get_image(),
					'is_deleted'    => $note->get_is_deleted(),
				),
				array( 'note_id' => $note->get_id() )
			);
		}

		$note->save_meta_data();
		$this->save_actions( $note );
		$note->apply_changes();

		/**
		 * Fires when an admin note is updated.
		 *
		 * @param int $note_id Note ID.
		 */
		do_action( 'woocommerce_note_updated', $note->get_id() );
	}

	/**
	 * Deletes a note from the database.
	 *
	 * @param WC_Admin_Note $note Admin note.
	 * @param array         $args Array of args to pass to the delete method (not used).
	 */
	public function delete( &$note, $args = array() ) {
		$note_id = $note->get_id();
		if ( $note_id ) {
			global $wpdb;
			$wpdb->delete( $wpdb->prefix . 'wc_admin_notes', array( 'note_id' => $note_id ) );
			$wpdb->delete( $wpdb->prefix . 'wc_admin_note_actions', array( 'note_id' => $note_id ) );
			$note->set_id( null );
		}

		/**
		 * Fires when an admin note is deleted.
		 *
		 * @param int $note_id Note ID.
		 */
		do_action( 'woocommerce_note_deleted', $note_id );
	}

	/**
	 * Read actions from the database.
	 *
	 * @param WC_Admin_Note $note Admin note.
	 */
	private function read_actions( &$note ) {
		global $wpdb;

		$db_actions = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT action_id, name, label, query, status, is_primary
				FROM {$wpdb->prefix}wc_admin_note_actions
				WHERE note_id = %d",
				$note->get_id()
			)
		);

		$note_actions = array();

		if ( $db_actions ) {
			foreach ( $db_actions as $action ) {
				$note_actions[] = (object) array(
					'id'      => (int) $action->action_id,
					'name'    => $action->name,
					'label'   => $action->label,
					'query'   => $action->query,
					'status'  => $action->status,
					'primary' => (bool) $action->is_primary,
				);
			}
		}

		$note->set_actions( $note_actions );
	}

	/**
	 * Save actions to the database.
	 * This function clears old actions, then re-inserts new if any changes are found.
	 *
	 * @param WC_Admin_Note $note Note object.
	 *
	 * @return bool|void
	 */
	private function save_actions( &$note ) {
		global $wpdb;

		$changed_props = array_keys( $note->get_changes() );

		if ( ! in_array( 'actions', $changed_props, true ) ) {
			return false;
		}

		// Process action removal. Actions are removed from
		// the note if they aren't part of the changeset.
		// See WC_Admin_Note::add_action().
		$changed_actions = $note->get_actions( 'edit' );
		$actions_to_keep = array();

		foreach ( $changed_actions as $action ) {
			if ( ! empty( $action->id ) ) {
				$actions_to_keep[] = (int) $action->id;
			}
		}

		$clear_actions_query = $wpdb->prepare(
			"DELETE FROM {$wpdb->prefix}wc_admin_note_actions WHERE note_id = %d",
			$note->get_id()
		);

		if ( $actions_to_keep ) {
			$clear_actions_query .= sprintf( ' AND action_id NOT IN (%s)', implode( ',', $actions_to_keep ) );
		}

		$wpdb->query( $clear_actions_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

		// Update/insert the actions in this changeset.
		foreach ( $changed_actions as $action ) {
			$action_data = array(
				'note_id'    => $note->get_id(),
				'name'       => $action->name,
				'label'      => $action->label,
				'query'      => $action->query,
				'status'     => $action->status,
				'is_primary' => $action->primary,
			);

			$data_format = array(
				'%d',
				'%s',
				'%s',
				'%s',
				'%s',
				'%d',
			);

			if ( ! empty( $action->id ) ) {
				$action_data['action_id'] = $action->id;
				$data_format[]            = '%d';
			}

			$wpdb->replace(
				$wpdb->prefix . 'wc_admin_note_actions',
				$action_data,
				$data_format
			);
		}

		// Update actions from DB (to grab new IDs).
		$this->read_actions( $note );
	}

	/**
	 * Return an ordered list of notes.
	 *
	 * @param array $args Query arguments.
	 * @return array An array of objects containing a note id.
	 */
	public function get_notes( $args = array() ) {
		global $wpdb;

		$defaults = array(
			'per_page' => get_option( 'posts_per_page' ),
			'page'     => 1,
			'order'    => 'DESC',
			'orderby'  => 'date_created',
		);
		$args     = wp_parse_args( $args, $defaults );

		$offset        = $args['per_page'] * ( $args['page'] - 1 );
		$where_clauses = $this->get_notes_where_clauses( $args );

		$query = $wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			"SELECT * FROM {$wpdb->prefix}wc_admin_notes WHERE 1=1{$where_clauses} ORDER BY {$args['orderby']} {$args['order']} LIMIT %d, %d",
			$offset,
			$args['per_page']
		);

		return $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	}

	/**
	 * Return a count of notes.
	 *
	 * @param string $type Comma separated list of note types.
	 * @param string $status Comma separated list of statuses.
	 * @return array An array of objects containing a note id.
	 */
	public function get_notes_count( $type = array(), $status = array() ) {
		global $wpdb;

		$where_clauses = $this->get_notes_where_clauses(
			array(
				'type'   => $type,
				'status' => $status,
			)
		);

		if ( ! empty( $where_clauses ) ) {
			// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_admin_notes WHERE 1=1{$where_clauses}" );
		}

		return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}wc_admin_notes" );
	}

	/**
	 * Return where clauses for getting notes by status and type. For use in both the count and listing queries.
	 *
	 *  @param array $args Array of args to pass.
	 * @return string Where clauses for the query.
	 */
	public function get_notes_where_clauses( $args = array() ) {
		$allowed_types    = WC_Admin_Note::get_allowed_types();
		$where_type_array = array();
		if ( isset( $args['type'] ) ) {
			foreach ( $args['type'] as $args_type ) {
				$args_type = trim( $args_type );
				if ( in_array( $args_type, $allowed_types, true ) ) {
					$where_type_array[] = "'" . esc_sql( $args_type ) . "'";
				}
			}
		}

		$allowed_statuses   = WC_Admin_Note::get_allowed_statuses();
		$where_status_array = array();
		if ( isset( $args['status'] ) ) {
			foreach ( $args['status'] as $args_status ) {
				$args_status = trim( $args_status );
				if ( in_array( $args_status, $allowed_statuses, true ) ) {
					$where_status_array[] = "'" . esc_sql( $args_status ) . "'";
				}
			}
		}

		$escaped_is_deleted = '';
		if ( isset( $args['is_deleted'] ) ) {
			$escaped_is_deleted = esc_sql( $args['is_deleted'] );
		}

		$where_name_array = [];
		if ( isset( $args['name'] ) ) {
			foreach ( $args['name'] as $args_name ) {
				$args_name          = trim( $args_name );
				$where_name_array[] = sprintf( "'%s'", esc_sql( $args_name ) );
			}
		}

		$escaped_where_types  = implode( ',', $where_type_array );
		$escaped_where_status = implode( ',', $where_status_array );
		$escaped_where_names  = implode( ',', $where_name_array );
		$where_clauses        = '';

		if ( ! empty( $escaped_where_types ) ) {
			$where_clauses .= " AND type IN ($escaped_where_types)";
		}

		if ( ! empty( $escaped_where_status ) ) {
			$where_clauses .= " AND status IN ($escaped_where_status)";
		}

		if ( ! empty( $escaped_where_names ) ) {
			$where_clauses .= " AND name IN ($escaped_where_names)";
		}

		$where_clauses .= $escaped_is_deleted ? ' AND is_deleted = 1' : ' AND is_deleted = 0';

		/**
		 * Filter the notes WHERE clause before retrieving the data.
		 *
		 * Allows modification of the notes select criterial.
		 *
		 * @param string $where_clauses The generated WHERE clause.
		 * @param array  $args          The original arguments for the request.
		 */
		return apply_filters( 'woocommerce_note_where_clauses', $where_clauses, $args );
	}

	/**
	 * Find all the notes with a given name.
	 *
	 * @param string $name Name to search for.
	 * @return array An array of matching note ids.
	 */
	public function get_notes_with_name( $name ) {
		global $wpdb;
		return $wpdb->get_col(
			$wpdb->prepare(
				"SELECT note_id FROM {$wpdb->prefix}wc_admin_notes WHERE name = %s ORDER BY note_id ASC",
				$name
			)
		);
	}
}